blob: 7704d46e983e82dc3e039290e9ab419e7b860061 [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 Moolenaar6acc79f2019-01-14 22:53:31 +0100219 semsg(_(e_invexpr2), 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 Moolenaarf9e3e092019-01-13 23:38:42 +0100300 semsg(_(e_invexpr2), 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 Moolenaar7480b5c2005-01-16 22:07:38 +0000935 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
Bram Moolenaar81ed4962020-09-23 15:56:50 +0200936 && !(lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100937 && !(lp->ll_tv->v_type == VAR_BLOB
938 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000939 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000940 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100941 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000942 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000943 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000944 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000945 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000946 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100947 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000948 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000949 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000950
Bram Moolenaar10c65862020-10-08 21:16:42 +0200951 if (in_vim9script() && lp->ll_valtype == NULL
952 && lp->ll_tv == &v->di_tv
953 && ht != NULL && ht == get_script_local_ht())
954 {
955 svar_T *sv = find_typval_in_script(lp->ll_tv);
956
957 // Vim9 script local variable: get the type
958 if (sv != NULL)
959 lp->ll_valtype = sv->sv_type;
960 }
961
Bram Moolenaar8c711452005-01-14 21:53:12 +0000962 len = -1;
963 if (*p == '.')
964 {
965 key = p + 1;
966 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
967 ;
968 if (len == 0)
969 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000970 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100971 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000972 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000973 }
974 p = key + len;
975 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000976 else
977 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100978 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000979 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000980 if (*p == ':')
981 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000982 else
983 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000984 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200985 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000986 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100987 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000988 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100989 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000990 clear_tv(&var1);
991 return NULL;
992 }
Bram Moolenaar6a250262020-08-04 15:53:01 +0200993 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000994 }
995
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100996 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000997 if (*p == ':')
998 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000999 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001000 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001001 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001002 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001003 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001004 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001005 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001006 if (rettv != NULL
1007 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001008 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001009 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001010 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001011 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001012 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001013 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001014 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001015 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001016 }
1017 p = skipwhite(p + 1);
1018 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001019 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001020 else
1021 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001022 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001023 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001024 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001025 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001026 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001027 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001028 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001029 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001030 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001031 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001032 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001033 clear_tv(&var2);
1034 return NULL;
1035 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001036 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001037 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001038 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001039 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001040 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001041
Bram Moolenaar8c711452005-01-14 21:53:12 +00001042 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001043 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001044 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001045 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001046 clear_tv(&var1);
1047 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001048 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001049 }
1050
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001051 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001052 ++p;
1053 }
1054
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001055 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001056 {
1057 if (len == -1)
1058 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001059 // "[key]": get key from "var1"
1060 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001061 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001062 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001063 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001064 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001065 }
1066 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001067 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001068
1069 // a NULL dict is equivalent with an empty dict
1070 if (lp->ll_tv->vval.v_dict == NULL)
1071 {
1072 lp->ll_tv->vval.v_dict = dict_alloc();
1073 if (lp->ll_tv->vval.v_dict == NULL)
1074 {
1075 clear_tv(&var1);
1076 return NULL;
1077 }
1078 ++lp->ll_tv->vval.v_dict->dv_refcount;
1079 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001080 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001081
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001082 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001083
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001084 // When assigning to a scope dictionary check that a function and
1085 // variable name is valid (only variable name unless it is l: or
1086 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001087 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001088 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001089 int prevval;
1090 int wrong;
1091
1092 if (len != -1)
1093 {
1094 prevval = key[len];
1095 key[len] = NUL;
1096 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001097 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001098 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001099 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1100 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001101 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar03290b82020-12-19 16:30:44 +01001102 || !valid_varname(key, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001103 if (len != -1)
1104 key[len] = prevval;
1105 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001106 {
1107 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001108 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001109 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001110 }
1111
Bram Moolenaar10c65862020-10-08 21:16:42 +02001112 if (lp->ll_valtype != NULL)
1113 // use the type of the member
1114 lp->ll_valtype = lp->ll_valtype->tt_member;
1115
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001116 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001117 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001118 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001119 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001120 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001121 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001122 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001123 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001124 return NULL;
1125 }
1126
Bram Moolenaar31b81602019-02-10 22:14:27 +01001127 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001128 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001129 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001130 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001131 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001132 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001133 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001134 }
1135 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001136 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001137 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001138 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001139 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001140 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001141 p = NULL;
1142 break;
1143 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001144 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001145 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001146 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1147 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001148 {
1149 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001150 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001151 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001152
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001153 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001154 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001155 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001156 else if (lp->ll_tv->v_type == VAR_BLOB)
1157 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001158 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1159
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001160 /*
1161 * Get the number and item for the only or first index of the List.
1162 */
1163 if (empty1)
1164 lp->ll_n1 = 0;
1165 else
1166 // is number or string
1167 lp->ll_n1 = (long)tv_get_number(&var1);
1168 clear_tv(&var1);
1169
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001170 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001171 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001172 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001173 return NULL;
1174 }
1175 if (lp->ll_range && !lp->ll_empty2)
1176 {
1177 lp->ll_n2 = (long)tv_get_number(&var2);
1178 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001179 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1180 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001181 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001182 }
1183 lp->ll_blob = lp->ll_tv->vval.v_blob;
1184 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001185 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001186 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001187 else
1188 {
1189 /*
1190 * Get the number and item for the only or first index of the List.
1191 */
1192 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001193 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001194 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001195 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001196 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001197 clear_tv(&var1);
1198
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001199 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001200 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001201 lp->ll_li = list_find_index(lp->ll_list, &lp->ll_n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001202 if (lp->ll_li == NULL)
1203 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001204 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001205 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001206 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001207 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001208 }
1209
Bram Moolenaar10c65862020-10-08 21:16:42 +02001210 if (lp->ll_valtype != NULL)
1211 // use the type of the member
1212 lp->ll_valtype = lp->ll_valtype->tt_member;
1213
Bram Moolenaar8c711452005-01-14 21:53:12 +00001214 /*
1215 * May need to find the item or absolute index for the second
1216 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001217 * When no index given: "lp->ll_empty2" is TRUE.
1218 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001219 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001220 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001221 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001222 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001223 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001224 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001225 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001226 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001227 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001228 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001229 {
1230 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001231 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001232 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001233 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001234 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001235 }
1236
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001237 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001238 if (lp->ll_n1 < 0)
1239 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1240 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001241 {
1242 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001243 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001244 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001245 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001246 }
1247
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001248 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001249 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001250 }
1251
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001252 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001253 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001254 return p;
1255}
1256
1257/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001258 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001259 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001260 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001261clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001262{
1263 vim_free(lp->ll_exp_name);
1264 vim_free(lp->ll_newkey);
1265}
1266
1267/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001268 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001269 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001270 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1271 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001272 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001273 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001274set_var_lval(
1275 lval_T *lp,
1276 char_u *endp,
1277 typval_T *rettv,
1278 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001279 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1280 char_u *op,
1281 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001282{
1283 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001284 listitem_T *ri;
1285 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001286
1287 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001288 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001289 cc = *endp;
1290 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001291 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1292 return;
1293
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001294 if (lp->ll_blob != NULL)
1295 {
1296 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001297
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001298 if (op != NULL && *op != '=')
1299 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001300 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001301 return;
1302 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001303 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001304 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001305
1306 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1307 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001308 if (lp->ll_empty2)
1309 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1310
Bram Moolenaar68452172021-04-12 21:21:02 +02001311 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1312 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001313 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001314 }
1315 else
1316 {
1317 val = (int)tv_get_number_chk(rettv, &error);
1318 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001319 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001320 }
1321 }
1322 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001323 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001324 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001325
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001326 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1327 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001328 {
1329 emsg(_(e_cannot_mod));
1330 *endp = cc;
1331 return;
1332 }
1333
Bram Moolenaarff697e62019-02-12 22:28:33 +01001334 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001335 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001336 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001337 &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001338 {
1339 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001340 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1341 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001342 && tv_op(&tv, rettv, op) == OK)
1343 set_var(lp->ll_name, &tv, FALSE);
1344 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001345 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001346 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001347 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001348 {
1349 if (lp->ll_type != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001350 && check_typval_arg_type(lp->ll_type, rettv, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001351 return;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001352 set_var_const(lp->ll_name, lp->ll_type, rettv, copy,
1353 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001354 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001355 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001356 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001357 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001358 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001359 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001360 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001361 else if (lp->ll_range)
1362 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001363 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001364 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001365
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001366 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1367 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001368 {
1369 emsg(_("E996: Cannot lock a range"));
1370 return;
1371 }
1372
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001373 /*
1374 * Check whether any of the list items is locked
1375 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001376 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001377 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001378 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001379 return;
1380 ri = ri->li_next;
1381 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1382 break;
1383 ll_li = ll_li->li_next;
1384 ++ll_n1;
1385 }
1386
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001387 /*
1388 * Assign the List values to the list items.
1389 */
1390 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001391 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001392 if (op != NULL && *op != '=')
1393 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1394 else
1395 {
1396 clear_tv(&lp->ll_li->li_tv);
1397 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1398 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001399 ri = ri->li_next;
1400 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1401 break;
1402 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001403 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001404 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001405 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001406 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001407 ri = NULL;
1408 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001409 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001410 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001411 lp->ll_li = lp->ll_li->li_next;
1412 ++lp->ll_n1;
1413 }
1414 if (ri != NULL)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001415 emsg(_(e_list_value_has_more_items_than_targets));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001416 else if (lp->ll_empty2
1417 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001418 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001419 emsg(_(e_list_value_does_not_have_enough_items));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001420 }
1421 else
1422 {
1423 /*
1424 * Assign to a List or Dictionary item.
1425 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001426 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1427 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001428 {
1429 emsg(_("E996: Cannot lock a list or dict"));
1430 return;
1431 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001432
1433 if (lp->ll_valtype != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001434 && check_typval_arg_type(lp->ll_valtype, rettv, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001435 return;
1436
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001437 if (lp->ll_newkey != NULL)
1438 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001439 if (op != NULL && *op != '=')
1440 {
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02001441 semsg(_(e_dictkey), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001442 return;
1443 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001444 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1445 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001446 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001447
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001448 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001449 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001450 if (di == NULL)
1451 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001452 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1453 {
1454 vim_free(di);
1455 return;
1456 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001457 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001458 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001459 else if (op != NULL && *op != '=')
1460 {
1461 tv_op(lp->ll_tv, rettv, op);
1462 return;
1463 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001464 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001465 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001466
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001467 /*
1468 * Assign the value to the variable or list item.
1469 */
1470 if (copy)
1471 copy_tv(rettv, lp->ll_tv);
1472 else
1473 {
1474 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001475 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001476 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001477 }
1478 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001479}
1480
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001481/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001482 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1483 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001484 * Returns OK or FAIL.
1485 */
1486 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001487tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001488{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001489 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001490 char_u numbuf[NUMBUFLEN];
1491 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001492 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001493
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001494 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001495 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001496 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001497 {
1498 switch (tv1->v_type)
1499 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001500 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001501 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001502 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001503 case VAR_DICT:
1504 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001505 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001506 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001507 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001508 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001509 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001510 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001511 break;
1512
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001513 case VAR_BLOB:
1514 if (*op != '+' || tv2->v_type != VAR_BLOB)
1515 break;
1516 // BLOB += BLOB
1517 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1518 {
1519 blob_T *b1 = tv1->vval.v_blob;
1520 blob_T *b2 = tv2->vval.v_blob;
1521 int i, len = blob_len(b2);
1522 for (i = 0; i < len; i++)
1523 ga_append(&b1->bv_ga, blob_get(b2, i));
1524 }
1525 return OK;
1526
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001527 case VAR_LIST:
1528 if (*op != '+' || tv2->v_type != VAR_LIST)
1529 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001530 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001531 if (tv2->vval.v_list != NULL)
1532 {
1533 if (tv1->vval.v_list == NULL)
1534 {
1535 tv1->vval.v_list = tv2->vval.v_list;
1536 ++tv1->vval.v_list->lv_refcount;
1537 }
1538 else
1539 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1540 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001541 return OK;
1542
1543 case VAR_NUMBER:
1544 case VAR_STRING:
1545 if (tv2->v_type == VAR_LIST)
1546 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001547 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001548 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001549 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001550 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001551#ifdef FEAT_FLOAT
1552 if (tv2->v_type == VAR_FLOAT)
1553 {
1554 float_T f = n;
1555
Bram Moolenaarff697e62019-02-12 22:28:33 +01001556 if (*op == '%')
1557 break;
1558 switch (*op)
1559 {
1560 case '+': f += tv2->vval.v_float; break;
1561 case '-': f -= tv2->vval.v_float; break;
1562 case '*': f *= tv2->vval.v_float; break;
1563 case '/': f /= tv2->vval.v_float; break;
1564 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001565 clear_tv(tv1);
1566 tv1->v_type = VAR_FLOAT;
1567 tv1->vval.v_float = f;
1568 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001569 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001570#endif
1571 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001572 switch (*op)
1573 {
1574 case '+': n += tv_get_number(tv2); break;
1575 case '-': n -= tv_get_number(tv2); break;
1576 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001577 case '/': n = num_divide(n, tv_get_number(tv2),
1578 &failed); break;
1579 case '%': n = num_modulus(n, tv_get_number(tv2),
1580 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001581 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001582 clear_tv(tv1);
1583 tv1->v_type = VAR_NUMBER;
1584 tv1->vval.v_number = n;
1585 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001586 }
1587 else
1588 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001589 if (tv2->v_type == VAR_FLOAT)
1590 break;
1591
Bram Moolenaarff697e62019-02-12 22:28:33 +01001592 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001593 s = tv_get_string(tv1);
1594 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001595 clear_tv(tv1);
1596 tv1->v_type = VAR_STRING;
1597 tv1->vval.v_string = s;
1598 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001599 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001600
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001601 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001602#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001603 {
1604 float_T f;
1605
Bram Moolenaarff697e62019-02-12 22:28:33 +01001606 if (*op == '%' || *op == '.'
1607 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001608 && tv2->v_type != VAR_NUMBER
1609 && tv2->v_type != VAR_STRING))
1610 break;
1611 if (tv2->v_type == VAR_FLOAT)
1612 f = tv2->vval.v_float;
1613 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001614 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001615 switch (*op)
1616 {
1617 case '+': tv1->vval.v_float += f; break;
1618 case '-': tv1->vval.v_float -= f; break;
1619 case '*': tv1->vval.v_float *= f; break;
1620 case '/': tv1->vval.v_float /= f; break;
1621 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001622 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001623#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001624 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001625 }
1626 }
1627
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001628 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001629 return FAIL;
1630}
1631
1632/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001633 * Evaluate the expression used in a ":for var in expr" command.
1634 * "arg" points to "var".
1635 * Set "*errp" to TRUE for an error, FALSE otherwise;
1636 * Return a pointer that holds the info. Null when there is an error.
1637 */
1638 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001639eval_for_line(
1640 char_u *arg,
1641 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001642 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001643 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001644{
Bram Moolenaar33570922005-01-25 22:26:29 +00001645 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001646 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001647 typval_T tv;
1648 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001649 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001650
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001651 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001652
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001653 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001654 if (fi == NULL)
1655 return NULL;
1656
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001657 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001658 if (expr == NULL)
1659 return fi;
1660
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001661 expr = skipwhite_and_linebreak(expr, evalarg);
1662 if (expr[0] != 'i' || expr[1] != 'n'
1663 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001664 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001665 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001666 return fi;
1667 }
1668
1669 if (skip)
1670 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001671 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1672 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001673 {
1674 *errp = FALSE;
1675 if (!skip)
1676 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001677 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001678 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001679 l = tv.vval.v_list;
1680 if (l == NULL)
1681 {
1682 // a null list is like an empty list: do nothing
1683 clear_tv(&tv);
1684 }
1685 else
1686 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001687 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001688 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001689
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001690 // No need to increment the refcount, it's already set for
1691 // the list being used in "tv".
1692 fi->fi_list = l;
1693 list_add_watch(l, &fi->fi_lw);
1694 fi->fi_lw.lw_item = l->lv_first;
1695 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001696 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001697 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001698 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001699 fi->fi_bi = 0;
1700 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001701 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001702 typval_T btv;
1703
1704 // Make a copy, so that the iteration still works when the
1705 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001706 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001707 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001708 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001709 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001710 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001711 else if (tv.v_type == VAR_STRING)
1712 {
1713 fi->fi_byte_idx = 0;
1714 fi->fi_string = tv.vval.v_string;
1715 tv.vval.v_string = NULL;
1716 if (fi->fi_string == NULL)
1717 fi->fi_string = vim_strsave((char_u *)"");
1718 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001719 else
1720 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001721 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001722 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001723 }
1724 }
1725 }
1726 if (skip)
1727 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001728 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001729
1730 return fi;
1731}
1732
1733/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001734 * Used when looping over a :for line, skip the "in expr" part.
1735 */
1736 void
1737skip_for_lines(void *fi_void, evalarg_T *evalarg)
1738{
1739 forinfo_T *fi = (forinfo_T *)fi_void;
1740 int i;
1741
1742 for (i = 0; i < fi->fi_break_count; ++i)
1743 eval_next_line(evalarg);
1744}
1745
1746/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001747 * Use the first item in a ":for" list. Advance to the next.
1748 * Assign the values to the variable (list). "arg" points to the first one.
1749 * Return TRUE when a valid item was found, FALSE when at end of list or
1750 * something wrong.
1751 */
1752 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001753next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001754{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001755 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001756 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001757 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
1758 ? (ASSIGN_FINAL | ASSIGN_DECL | ASSIGN_NO_MEMBER_TYPE)
1759 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001760 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001761
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001762 if (fi->fi_blob != NULL)
1763 {
1764 typval_T tv;
1765
1766 if (fi->fi_bi >= blob_len(fi->fi_blob))
1767 return FALSE;
1768 tv.v_type = VAR_NUMBER;
1769 tv.v_lock = VAR_FIXED;
1770 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1771 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001772 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001773 fi->fi_varcount, flag, NULL) == OK;
1774 }
1775
1776 if (fi->fi_string != NULL)
1777 {
1778 typval_T tv;
1779 int len;
1780
1781 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1782 if (len == 0)
1783 return FALSE;
1784 tv.v_type = VAR_STRING;
1785 tv.v_lock = VAR_FIXED;
1786 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1787 fi->fi_byte_idx += len;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001788 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001789 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001790 vim_free(tv.vval.v_string);
1791 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001792 }
1793
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001794 item = fi->fi_lw.lw_item;
1795 if (item == NULL)
1796 result = FALSE;
1797 else
1798 {
1799 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001800 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001801 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001802 }
1803 return result;
1804}
1805
1806/*
1807 * Free the structure used to store info used by ":for".
1808 */
1809 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001810free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001811{
Bram Moolenaar33570922005-01-25 22:26:29 +00001812 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001813
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001814 if (fi == NULL)
1815 return;
1816 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001817 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001818 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001819 list_unref(fi->fi_list);
1820 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001821 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001822 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001823 else
1824 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001825 vim_free(fi);
1826}
1827
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001829set_context_for_expression(
1830 expand_T *xp,
1831 char_u *arg,
1832 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001834 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001836 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001838 if (cmdidx == CMD_let || cmdidx == CMD_var
1839 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001840 {
1841 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001842 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001843 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001844 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001845 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001846 {
1847 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001848 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001849 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001850 break;
1851 }
1852 return;
1853 }
1854 }
1855 else
1856 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1857 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 while ((xp->xp_pattern = vim_strpbrk(arg,
1859 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1860 {
1861 c = *xp->xp_pattern;
1862 if (c == '&')
1863 {
1864 c = xp->xp_pattern[1];
1865 if (c == '&')
1866 {
1867 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001868 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 }
1870 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001871 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001873 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1874 xp->xp_pattern += 2;
1875
1876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 }
1878 else if (c == '$')
1879 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001880 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 xp->xp_context = EXPAND_ENV_VARS;
1882 }
1883 else if (c == '=')
1884 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001885 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 xp->xp_context = EXPAND_EXPRESSION;
1887 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001888 else if (c == '#'
1889 && xp->xp_context == EXPAND_EXPRESSION)
1890 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001891 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001892 break;
1893 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001894 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 && xp->xp_context == EXPAND_FUNCTIONS
1896 && vim_strchr(xp->xp_pattern, '(') == NULL)
1897 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001898 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 break;
1900 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001901 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001903 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 {
1905 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1906 if (c == '\\' && xp->xp_pattern[1] != NUL)
1907 ++xp->xp_pattern;
1908 xp->xp_context = EXPAND_NOTHING;
1909 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001910 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001912 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1914 /* skip */ ;
1915 xp->xp_context = EXPAND_NOTHING;
1916 }
1917 else if (c == '|')
1918 {
1919 if (xp->xp_pattern[1] == '|')
1920 {
1921 ++xp->xp_pattern;
1922 xp->xp_context = EXPAND_EXPRESSION;
1923 }
1924 else
1925 xp->xp_context = EXPAND_COMMANDS;
1926 }
1927 else
1928 xp->xp_context = EXPAND_EXPRESSION;
1929 }
1930 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001931 // Doesn't look like something valid, expand as an expression
1932 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001933 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 arg = xp->xp_pattern;
1935 if (*arg != NUL)
1936 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1937 /* skip */ ;
1938 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01001939
1940 // ":exe one two" completes "two"
1941 if ((cmdidx == CMD_execute
1942 || cmdidx == CMD_echo
1943 || cmdidx == CMD_echon
1944 || cmdidx == CMD_echomsg)
1945 && xp->xp_context == EXPAND_EXPRESSION)
1946 {
1947 for (;;)
1948 {
1949 char_u *n = skiptowhite(arg);
1950
1951 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
1952 break;
1953 arg = skipwhite(n);
1954 }
1955 }
1956
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 xp->xp_pattern = arg;
1958}
1959
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001961 * Return TRUE if "pat" matches "text".
1962 * Does not use 'cpo' and always uses 'magic'.
1963 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001964 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001965pattern_match(char_u *pat, char_u *text, int ic)
1966{
1967 int matches = FALSE;
1968 char_u *save_cpo;
1969 regmatch_T regmatch;
1970
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001971 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001972 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01001973 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001974 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1975 if (regmatch.regprog != NULL)
1976 {
1977 regmatch.rm_ic = ic;
1978 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1979 vim_regfree(regmatch.regprog);
1980 }
1981 p_cpo = save_cpo;
1982 return matches;
1983}
1984
1985/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001986 * Handle a name followed by "(". Both for just "name(arg)" and for
1987 * "expr->name(arg)".
1988 * Returns OK or FAIL.
1989 */
1990 static int
1991eval_func(
1992 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02001993 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001994 char_u *name,
1995 int name_len,
1996 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001997 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001998 typval_T *basetv) // "expr" for "expr->name(arg)"
1999{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002000 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002001 char_u *s = name;
2002 int len = name_len;
2003 partial_T *partial;
2004 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002005 type_T *type = NULL;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002006
2007 if (!evaluate)
2008 check_vars(s, len);
2009
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002010 // If "s" is the name of a variable of type VAR_FUNC
2011 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002012 s = deref_func_name(s, &len, &partial,
2013 in_vim9script() ? &type : NULL, !evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002014
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002015 // Need to make a copy, in case evaluating the arguments makes
2016 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002017 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002018 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002019 ret = FAIL;
2020 else
2021 {
2022 funcexe_T funcexe;
2023
2024 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002025 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002026 funcexe.firstline = curwin->w_cursor.lnum;
2027 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002028 funcexe.evaluate = evaluate;
2029 funcexe.partial = partial;
2030 funcexe.basetv = basetv;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002031 funcexe.check_type = type;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002032 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002033 }
2034 vim_free(s);
2035
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002036 // If evaluate is FALSE rettv->v_type was not set in
2037 // get_func_tv, but it's needed in handle_subscript() to parse
2038 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002039 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2040 {
2041 rettv->vval.v_string = NULL;
2042 rettv->v_type = VAR_FUNC;
2043 }
2044
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002045 // Stop the expression evaluation when immediately
2046 // aborting on error, or when an interrupt occurred or
2047 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002048 if (evaluate && aborting())
2049 {
2050 if (ret == OK)
2051 clear_tv(rettv);
2052 ret = FAIL;
2053 }
2054 return ret;
2055}
2056
2057/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002058 * Get the next line source line without advancing. But do skip over comment
2059 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002060 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002061 */
2062 static char_u *
2063getline_peek_skip_comments(evalarg_T *evalarg)
2064{
2065 for (;;)
2066 {
2067 char_u *next = getline_peek(evalarg->eval_getline,
2068 evalarg->eval_cookie);
2069 char_u *p;
2070
2071 if (next == NULL)
2072 break;
2073 p = skipwhite(next);
2074 if (*p != NUL && !vim9_comment_start(p))
2075 return next;
2076 (void)eval_next_line(evalarg);
2077 }
2078 return NULL;
2079}
2080
2081/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002082 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2083 * comment) and there is a next line, return the next line (skipping blanks)
2084 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002085 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2086 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002087 * "arg" must point somewhere inside a line, not at the start.
2088 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002089 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002090eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2091{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002092 char_u *p = skipwhite(arg);
2093
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002094 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002095 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002096 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002097 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002098 && (*p == NUL || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002099 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002100 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002101
2102 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002103 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002104 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002105 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002106
Bram Moolenaar9d489562020-07-30 20:08:50 +02002107 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002108 {
2109 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002110 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002111 }
2112 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002113 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002114}
2115
2116/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002117 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002118 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002119 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002120 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002121eval_next_line(evalarg_T *evalarg)
2122{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002123 garray_T *gap = &evalarg->eval_ga;
2124 char_u *line;
2125
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002126 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002127 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2128 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002129 else
2130 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002131 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002132 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2133 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002134 char_u *p = skipwhite(line);
2135
2136 // Going to concatenate the lines after parsing. For an empty or
2137 // comment line use an empty string.
2138 if (*p == NUL || vim9_comment_start(p))
2139 {
2140 vim_free(line);
2141 line = vim_strsave((char_u *)"");
2142 }
2143
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002144 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2145 ++gap->ga_len;
2146 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002147 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002148 {
2149 vim_free(evalarg->eval_tofree);
2150 evalarg->eval_tofree = line;
2151 }
2152 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002153}
2154
Bram Moolenaare6b53242020-07-01 17:28:33 +02002155/*
2156 * Call eval_next_non_blank() and get the next line if needed.
2157 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002158 char_u *
2159skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2160{
2161 int getnext;
2162 char_u *p = skipwhite(arg);
2163
Bram Moolenaar962d7212020-07-04 14:15:00 +02002164 if (evalarg == NULL)
2165 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002166 eval_next_non_blank(p, evalarg, &getnext);
2167 if (getnext)
2168 return eval_next_line(evalarg);
2169 return p;
2170}
2171
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002172/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002173 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002174 */
2175 void
2176clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2177{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002178 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002179 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002180 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002181 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002182 if (eap != NULL)
2183 {
2184 // We may need to keep the original command line, e.g. for
2185 // ":let" it has the variable names. But we may also need the
2186 // new one, "nextcmd" points into it. Keep both.
2187 vim_free(eap->cmdline_tofree);
2188 eap->cmdline_tofree = *eap->cmdlinep;
2189 *eap->cmdlinep = evalarg->eval_tofree;
2190 }
2191 else
2192 vim_free(evalarg->eval_tofree);
2193 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002194 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002195
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002196 VIM_CLEAR(evalarg->eval_tofree_cmdline);
2197 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002198 }
2199}
2200
2201/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002203 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2205 */
2206
2207/*
2208 * Handle zero level expression.
2209 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002210 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002211 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002212 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 * Return OK or FAIL.
2214 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002215 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002216eval0(
2217 char_u *arg,
2218 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002219 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002220 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221{
2222 int ret;
2223 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002224 int did_emsg_before = did_emsg;
2225 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002226 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002227 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228
2229 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002230 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002231 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002232
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002233 if (ret != FAIL)
2234 end_error = !ends_excmd2(arg, p);
2235 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 {
2237 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 /*
2240 * Report the invalid expression unless the expression evaluation has
2241 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002242 * exception, or we already gave a more specific error.
2243 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002245 if (!aborting()
2246 && did_emsg == did_emsg_before
2247 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002248 && (flags & EVAL_CONSTANT) == 0
2249 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002250 {
2251 if (end_error)
2252 semsg(_(e_trailing_arg), p);
2253 else
2254 semsg(_(e_invexpr2), arg);
2255 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002256
2257 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002258 // a next command to avoid more errors, unless "|" is following, which
2259 // could only be a command separator.
2260 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2261 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002262 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002264
2265 if (eap != NULL)
2266 eap->nextcmd = check_nextcmd(p);
2267
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 return ret;
2269}
2270
2271/*
2272 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002273 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002274 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 *
2276 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002277 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002279 * Note: "rettv.v_lock" is not set.
2280 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 * Return OK or FAIL.
2282 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002283 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002284eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002286 char_u *p;
2287 int getnext;
2288
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002289 CLEAR_POINTER(rettv);
2290
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 /*
2292 * Get the first variable.
2293 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002294 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 return FAIL;
2296
Bram Moolenaar793648f2020-06-26 21:28:25 +02002297 p = eval_next_non_blank(*arg, evalarg, &getnext);
2298 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002300 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002301 int result;
2302 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002303 evalarg_T *evalarg_used = evalarg;
2304 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002305 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002306 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002307 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002308
2309 if (evalarg == NULL)
2310 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002311 CLEAR_FIELD(local_evalarg);
2312 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002313 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002314 orig_flags = evalarg_used->eval_flags;
2315 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002316
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002317 if (getnext)
2318 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002319 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002320 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002321 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002322 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002323 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002324 clear_tv(rettv);
2325 return FAIL;
2326 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002327 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002328 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002329
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002331 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002333 int error = FALSE;
2334
Bram Moolenaar13106602020-10-04 16:06:05 +02002335 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002336 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002337 else if (vim9script)
2338 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002339 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002341 if (error || !op_falsy || !result)
2342 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002343 if (error)
2344 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 }
2346
2347 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002348 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002350 if (op_falsy)
2351 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002352 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002353 {
mityu4ac198c2021-05-28 17:52:40 +02002354 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002355 clear_tv(rettv);
2356 return FAIL;
2357 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002358 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002359 evalarg_used->eval_flags = (op_falsy ? !result : result)
2360 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2361 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002362 {
2363 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002365 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002366 if (!op_falsy || !result)
2367 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002369 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002371 /*
2372 * Check for the ":".
2373 */
2374 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2375 if (*p != ':')
2376 {
2377 emsg(_(e_missing_colon));
2378 if (evaluate && result)
2379 clear_tv(rettv);
2380 evalarg_used->eval_flags = orig_flags;
2381 return FAIL;
2382 }
2383 if (getnext)
2384 *arg = eval_next_line(evalarg_used);
2385 else
2386 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002387 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002388 {
2389 error_white_both(p, 1);
2390 clear_tv(rettv);
2391 evalarg_used->eval_flags = orig_flags;
2392 return FAIL;
2393 }
2394 *arg = p;
2395 }
2396
2397 /*
2398 * Get the third variable. Recursive!
2399 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002400 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002401 {
mityu4ac198c2021-05-28 17:52:40 +02002402 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002403 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002404 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002405 return FAIL;
2406 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002407 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2408 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002409 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002410 if (eval1(arg, &var2, evalarg_used) == FAIL)
2411 {
2412 if (evaluate && result)
2413 clear_tv(rettv);
2414 evalarg_used->eval_flags = orig_flags;
2415 return FAIL;
2416 }
2417 if (evaluate && !result)
2418 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002420
2421 if (evalarg == NULL)
2422 clear_evalarg(&local_evalarg, NULL);
2423 else
2424 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 }
2426
2427 return OK;
2428}
2429
2430/*
2431 * Handle first level expression:
2432 * expr2 || expr2 || expr2 logical OR
2433 *
2434 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002435 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 *
2437 * Return OK or FAIL.
2438 */
2439 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002440eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002442 char_u *p;
2443 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444
2445 /*
2446 * Get the first variable.
2447 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002448 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 return FAIL;
2450
2451 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002452 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002454 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002455 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002457 evalarg_T *evalarg_used = evalarg;
2458 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002459 int evaluate;
2460 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002461 long result = FALSE;
2462 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002463 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002464 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002465
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002466 if (evalarg == NULL)
2467 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002468 CLEAR_FIELD(local_evalarg);
2469 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002470 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002471 orig_flags = evalarg_used->eval_flags;
2472 evaluate = orig_flags & EVAL_EVALUATE;
2473 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002474 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002475 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002476 result = tv_get_bool_chk(rettv, &error);
2477 else if (tv_get_number_chk(rettv, &error) != 0)
2478 result = TRUE;
2479 clear_tv(rettv);
2480 if (error)
2481 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 }
2483
2484 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002485 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002487 while (p[0] == '|' && p[1] == '|')
2488 {
2489 if (getnext)
2490 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002491 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002492 {
2493 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2494 {
2495 error_white_both(p, 2);
2496 clear_tv(rettv);
2497 return FAIL;
2498 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002499 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002500 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002501
2502 /*
2503 * Get the second variable.
2504 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002505 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2506 {
mityu4ac198c2021-05-28 17:52:40 +02002507 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002508 clear_tv(rettv);
2509 return FAIL;
2510 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002511 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2512 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002513 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002514 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002515 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002516
2517 /*
2518 * Compute the result.
2519 */
2520 if (evaluate && !result)
2521 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002522 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002523 result = tv_get_bool_chk(&var2, &error);
2524 else if (tv_get_number_chk(&var2, &error) != 0)
2525 result = TRUE;
2526 clear_tv(&var2);
2527 if (error)
2528 return FAIL;
2529 }
2530 if (evaluate)
2531 {
2532 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002533 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002534 rettv->v_type = VAR_BOOL;
2535 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002536 }
2537 else
2538 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002539 rettv->v_type = VAR_NUMBER;
2540 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002541 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002542 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002543
2544 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002546
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002547 if (evalarg == NULL)
2548 clear_evalarg(&local_evalarg, NULL);
2549 else
2550 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 }
2552
2553 return OK;
2554}
2555
2556/*
2557 * Handle second level expression:
2558 * expr3 && expr3 && expr3 logical AND
2559 *
2560 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002561 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 *
2563 * Return OK or FAIL.
2564 */
2565 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002566eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002568 char_u *p;
2569 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570
2571 /*
2572 * Get the first variable.
2573 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002574 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 return FAIL;
2576
2577 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002578 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002580 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002581 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002583 evalarg_T *evalarg_used = evalarg;
2584 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002585 int orig_flags;
2586 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002587 long result = TRUE;
2588 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002589 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002590 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002591
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002592 if (evalarg == NULL)
2593 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002594 CLEAR_FIELD(local_evalarg);
2595 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002596 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002597 orig_flags = evalarg_used->eval_flags;
2598 evaluate = orig_flags & EVAL_EVALUATE;
2599 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002600 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002601 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002602 result = tv_get_bool_chk(rettv, &error);
2603 else if (tv_get_number_chk(rettv, &error) == 0)
2604 result = FALSE;
2605 clear_tv(rettv);
2606 if (error)
2607 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 }
2609
2610 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002611 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002613 while (p[0] == '&' && p[1] == '&')
2614 {
2615 if (getnext)
2616 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002617 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002618 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002619 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002620 {
2621 error_white_both(p, 2);
2622 clear_tv(rettv);
2623 return FAIL;
2624 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002625 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002626 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002627
2628 /*
2629 * Get the second variable.
2630 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002631 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2632 {
mityu4ac198c2021-05-28 17:52:40 +02002633 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002634 clear_tv(rettv);
2635 return FAIL;
2636 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002637 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2638 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002639 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002640 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002641 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002642 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002643
2644 /*
2645 * Compute the result.
2646 */
2647 if (evaluate && result)
2648 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002649 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002650 result = tv_get_bool_chk(&var2, &error);
2651 else if (tv_get_number_chk(&var2, &error) == 0)
2652 result = FALSE;
2653 clear_tv(&var2);
2654 if (error)
2655 return FAIL;
2656 }
2657 if (evaluate)
2658 {
2659 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002660 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002661 rettv->v_type = VAR_BOOL;
2662 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002663 }
2664 else
2665 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002666 rettv->v_type = VAR_NUMBER;
2667 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002668 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002669 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002670
2671 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002673
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002674 if (evalarg == NULL)
2675 clear_evalarg(&local_evalarg, NULL);
2676 else
2677 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 }
2679
2680 return OK;
2681}
2682
2683/*
2684 * Handle third level expression:
2685 * var1 == var2
2686 * var1 =~ var2
2687 * var1 != var2
2688 * var1 !~ var2
2689 * var1 > var2
2690 * var1 >= var2
2691 * var1 < var2
2692 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002693 * var1 is var2
2694 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 *
2696 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002697 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 *
2699 * Return OK or FAIL.
2700 */
2701 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002702eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002705 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002706 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002708 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709
2710 /*
2711 * Get the first variable.
2712 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002713 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 return FAIL;
2715
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002716 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002717 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718
2719 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002720 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002722 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002724 typval_T var2;
2725 int ic;
2726 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002727 int evaluate = evalarg == NULL
2728 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002729
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002730 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002731 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002732 *arg = eval_next_line(evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002733 p = *arg;
2734 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002735 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2736 {
mityu4ac198c2021-05-28 17:52:40 +02002737 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002738 clear_tv(rettv);
2739 return FAIL;
2740 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002741
Bram Moolenaar696ba232020-07-29 21:20:41 +02002742 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2743 {
2744 semsg(_(e_invexpr2), p);
2745 clear_tv(rettv);
2746 return FAIL;
2747 }
2748
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002749 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 if (p[len] == '?')
2751 {
2752 ic = TRUE;
2753 ++len;
2754 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002755 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 else if (p[len] == '#')
2757 {
2758 ic = FALSE;
2759 ++len;
2760 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002761 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002763 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764
2765 /*
2766 * Get the second variable.
2767 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002768 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2769 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002770 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002771 clear_tv(rettv);
2772 return FAIL;
2773 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002774 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002775 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002777 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 return FAIL;
2779 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002780 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002781 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002782 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002783
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002784 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002785 {
2786 ret = FAIL;
2787 clear_tv(rettv);
2788 }
2789 else
2790 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002791 clear_tv(&var2);
2792 return ret;
2793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 }
2795
2796 return OK;
2797}
2798
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002799/*
2800 * Make a copy of blob "tv1" and append blob "tv2".
2801 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002802 void
2803eval_addblob(typval_T *tv1, typval_T *tv2)
2804{
2805 blob_T *b1 = tv1->vval.v_blob;
2806 blob_T *b2 = tv2->vval.v_blob;
2807 blob_T *b = blob_alloc();
2808 int i;
2809
2810 if (b != NULL)
2811 {
2812 for (i = 0; i < blob_len(b1); i++)
2813 ga_append(&b->bv_ga, blob_get(b1, i));
2814 for (i = 0; i < blob_len(b2); i++)
2815 ga_append(&b->bv_ga, blob_get(b2, i));
2816
2817 clear_tv(tv1);
2818 rettv_blob_set(tv1, b);
2819 }
2820}
2821
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002822/*
2823 * Make a copy of list "tv1" and append list "tv2".
2824 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002825 int
2826eval_addlist(typval_T *tv1, typval_T *tv2)
2827{
2828 typval_T var3;
2829
2830 // concatenate Lists
2831 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2832 {
2833 clear_tv(tv1);
2834 clear_tv(tv2);
2835 return FAIL;
2836 }
2837 clear_tv(tv1);
2838 *tv1 = var3;
2839 return OK;
2840}
2841
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842/*
2843 * Handle fourth level expression:
2844 * + number addition
2845 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002846 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002847 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 *
2849 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002850 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 *
2852 * Return OK or FAIL.
2853 */
2854 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002855eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 /*
2858 * Get the first variable.
2859 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002860 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 return FAIL;
2862
2863 /*
2864 * Repeat computing, until no '+', '-' or '.' is following.
2865 */
2866 for (;;)
2867 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002868 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002869 int getnext;
2870 char_u *p;
2871 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002872 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002873 int concat;
2874 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002875 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002876
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002877 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002878 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002879 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002880 p = eval_next_non_blank(*arg, evalarg, &getnext);
2881 op = *p;
2882 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002883 if ((op != '+' && op != '-' && !concat) || p[1] == '='
2884 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002886 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
2887 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002888
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002889 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002890 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002891 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002892 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002893 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002894 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002895 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002896 {
mityu4ac198c2021-05-28 17:52:40 +02002897 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002898 clear_tv(rettv);
2899 return FAIL;
2900 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002901 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002902 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002903 if ((op != '+' || (rettv->v_type != VAR_LIST
2904 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002905#ifdef FEAT_FLOAT
2906 && (op == '.' || rettv->v_type != VAR_FLOAT)
2907#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002908 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002909 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002910 int error = FALSE;
2911
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002912 // For "list + ...", an illegal use of the first operand as
2913 // a number cannot be determined before evaluating the 2nd
2914 // operand: if this is also a list, all is ok.
2915 // For "something . ...", "something - ..." or "non-list + ...",
2916 // we know that the first operand needs to be a string or number
2917 // without evaluating the 2nd operand. So check before to avoid
2918 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002919 if (op != '.')
2920 tv_get_number_chk(rettv, &error);
2921 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002922 {
2923 clear_tv(rettv);
2924 return FAIL;
2925 }
2926 }
2927
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 /*
2929 * Get the second variable.
2930 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002931 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002932 {
mityu89dcb4d2021-05-28 13:50:17 +02002933 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002934 clear_tv(rettv);
2935 return FAIL;
2936 }
2937 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002938 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002940 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 return FAIL;
2942 }
2943
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002944 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 {
2946 /*
2947 * Compute the result.
2948 */
2949 if (op == '.')
2950 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002951 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2952 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002953 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002954
Bram Moolenaar2e086612020-08-25 22:37:48 +02002955 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002956 || var2.v_type == VAR_CHANNEL
2957 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02002958 semsg(_(e_using_invalid_value_as_string_str),
2959 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002960#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02002961 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002962 {
2963 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2964 var2.vval.v_float);
2965 s2 = buf2;
2966 }
2967#endif
2968 else
2969 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002970 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002971 {
2972 clear_tv(rettv);
2973 clear_tv(&var2);
2974 return FAIL;
2975 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002976 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002977 clear_tv(rettv);
2978 rettv->v_type = VAR_STRING;
2979 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002981 else if (op == '+' && rettv->v_type == VAR_BLOB
2982 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002983 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002984 else if (op == '+' && rettv->v_type == VAR_LIST
2985 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002986 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002987 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002988 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 else
2991 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002992 int error = FALSE;
2993 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002994#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002995 float_T f1 = 0, f2 = 0;
2996
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002997 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002998 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002999 f1 = rettv->vval.v_float;
3000 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003001 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003002 else
3003#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003004 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003005 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003006 if (error)
3007 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003008 // This can only happen for "list + non-list" or
3009 // "blob + non-blob". For "non-list + ..." or
3010 // "something - ...", we returned before evaluating the
3011 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003012 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003013 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003014 return FAIL;
3015 }
3016#ifdef FEAT_FLOAT
3017 if (var2.v_type == VAR_FLOAT)
3018 f1 = n1;
3019#endif
3020 }
3021#ifdef FEAT_FLOAT
3022 if (var2.v_type == VAR_FLOAT)
3023 {
3024 f2 = var2.vval.v_float;
3025 n2 = 0;
3026 }
3027 else
3028#endif
3029 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003030 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003031 if (error)
3032 {
3033 clear_tv(rettv);
3034 clear_tv(&var2);
3035 return FAIL;
3036 }
3037#ifdef FEAT_FLOAT
3038 if (rettv->v_type == VAR_FLOAT)
3039 f2 = n2;
3040#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003041 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003042 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003043
3044#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003045 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003046 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3047 {
3048 if (op == '+')
3049 f1 = f1 + f2;
3050 else
3051 f1 = f1 - f2;
3052 rettv->v_type = VAR_FLOAT;
3053 rettv->vval.v_float = f1;
3054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003056#endif
3057 {
3058 if (op == '+')
3059 n1 = n1 + n2;
3060 else
3061 n1 = n1 - n2;
3062 rettv->v_type = VAR_NUMBER;
3063 rettv->vval.v_number = n1;
3064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003066 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 }
3068 }
3069 return OK;
3070}
3071
3072/*
3073 * Handle fifth level expression:
3074 * * number multiplication
3075 * / number division
3076 * % number modulo
3077 *
3078 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003079 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 *
3081 * Return OK or FAIL.
3082 */
3083 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003084eval6(
3085 char_u **arg,
3086 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003087 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003088 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003090#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003091 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093
3094 /*
3095 * Get the first variable.
3096 */
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003097 if (eval7t(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 return FAIL;
3099
3100 /*
3101 * Repeat computing, until no '*', '/' or '%' is following.
3102 */
3103 for (;;)
3104 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003105 int evaluate;
3106 int getnext;
3107 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003108 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003109 int op;
3110 varnumber_T n1, n2;
3111#ifdef FEAT_FLOAT
3112 float_T f1, f2;
3113#endif
3114 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003115
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003116 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003117 p = eval_next_non_blank(*arg, evalarg, &getnext);
3118 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003119 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003121
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003122 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003123 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003124 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003125 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003126 {
3127 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3128 {
mityu4ac198c2021-05-28 17:52:40 +02003129 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003130 clear_tv(rettv);
3131 return FAIL;
3132 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003133 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003136#ifdef FEAT_FLOAT
3137 f1 = 0;
3138 f2 = 0;
3139#endif
3140 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003141 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003143#ifdef FEAT_FLOAT
3144 if (rettv->v_type == VAR_FLOAT)
3145 {
3146 f1 = rettv->vval.v_float;
3147 use_float = TRUE;
3148 n1 = 0;
3149 }
3150 else
3151#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003152 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003153 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003154 if (error)
3155 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 }
3157 else
3158 n1 = 0;
3159
3160 /*
3161 * Get the second variable.
3162 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003163 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3164 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003165 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003166 clear_tv(rettv);
3167 return FAIL;
3168 }
3169 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003170 if (eval7t(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 return FAIL;
3172
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003173 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003175#ifdef FEAT_FLOAT
3176 if (var2.v_type == VAR_FLOAT)
3177 {
3178 if (!use_float)
3179 {
3180 f1 = n1;
3181 use_float = TRUE;
3182 }
3183 f2 = var2.vval.v_float;
3184 n2 = 0;
3185 }
3186 else
3187#endif
3188 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003189 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003190 clear_tv(&var2);
3191 if (error)
3192 return FAIL;
3193#ifdef FEAT_FLOAT
3194 if (use_float)
3195 f2 = n2;
3196#endif
3197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198
3199 /*
3200 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003201 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003203#ifdef FEAT_FLOAT
3204 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003206 if (op == '*')
3207 f1 = f1 * f2;
3208 else if (op == '/')
3209 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003210# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003211 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003212 if (f2 == 0.0)
3213 {
3214 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003215 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003216 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003217 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003218 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003219 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003220 }
3221 else
3222 f1 = f1 / f2;
3223# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003224 // We rely on the floating point library to handle divide
3225 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003226 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003227# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003230 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003231 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003232 return FAIL;
3233 }
3234 rettv->v_type = VAR_FLOAT;
3235 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 }
3237 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003238#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003240 int failed = FALSE;
3241
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003242 if (op == '*')
3243 n1 = n1 * n2;
3244 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003245 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003247 n1 = num_modulus(n1, n2, &failed);
3248 if (failed)
3249 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003250
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003251 rettv->v_type = VAR_NUMBER;
3252 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 }
3255 }
3256
3257 return OK;
3258}
3259
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003260/*
3261 * Handle a type cast before a base level expression.
3262 * "arg" must point to the first non-white of the expression.
3263 * "arg" is advanced to just after the recognized expression.
3264 * Return OK or FAIL.
3265 */
3266 static int
3267eval7t(
3268 char_u **arg,
3269 typval_T *rettv,
3270 evalarg_T *evalarg,
3271 int want_string) // after "." operator
3272{
3273 type_T *want_type = NULL;
3274 garray_T type_list; // list of pointers to allocated types
3275 int res;
3276 int evaluate = evalarg == NULL ? 0
3277 : (evalarg->eval_flags & EVAL_EVALUATE);
3278
3279 // Recognize <type> in Vim9 script only.
3280 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1]))
3281 {
3282 ++*arg;
3283 ga_init2(&type_list, sizeof(type_T *), 10);
3284 want_type = parse_type(arg, &type_list, TRUE);
3285 if (want_type == NULL && (evaluate || **arg != '>'))
3286 {
3287 clear_type_list(&type_list);
3288 return FAIL;
3289 }
3290
3291 if (**arg != '>')
3292 {
3293 if (*skipwhite(*arg) == '>')
3294 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3295 else
3296 emsg(_(e_missing_gt));
3297 clear_type_list(&type_list);
3298 return FAIL;
3299 }
3300 ++*arg;
3301 *arg = skipwhite_and_linebreak(*arg, evalarg);
3302 }
3303
3304 res = eval7(arg, rettv, evalarg, want_string);
3305
3306 if (want_type != NULL && evaluate)
3307 {
3308 if (res == OK)
3309 {
3310 type_T *actual = typval2type(rettv, get_copyID(), &type_list, TRUE);
3311
3312 if (!equal_type(want_type, actual))
3313 {
3314 if (want_type == &t_bool && actual != &t_bool
3315 && (actual->tt_flags & TTFLAG_BOOL_OK))
3316 {
3317 int n = tv2bool(rettv);
3318
3319 // can use "0" and "1" for boolean in some places
3320 clear_tv(rettv);
3321 rettv->v_type = VAR_BOOL;
3322 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3323 }
3324 else
3325 {
3326 where_T where;
3327
3328 where.wt_index = 0;
3329 where.wt_variable = TRUE;
3330 res = check_type(want_type, actual, TRUE, where);
3331 }
3332 }
3333 }
3334 clear_type_list(&type_list);
3335 }
3336
3337 return res;
3338}
3339
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003340 int
3341eval_leader(char_u **arg, int vim9)
3342{
3343 char_u *s = *arg;
3344 char_u *p = *arg;
3345
3346 while (*p == '!' || *p == '-' || *p == '+')
3347 {
3348 char_u *n = skipwhite(p + 1);
3349
3350 // ++, --, -+ and +- are not accepted in Vim9 script
3351 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3352 {
3353 semsg(_(e_invexpr2), s);
3354 return FAIL;
3355 }
3356 p = n;
3357 }
3358 *arg = p;
3359 return OK;
3360}
3361
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362/*
3363 * Handle sixth level expression:
3364 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003365 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003366 * "string" string constant
3367 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 * &option-name option value
3369 * @r register contents
3370 * identifier variable value
3371 * function() function call
3372 * $VAR environment variable
3373 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003374 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003375 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003376 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003377 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 *
3379 * Also handle:
3380 * ! in front logical NOT
3381 * - in front unary minus
3382 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003383 * trailing [] subscript in String or List
3384 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003385 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 *
3387 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003388 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 *
3390 * Return OK or FAIL.
3391 */
3392 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003393eval7(
3394 char_u **arg,
3395 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003396 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003397 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003399 int evaluate = evalarg != NULL
3400 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 int len;
3402 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 char_u *start_leader, *end_leader;
3404 int ret = OK;
3405 char_u *alias;
3406
3407 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003408 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003409 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003411 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412
3413 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003414 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 */
3416 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003417 if (eval_leader(arg, in_vim9script()) == FAIL)
3418 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 end_leader = *arg;
3420
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003421 if (**arg == '.' && (!isdigit(*(*arg + 1))
3422#ifdef FEAT_FLOAT
3423 || current_sctx.sc_version < 2
3424#endif
3425 ))
3426 {
3427 semsg(_(e_invexpr2), *arg);
3428 ++*arg;
3429 return FAIL;
3430 }
3431
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 switch (**arg)
3433 {
3434 /*
3435 * Number constant.
3436 */
3437 case '0':
3438 case '1':
3439 case '2':
3440 case '3':
3441 case '4':
3442 case '5':
3443 case '6':
3444 case '7':
3445 case '8':
3446 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003447 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003448
3449 // Apply prefixed "-" and "+" now. Matters especially when
3450 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003451 if (ret == OK && evaluate && end_leader > start_leader
3452 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003453 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 break;
3455
3456 /*
3457 * String constant: "string".
3458 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003459 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 break;
3461
3462 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003463 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003465 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003466 break;
3467
3468 /*
3469 * List: [expr, expr]
3470 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003471 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 break;
3473
3474 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003475 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003476 */
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003477 case '#': if (in_vim9script())
3478 {
3479 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3480 }
3481 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003482 {
3483 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003484 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003485 }
3486 else
3487 ret = NOTDONE;
3488 break;
3489
3490 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003491 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003492 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003493 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003494 case '{': if (in_vim9script())
3495 ret = NOTDONE;
3496 else
3497 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003498 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003499 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003500 break;
3501
3502 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003503 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003505 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 break;
3507
3508 /*
3509 * Environment variable: $VAR.
3510 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003511 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 break;
3513
3514 /*
3515 * Register contents: @r.
3516 */
3517 case '@': ++*arg;
3518 if (evaluate)
3519 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003520 if (in_vim9script() && IS_WHITE_OR_NUL(**arg))
3521 semsg(_(e_syntax_error_at_str), *arg);
3522 else if (in_vim9script() && !valid_yank_reg(**arg, FALSE))
3523 emsg_invreg(**arg);
3524 else
3525 {
3526 rettv->v_type = VAR_STRING;
3527 rettv->vval.v_string = get_reg_contents(**arg,
3528 GREG_EXPR_SRC);
3529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 }
3531 if (**arg != NUL)
3532 ++*arg;
3533 break;
3534
3535 /*
3536 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003537 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003539 case '(': ret = NOTDONE;
3540 if (in_vim9script())
Bram Moolenaar06409502021-02-17 17:00:27 +01003541 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003542 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003543 if (ret == OK && evaluate)
3544 {
3545 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3546
Bram Moolenaara9931532021-06-12 15:58:16 +02003547 // Compile it here to get the return type. The return
3548 // type is optional, when it's missing use t_unknown.
3549 // This is recognized in compile_return().
3550 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3551 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003552 if (compile_def_function(ufunc,
Bram Moolenaare99d4222021-06-13 14:01:26 +02003553 FALSE, COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003554 {
3555 clear_tv(rettv);
3556 ret = FAIL;
3557 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003558 }
3559 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003560 if (ret == NOTDONE)
3561 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003562 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003563 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003564
Bram Moolenaar9215f012020-06-27 21:18:00 +02003565 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003566 if (**arg == ')')
3567 ++*arg;
3568 else if (ret == OK)
3569 {
3570 emsg(_(e_missing_close));
3571 clear_tv(rettv);
3572 ret = FAIL;
3573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 }
3575 break;
3576
Bram Moolenaar8c711452005-01-14 21:53:12 +00003577 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 break;
3579 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003580
3581 if (ret == NOTDONE)
3582 {
3583 /*
3584 * Must be a variable or function name.
3585 * Can also be a curly-braces kind of name: {expr}.
3586 */
3587 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003588 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003589 if (alias != NULL)
3590 s = alias;
3591
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003592 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003593 ret = FAIL;
3594 else
3595 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003596 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3597
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003598 if (evaluate && in_vim9script() && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003599 {
3600 emsg(_(e_cannot_use_underscore_here));
3601 ret = FAIL;
3602 }
3603 else if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003604 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003605 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003606 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003607 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003608 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003609 else if (flags & EVAL_CONSTANT)
3610 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003611 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003612 {
3613 // get the value of "true", "false" or a variable
3614 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3615 {
3616 rettv->v_type = VAR_BOOL;
3617 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003618 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003619 }
3620 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003621 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003622 {
3623 rettv->v_type = VAR_BOOL;
3624 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003625 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003626 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003627 else if (len == 4 && in_vim9script()
3628 && STRNCMP(s, "null", 4) == 0)
3629 {
3630 rettv->v_type = VAR_SPECIAL;
3631 rettv->vval.v_number = VVAL_NULL;
3632 ret = OK;
3633 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003634 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003635 ret = eval_variable(s, len, rettv, NULL,
3636 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003637 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003638 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003639 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003640 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003641 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003642 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003643 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003644 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003645 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003646 }
3647
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003648 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3649 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003650 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003651 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652
3653 /*
3654 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3655 */
3656 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003657 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003658 return ret;
3659}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003660
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003661/*
3662 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003663 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003664 * Adjusts "end_leaderp" until it is at "start_leader".
3665 */
3666 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003667eval7_leader(
3668 typval_T *rettv,
3669 int numeric_only,
3670 char_u *start_leader,
3671 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003672{
3673 char_u *end_leader = *end_leaderp;
3674 int ret = OK;
3675 int error = FALSE;
3676 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003677 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003678#ifdef FEAT_FLOAT
3679 float_T f = 0.0;
3680
3681 if (rettv->v_type == VAR_FLOAT)
3682 f = rettv->vval.v_float;
3683 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003684#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003685 {
3686 while (VIM_ISWHITE(end_leader[-1]))
3687 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003688 if (in_vim9script() && end_leader[-1] == '!')
3689 val = tv2bool(rettv);
3690 else
3691 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003692 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003693 if (error)
3694 {
3695 clear_tv(rettv);
3696 ret = FAIL;
3697 }
3698 else
3699 {
3700 while (end_leader > start_leader)
3701 {
3702 --end_leader;
3703 if (*end_leader == '!')
3704 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003705 if (numeric_only)
3706 {
3707 ++end_leader;
3708 break;
3709 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003710#ifdef FEAT_FLOAT
3711 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003712 {
3713 if (in_vim9script())
3714 {
3715 rettv->v_type = VAR_BOOL;
3716 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3717 }
3718 else
3719 f = !f;
3720 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003721 else
3722#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003723 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003724 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003725 type = VAR_BOOL;
3726 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003727 }
3728 else if (*end_leader == '-')
3729 {
3730#ifdef FEAT_FLOAT
3731 if (rettv->v_type == VAR_FLOAT)
3732 f = -f;
3733 else
3734#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003735 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003736 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003737 type = VAR_NUMBER;
3738 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003739 }
3740 }
3741#ifdef FEAT_FLOAT
3742 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003744 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003745 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003747 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003748#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003749 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003750 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003751 if (in_vim9script())
3752 rettv->v_type = type;
3753 else
3754 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003755 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003758 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 return ret;
3760}
3761
3762/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003763 * Call the function referred to in "rettv".
3764 */
3765 static int
3766call_func_rettv(
3767 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003768 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003769 typval_T *rettv,
3770 int evaluate,
3771 dict_T *selfdict,
3772 typval_T *basetv)
3773{
3774 partial_T *pt = NULL;
3775 funcexe_T funcexe;
3776 typval_T functv;
3777 char_u *s;
3778 int ret;
3779
3780 // need to copy the funcref so that we can clear rettv
3781 if (evaluate)
3782 {
3783 functv = *rettv;
3784 rettv->v_type = VAR_UNKNOWN;
3785
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003786 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003787 if (functv.v_type == VAR_PARTIAL)
3788 {
3789 pt = functv.vval.v_partial;
3790 s = partial_name(pt);
3791 }
3792 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003793 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003794 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003795 if (s == NULL || *s == NUL)
3796 {
3797 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02003798 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003799 goto theend;
3800 }
3801 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003802 }
3803 else
3804 s = (char_u *)"";
3805
Bram Moolenaara80faa82020-04-12 19:37:17 +02003806 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003807 funcexe.firstline = curwin->w_cursor.lnum;
3808 funcexe.lastline = curwin->w_cursor.lnum;
3809 funcexe.evaluate = evaluate;
3810 funcexe.partial = pt;
3811 funcexe.selfdict = selfdict;
3812 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003813 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003814
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003815theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003816 // Clear the funcref afterwards, so that deleting it while
3817 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003818 if (evaluate)
3819 clear_tv(&functv);
3820
3821 return ret;
3822}
3823
3824/*
3825 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003826 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003827 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3828 */
3829 static int
3830eval_lambda(
3831 char_u **arg,
3832 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003833 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003834 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003835{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003836 int evaluate = evalarg != NULL
3837 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003838 typval_T base = *rettv;
3839 int ret;
3840
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003841 rettv->v_type = VAR_UNKNOWN;
3842
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003843 if (**arg == '{')
3844 {
3845 // ->{lambda}()
3846 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3847 }
3848 else
3849 {
3850 // ->(lambda)()
3851 ++*arg;
3852 ret = eval1(arg, rettv, evalarg);
3853 *arg = skipwhite_and_linebreak(*arg, evalarg);
3854 if (**arg != ')')
3855 {
3856 emsg(_(e_missing_close));
3857 ret = FAIL;
3858 }
3859 ++*arg;
3860 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003861 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003862 return FAIL;
3863 else if (**arg != '(')
3864 {
3865 if (verbose)
3866 {
3867 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003868 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003869 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003870 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003871 }
3872 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003873 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003874 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003875 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003876 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003877
3878 // Clear the funcref afterwards, so that deleting it while
3879 // evaluating the arguments is possible (see test55).
3880 if (evaluate)
3881 clear_tv(&base);
3882
3883 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003884}
3885
3886/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003887 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003888 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02003889 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3890 */
3891 static int
3892eval_method(
3893 char_u **arg,
3894 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003895 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003896 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003897{
3898 char_u *name;
3899 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003900 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003901 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003902 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003903 int evaluate = evalarg != NULL
3904 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003905
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003906 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003907
Bram Moolenaarac92e252019-08-03 21:58:38 +02003908 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003909 len = get_name_len(arg, &alias, evaluate, TRUE);
3910 if (alias != NULL)
3911 name = alias;
3912
3913 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003914 {
3915 if (verbose)
3916 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003917 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003918 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003919 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003920 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003921 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003922 if (**arg != '(')
3923 {
3924 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003925 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003926 ret = FAIL;
3927 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003928 else if (VIM_ISWHITE((*arg)[-1]))
3929 {
3930 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003931 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003932 ret = FAIL;
3933 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003934 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003935 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003936 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003937 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003938
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003939 // Clear the funcref afterwards, so that deleting it while
3940 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003941 if (evaluate)
3942 clear_tv(&base);
3943
Bram Moolenaarac92e252019-08-03 21:58:38 +02003944 return ret;
3945}
3946
3947/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003948 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3949 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003950 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3951 */
3952 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003953eval_index(
3954 char_u **arg,
3955 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003956 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003957 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003958{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003959 int evaluate = evalarg != NULL
3960 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003961 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003962 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003963 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003964 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003965 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003966 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003967
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003968 if (check_can_index(rettv, evaluate, verbose) == FAIL)
3969 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003970
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003971 init_tv(&var1);
3972 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003973 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003974 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003975 /*
3976 * dict.name
3977 */
3978 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003979 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003980 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003981 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003982 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02003983 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003984 }
3985 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003986 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003987 /*
3988 * something[idx]
3989 *
3990 * Get the (first) variable from inside the [].
3991 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003992 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003993 if (**arg == ':')
3994 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003995 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003996 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003997 else if (vim9 && **arg == ':')
3998 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003999 semsg(_(e_white_space_required_before_and_after_str_at_str),
4000 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004001 clear_tv(&var1);
4002 return FAIL;
4003 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004004 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004005 {
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004006#ifdef FEAT_FLOAT
4007 // allow for indexing with float
4008 if (vim9 && rettv->v_type == VAR_DICT
4009 && var1.v_type == VAR_FLOAT)
4010 {
4011 var1.vval.v_string = typval_tostring(&var1, TRUE);
4012 var1.v_type = VAR_STRING;
4013 }
4014#endif
4015 if (tv_get_string_chk(&var1) == NULL)
4016 {
4017 // not a number or string
4018 clear_tv(&var1);
4019 return FAIL;
4020 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004021 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004022
4023 /*
4024 * Get the second variable from inside the [:].
4025 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004026 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004027 if (**arg == ':')
4028 {
4029 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004030 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01004031 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004032 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004033 semsg(_(e_white_space_required_before_and_after_str_at_str),
4034 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004035 if (!empty1)
4036 clear_tv(&var1);
4037 return FAIL;
4038 }
4039 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004040 if (**arg == ']')
4041 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004042 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004043 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004044 if (!empty1)
4045 clear_tv(&var1);
4046 return FAIL;
4047 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004048 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004049 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004050 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004051 if (!empty1)
4052 clear_tv(&var1);
4053 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004054 return FAIL;
4055 }
4056 }
4057
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004058 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004059 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004060 if (**arg != ']')
4061 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004062 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004063 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004064 clear_tv(&var1);
4065 if (range)
4066 clear_tv(&var2);
4067 return FAIL;
4068 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004069 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004070 }
4071
4072 if (evaluate)
4073 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004074 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004075 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004076 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004077
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004078 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004079 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004080 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004081 clear_tv(&var2);
4082 return res;
4083 }
4084 return OK;
4085}
4086
4087/*
4088 * Check if "rettv" can have an [index] or [sli:ce]
4089 */
4090 int
4091check_can_index(typval_T *rettv, int evaluate, int verbose)
4092{
4093 switch (rettv->v_type)
4094 {
4095 case VAR_FUNC:
4096 case VAR_PARTIAL:
4097 if (verbose)
4098 emsg(_("E695: Cannot index a Funcref"));
4099 return FAIL;
4100 case VAR_FLOAT:
4101#ifdef FEAT_FLOAT
4102 if (verbose)
4103 emsg(_(e_float_as_string));
4104 return FAIL;
4105#endif
4106 case VAR_BOOL:
4107 case VAR_SPECIAL:
4108 case VAR_JOB:
4109 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004110 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004111 if (verbose)
4112 emsg(_(e_cannot_index_special_variable));
4113 return FAIL;
4114 case VAR_UNKNOWN:
4115 case VAR_ANY:
4116 case VAR_VOID:
4117 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004118 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004119 emsg(_(e_cannot_index_special_variable));
4120 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004121 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004122 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004123
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004124 case VAR_STRING:
4125 case VAR_LIST:
4126 case VAR_DICT:
4127 case VAR_BLOB:
4128 break;
4129 case VAR_NUMBER:
4130 if (in_vim9script())
4131 emsg(_(e_cannot_index_number));
4132 break;
4133 }
4134 return OK;
4135}
4136
4137/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004138 * slice() function
4139 */
4140 void
4141f_slice(typval_T *argvars, typval_T *rettv)
4142{
4143 if (check_can_index(argvars, TRUE, FALSE) == OK)
4144 {
4145 copy_tv(argvars, rettv);
4146 eval_index_inner(rettv, TRUE, argvars + 1,
4147 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4148 TRUE, NULL, 0, FALSE);
4149 }
4150}
4151
4152/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004153 * Apply index or range to "rettv".
4154 * "var1" is the first index, NULL for [:expr].
4155 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004156 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4157 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004158 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4159 */
4160 int
4161eval_index_inner(
4162 typval_T *rettv,
4163 int is_range,
4164 typval_T *var1,
4165 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004166 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004167 char_u *key,
4168 int keylen,
4169 int verbose)
4170{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004171 varnumber_T n1, n2 = 0;
4172 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004173
4174 n1 = 0;
4175 if (var1 != NULL && rettv->v_type != VAR_DICT)
4176 n1 = tv_get_number(var1);
4177
4178 if (is_range)
4179 {
4180 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004181 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004182 if (verbose)
4183 emsg(_(e_cannot_slice_dictionary));
4184 return FAIL;
4185 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004186 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004187 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004188 else
4189 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004190 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004191
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004192 switch (rettv->v_type)
4193 {
4194 case VAR_UNKNOWN:
4195 case VAR_ANY:
4196 case VAR_VOID:
4197 case VAR_FUNC:
4198 case VAR_PARTIAL:
4199 case VAR_FLOAT:
4200 case VAR_BOOL:
4201 case VAR_SPECIAL:
4202 case VAR_JOB:
4203 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004204 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004205 break; // not evaluating, skipping over subscript
4206
4207 case VAR_NUMBER:
4208 case VAR_STRING:
4209 {
4210 char_u *s = tv_get_string(rettv);
4211
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004212 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004213 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004214 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004215 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004216 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004217 else
4218 s = char_from_string(s, n1);
4219 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004220 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004221 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004222 // The resulting variable is a substring. If the indexes
4223 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004224 if (n1 < 0)
4225 {
4226 n1 = len + n1;
4227 if (n1 < 0)
4228 n1 = 0;
4229 }
4230 if (n2 < 0)
4231 n2 = len + n2;
4232 else if (n2 >= len)
4233 n2 = len;
4234 if (n1 >= len || n2 < 0 || n1 > n2)
4235 s = NULL;
4236 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004237 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004238 }
4239 else
4240 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004241 // The resulting variable is a string of a single
4242 // character. If the index is too big or negative the
4243 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004244 if (n1 >= len || n1 < 0)
4245 s = NULL;
4246 else
4247 s = vim_strnsave(s + n1, 1);
4248 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004249 clear_tv(rettv);
4250 rettv->v_type = VAR_STRING;
4251 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004252 }
4253 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004254
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004255 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004256 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4257 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004258 break;
4259
4260 case VAR_LIST:
4261 if (var1 == NULL)
4262 n1 = 0;
4263 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004264 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004265 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004266 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004267 return FAIL;
4268 break;
4269
4270 case VAR_DICT:
4271 {
4272 dictitem_T *item;
4273 typval_T tmp;
4274
4275 if (key == NULL)
4276 {
4277 key = tv_get_string_chk(var1);
4278 if (key == NULL)
4279 return FAIL;
4280 }
4281
4282 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
4283
4284 if (item == NULL && verbose)
4285 semsg(_(e_dictkey), key);
4286 if (item == NULL)
4287 return FAIL;
4288
4289 copy_tv(&item->di_tv, &tmp);
4290 clear_tv(rettv);
4291 *rettv = tmp;
4292 }
4293 break;
4294 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004295 return OK;
4296}
4297
4298/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004299 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004300 */
4301 char_u *
4302partial_name(partial_T *pt)
4303{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004304 if (pt != NULL)
4305 {
4306 if (pt->pt_name != NULL)
4307 return pt->pt_name;
4308 if (pt->pt_func != NULL)
4309 return pt->pt_func->uf_name;
4310 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004311 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004312}
4313
Bram Moolenaarddecc252016-04-06 22:59:37 +02004314 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004315partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004316{
4317 int i;
4318
4319 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004320 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004321 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004322 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004323 if (pt->pt_name != NULL)
4324 {
4325 func_unref(pt->pt_name);
4326 vim_free(pt->pt_name);
4327 }
4328 else
4329 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004330
Bram Moolenaar54656012021-06-09 20:50:46 +02004331 // "out_up" is no longer used, decrement refcount on partial that owns it.
4332 partial_unref(pt->pt_outer.out_up_partial);
4333
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004334 // Decrease the reference count for the context of a closure. If down
4335 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004336 if (pt->pt_funcstack != NULL)
4337 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004338 --pt->pt_funcstack->fs_refcount;
4339 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004340 }
4341
Bram Moolenaarddecc252016-04-06 22:59:37 +02004342 vim_free(pt);
4343}
4344
4345/*
4346 * Unreference a closure: decrement the reference count and free it when it
4347 * becomes zero.
4348 */
4349 void
4350partial_unref(partial_T *pt)
4351{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004352 if (pt != NULL)
4353 {
4354 if (--pt->pt_refcount <= 0)
4355 partial_free(pt);
4356
4357 // If the reference count goes down to one, the funcstack may be the
4358 // only reference and can be freed if no other partials reference it.
4359 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4360 funcstack_check_refcount(pt->pt_funcstack);
4361 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004362}
4363
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004364/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004365 * Return the next (unique) copy ID.
4366 * Used for serializing nested structures.
4367 */
4368 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004369get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004370{
4371 current_copyID += COPYID_INC;
4372 return current_copyID;
4373}
4374
4375/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004376 * Garbage collection for lists and dictionaries.
4377 *
4378 * We use reference counts to be able to free most items right away when they
4379 * are no longer used. But for composite items it's possible that it becomes
4380 * unused while the reference count is > 0: When there is a recursive
4381 * reference. Example:
4382 * :let l = [1, 2, 3]
4383 * :let d = {9: l}
4384 * :let l[1] = d
4385 *
4386 * Since this is quite unusual we handle this with garbage collection: every
4387 * once in a while find out which lists and dicts are not referenced from any
4388 * variable.
4389 *
4390 * Here is a good reference text about garbage collection (refers to Python
4391 * but it applies to all reference-counting mechanisms):
4392 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004393 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004394
4395/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004396 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004397 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004398 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004399 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004400 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004401garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004402{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004403 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004404 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004405 buf_T *buf;
4406 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004407 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004408 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004409
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004410 if (!testing)
4411 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004412 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004413 want_garbage_collect = FALSE;
4414 may_garbage_collect = FALSE;
4415 garbage_collect_at_exit = FALSE;
4416 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004417
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004418 // The execution stack can grow big, limit the size.
4419 if (exestack.ga_maxlen - exestack.ga_len > 500)
4420 {
4421 size_t new_len;
4422 char_u *pp;
4423 int n;
4424
4425 // Keep 150% of the current size, with a minimum of the growth size.
4426 n = exestack.ga_len / 2;
4427 if (n < exestack.ga_growsize)
4428 n = exestack.ga_growsize;
4429
4430 // Don't make it bigger though.
4431 if (exestack.ga_len + n < exestack.ga_maxlen)
4432 {
4433 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4434 pp = vim_realloc(exestack.ga_data, new_len);
4435 if (pp == NULL)
4436 return FAIL;
4437 exestack.ga_maxlen = exestack.ga_len + n;
4438 exestack.ga_data = pp;
4439 }
4440 }
4441
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004442 // We advance by two because we add one for items referenced through
4443 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004444 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004445
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004446 /*
4447 * 1. Go through all accessible variables and mark all lists and dicts
4448 * with copyID.
4449 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004450
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004451 // Don't free variables in the previous_funccal list unless they are only
4452 // referenced through previous_funccal. This must be first, because if
4453 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004454 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004455
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004456 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004457 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004458
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004459 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004460 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004461 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4462 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004463
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004464 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004465 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004466 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4467 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004468 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004469 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4470 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004471#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004472 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004473 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4474 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004475 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004476 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004477 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4478 NULL, NULL);
4479#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004480
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004481 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004482 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004483 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4484 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004485 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004486 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004487
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004488 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004489 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004490
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004491 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004492 abort = abort || set_ref_in_functions(copyID);
4493
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004494 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004495 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004496
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004497 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004498 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004499
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004500 // callbacks in buffers
4501 abort = abort || set_ref_in_buffers(copyID);
4502
Bram Moolenaar1dced572012-04-05 16:54:08 +02004503#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004504 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004505#endif
4506
Bram Moolenaardb913952012-06-29 12:54:53 +02004507#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004508 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004509#endif
4510
4511#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004512 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004513#endif
4514
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004515#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004516 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004517 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004518#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004519#ifdef FEAT_NETBEANS_INTG
4520 abort = abort || set_ref_in_nb_channel(copyID);
4521#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004522
Bram Moolenaare3188e22016-05-31 21:13:04 +02004523#ifdef FEAT_TIMERS
4524 abort = abort || set_ref_in_timer(copyID);
4525#endif
4526
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004527#ifdef FEAT_QUICKFIX
4528 abort = abort || set_ref_in_quickfix(copyID);
4529#endif
4530
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004531#ifdef FEAT_TERMINAL
4532 abort = abort || set_ref_in_term(copyID);
4533#endif
4534
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004535#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004536 abort = abort || set_ref_in_popups(copyID);
4537#endif
4538
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004539 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004540 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004541 /*
4542 * 2. Free lists and dictionaries that are not referenced.
4543 */
4544 did_free = free_unref_items(copyID);
4545
4546 /*
4547 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004548 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004549 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004550 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004551 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004552 else if (p_verbose > 0)
4553 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004554 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004555 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004556
4557 return did_free;
4558}
4559
4560/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004561 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004562 */
4563 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004564free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004565{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004566 int did_free = FALSE;
4567
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004568 // Let all "free" functions know that we are here. This means no
4569 // dictionaries, lists, channels or jobs are to be freed, because we will
4570 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004571 in_free_unref_items = TRUE;
4572
4573 /*
4574 * PASS 1: free the contents of the items. We don't free the items
4575 * themselves yet, so that it is possible to decrement refcount counters
4576 */
4577
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004578 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004579 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004580
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004581 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004582 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004583
4584#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004585 // Go through the list of jobs and free items without the copyID. This
4586 // must happen before doing channels, because jobs refer to channels, but
4587 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004588 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4589
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004590 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004591 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4592#endif
4593
4594 /*
4595 * PASS 2: free the items themselves.
4596 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004597 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004598 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004599
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004600#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004601 // Go through the list of jobs and free items without the copyID. This
4602 // must happen before doing channels, because jobs refer to channels, but
4603 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004604 free_unused_jobs(copyID, COPYID_MASK);
4605
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004606 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004607 free_unused_channels(copyID, COPYID_MASK);
4608#endif
4609
4610 in_free_unref_items = FALSE;
4611
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004612 return did_free;
4613}
4614
4615/*
4616 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004617 * "list_stack" is used to add lists to be marked. Can be NULL.
4618 *
4619 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004620 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004621 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004622set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004623{
4624 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004625 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004626 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004627 hashtab_T *cur_ht;
4628 ht_stack_T *ht_stack = NULL;
4629 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004630
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004631 cur_ht = ht;
4632 for (;;)
4633 {
4634 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004635 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004636 // Mark each item in the hashtab. If the item contains a hashtab
4637 // it is added to ht_stack, if it contains a list it is added to
4638 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004639 todo = (int)cur_ht->ht_used;
4640 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4641 if (!HASHITEM_EMPTY(hi))
4642 {
4643 --todo;
4644 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4645 &ht_stack, list_stack);
4646 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004647 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004648
4649 if (ht_stack == NULL)
4650 break;
4651
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004652 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004653 cur_ht = ht_stack->ht;
4654 tempitem = ht_stack;
4655 ht_stack = ht_stack->prev;
4656 free(tempitem);
4657 }
4658
4659 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004660}
4661
4662/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004663 * Mark a dict and its items with "copyID".
4664 * Returns TRUE if setting references failed somehow.
4665 */
4666 int
4667set_ref_in_dict(dict_T *d, int copyID)
4668{
4669 if (d != NULL && d->dv_copyID != copyID)
4670 {
4671 d->dv_copyID = copyID;
4672 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4673 }
4674 return FALSE;
4675}
4676
4677/*
4678 * Mark a list and its items with "copyID".
4679 * Returns TRUE if setting references failed somehow.
4680 */
4681 int
4682set_ref_in_list(list_T *ll, int copyID)
4683{
4684 if (ll != NULL && ll->lv_copyID != copyID)
4685 {
4686 ll->lv_copyID = copyID;
4687 return set_ref_in_list_items(ll, copyID, NULL);
4688 }
4689 return FALSE;
4690}
4691
4692/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004693 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004694 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4695 *
4696 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004697 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004698 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004699set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004700{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004701 listitem_T *li;
4702 int abort = FALSE;
4703 list_T *cur_l;
4704 list_stack_T *list_stack = NULL;
4705 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004706
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004707 cur_l = l;
4708 for (;;)
4709 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004710 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004711 // Mark each item in the list. If the item contains a hashtab
4712 // it is added to ht_stack, if it contains a list it is added to
4713 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004714 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4715 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4716 ht_stack, &list_stack);
4717 if (list_stack == NULL)
4718 break;
4719
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004720 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004721 cur_l = list_stack->list;
4722 tempitem = list_stack;
4723 list_stack = list_stack->prev;
4724 free(tempitem);
4725 }
4726
4727 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004728}
4729
4730/*
4731 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004732 * "list_stack" is used to add lists to be marked. Can be NULL.
4733 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4734 *
4735 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004736 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004737 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004738set_ref_in_item(
4739 typval_T *tv,
4740 int copyID,
4741 ht_stack_T **ht_stack,
4742 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004743{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004744 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004745
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004746 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004747 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004748 dict_T *dd = tv->vval.v_dict;
4749
Bram Moolenaara03f2332016-02-06 18:09:59 +01004750 if (dd != NULL && dd->dv_copyID != copyID)
4751 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004752 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004753 dd->dv_copyID = copyID;
4754 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004755 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004756 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4757 }
4758 else
4759 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004760 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4761
Bram Moolenaara03f2332016-02-06 18:09:59 +01004762 if (newitem == NULL)
4763 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004764 else
4765 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004766 newitem->ht = &dd->dv_hashtab;
4767 newitem->prev = *ht_stack;
4768 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004769 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004770 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004771 }
4772 }
4773 else if (tv->v_type == VAR_LIST)
4774 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004775 list_T *ll = tv->vval.v_list;
4776
Bram Moolenaara03f2332016-02-06 18:09:59 +01004777 if (ll != NULL && ll->lv_copyID != copyID)
4778 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004779 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004780 ll->lv_copyID = copyID;
4781 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004782 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004783 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004784 }
4785 else
4786 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004787 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4788
Bram Moolenaara03f2332016-02-06 18:09:59 +01004789 if (newitem == NULL)
4790 abort = TRUE;
4791 else
4792 {
4793 newitem->list = ll;
4794 newitem->prev = *list_stack;
4795 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004796 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004797 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004798 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004799 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004800 else if (tv->v_type == VAR_FUNC)
4801 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004802 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004803 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004804 else if (tv->v_type == VAR_PARTIAL)
4805 {
4806 partial_T *pt = tv->vval.v_partial;
4807 int i;
4808
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004809 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004810 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004811 // Didn't see this partial yet.
4812 pt->pt_copyID = copyID;
4813
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004814 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004815
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004816 if (pt->pt_dict != NULL)
4817 {
4818 typval_T dtv;
4819
4820 dtv.v_type = VAR_DICT;
4821 dtv.vval.v_dict = pt->pt_dict;
4822 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4823 }
4824
4825 for (i = 0; i < pt->pt_argc; ++i)
4826 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4827 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004828 if (pt->pt_funcstack != NULL)
4829 {
4830 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4831
4832 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4833 abort = abort || set_ref_in_item(stack + i, copyID,
4834 ht_stack, list_stack);
4835 }
4836
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004837 }
4838 }
4839#ifdef FEAT_JOB_CHANNEL
4840 else if (tv->v_type == VAR_JOB)
4841 {
4842 job_T *job = tv->vval.v_job;
4843 typval_T dtv;
4844
4845 if (job != NULL && job->jv_copyID != copyID)
4846 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004847 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004848 if (job->jv_channel != NULL)
4849 {
4850 dtv.v_type = VAR_CHANNEL;
4851 dtv.vval.v_channel = job->jv_channel;
4852 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4853 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004854 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004855 {
4856 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004857 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004858 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4859 }
4860 }
4861 }
4862 else if (tv->v_type == VAR_CHANNEL)
4863 {
4864 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004865 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004866 typval_T dtv;
4867 jsonq_T *jq;
4868 cbq_T *cq;
4869
4870 if (ch != NULL && ch->ch_copyID != copyID)
4871 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004872 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004873 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004874 {
4875 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4876 jq = jq->jq_next)
4877 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4878 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4879 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004880 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004881 {
4882 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004883 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004884 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4885 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004886 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004887 {
4888 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004889 dtv.vval.v_partial =
4890 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004891 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4892 }
4893 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004894 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004895 {
4896 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004897 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004898 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4899 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004900 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004901 {
4902 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004903 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004904 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4905 }
4906 }
4907 }
4908#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004909 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004910}
4911
Bram Moolenaar8c711452005-01-14 21:53:12 +00004912/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004913 * Return a string with the string representation of a variable.
4914 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004915 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004916 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004917 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004918 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02004919 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004920 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4921 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004922 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004923 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004924 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004925echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004926 typval_T *tv,
4927 char_u **tofree,
4928 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004929 int copyID,
4930 int echo_style,
4931 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004932 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004933{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004934 static int recurse = 0;
4935 char_u *r = NULL;
4936
Bram Moolenaar33570922005-01-25 22:26:29 +00004937 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004938 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004939 if (!did_echo_string_emsg)
4940 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004941 // Only give this message once for a recursive call to avoid
4942 // flooding the user with errors. And stop iterating over lists
4943 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004944 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004945 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004946 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004947 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004948 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004949 }
4950 ++recurse;
4951
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004952 switch (tv->v_type)
4953 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004954 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004955 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004956 {
4957 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004958 r = tv->vval.v_string;
4959 if (r == NULL)
4960 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004961 }
4962 else
4963 {
4964 *tofree = string_quote(tv->vval.v_string, FALSE);
4965 r = *tofree;
4966 }
4967 break;
4968
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004969 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004970 if (echo_style)
4971 {
4972 *tofree = NULL;
4973 r = tv->vval.v_string;
4974 }
4975 else
4976 {
4977 *tofree = string_quote(tv->vval.v_string, TRUE);
4978 r = *tofree;
4979 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004980 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004981
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004982 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004983 {
4984 partial_T *pt = tv->vval.v_partial;
4985 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004986 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004987 garray_T ga;
4988 int i;
4989 char_u *tf;
4990
4991 ga_init2(&ga, 1, 100);
4992 ga_concat(&ga, (char_u *)"function(");
4993 if (fname != NULL)
4994 {
4995 ga_concat(&ga, fname);
4996 vim_free(fname);
4997 }
4998 if (pt != NULL && pt->pt_argc > 0)
4999 {
5000 ga_concat(&ga, (char_u *)", [");
5001 for (i = 0; i < pt->pt_argc; ++i)
5002 {
5003 if (i > 0)
5004 ga_concat(&ga, (char_u *)", ");
5005 ga_concat(&ga,
5006 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5007 vim_free(tf);
5008 }
5009 ga_concat(&ga, (char_u *)"]");
5010 }
5011 if (pt != NULL && pt->pt_dict != NULL)
5012 {
5013 typval_T dtv;
5014
5015 ga_concat(&ga, (char_u *)", ");
5016 dtv.v_type = VAR_DICT;
5017 dtv.vval.v_dict = pt->pt_dict;
5018 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5019 vim_free(tf);
5020 }
5021 ga_concat(&ga, (char_u *)")");
5022
5023 *tofree = ga.ga_data;
5024 r = *tofree;
5025 break;
5026 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005027
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005028 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005029 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005030 break;
5031
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005032 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005033 if (tv->vval.v_list == NULL)
5034 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005035 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005036 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005037 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005038 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005039 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5040 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005041 {
5042 *tofree = NULL;
5043 r = (char_u *)"[...]";
5044 }
5045 else
5046 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005047 int old_copyID = tv->vval.v_list->lv_copyID;
5048
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005049 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005050 *tofree = list2string(tv, copyID, restore_copyID);
5051 if (restore_copyID)
5052 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005053 r = *tofree;
5054 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005055 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005056
Bram Moolenaar8c711452005-01-14 21:53:12 +00005057 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005058 if (tv->vval.v_dict == NULL)
5059 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005060 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005061 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005062 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005063 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005064 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5065 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005066 {
5067 *tofree = NULL;
5068 r = (char_u *)"{...}";
5069 }
5070 else
5071 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005072 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005073
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005074 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005075 *tofree = dict2string(tv, copyID, restore_copyID);
5076 if (restore_copyID)
5077 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005078 r = *tofree;
5079 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005080 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005081
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005082 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005083 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005084 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005085 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005086 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005087 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005088 break;
5089
Bram Moolenaar835dc632016-02-07 14:27:38 +01005090 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005091 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005092#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005093 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005094 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5095 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005096 if (composite_val)
5097 {
5098 *tofree = string_quote(r, FALSE);
5099 r = *tofree;
5100 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005101#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005102 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005103
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005104 case VAR_INSTR:
5105 *tofree = NULL;
5106 r = (char_u *)"instructions";
5107 break;
5108
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005109 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005110#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005111 *tofree = NULL;
5112 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5113 r = numbuf;
5114 break;
5115#endif
5116
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005117 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005118 case VAR_SPECIAL:
5119 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005120 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005121 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005122 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005123
Bram Moolenaar8502c702014-06-17 12:51:16 +02005124 if (--recurse == 0)
5125 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005126 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005127}
5128
5129/*
5130 * Return a string with the string representation of a variable.
5131 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5132 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005133 * Does not put quotes around strings, as ":echo" displays values.
5134 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5135 * May return NULL.
5136 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005137 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005138echo_string(
5139 typval_T *tv,
5140 char_u **tofree,
5141 char_u *numbuf,
5142 int copyID)
5143{
5144 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5145}
5146
5147/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005148 * Return string "str" in ' quotes, doubling ' characters.
5149 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005150 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005151 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005152 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005153string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005154{
Bram Moolenaar33570922005-01-25 22:26:29 +00005155 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005156 char_u *p, *r, *s;
5157
Bram Moolenaar33570922005-01-25 22:26:29 +00005158 len = (function ? 13 : 3);
5159 if (str != NULL)
5160 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005161 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005162 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00005163 if (*p == '\'')
5164 ++len;
5165 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005166 s = r = alloc(len);
5167 if (r != NULL)
5168 {
5169 if (function)
5170 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005171 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005172 r += 10;
5173 }
5174 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005175 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005176 if (str != NULL)
5177 for (p = str; *p != NUL; )
5178 {
5179 if (*p == '\'')
5180 *r++ = '\'';
5181 MB_COPY_CHAR(p, r);
5182 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005183 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005184 if (function)
5185 *r++ = ')';
5186 *r++ = NUL;
5187 }
5188 return s;
5189}
5190
5191/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005192 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5193 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005194 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005195 */
5196 int
5197buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5198{
5199 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005200 char_u *t;
5201 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005202
5203 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5204 return -1;
5205
5206 if (lnum > buf->b_ml.ml_line_count)
5207 lnum = buf->b_ml.ml_line_count;
5208
5209 str = ml_get_buf(buf, lnum, FALSE);
5210 if (str == NULL)
5211 return -1;
5212
5213 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005214 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005215
Bram Moolenaar91458462021-01-13 20:08:38 +01005216 // count the number of characters
5217 t = str;
5218 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5219 t += mb_ptr2len(t);
5220
5221 // In insert mode, when the cursor is at the end of a non-empty line,
5222 // byteidx points to the NUL character immediately past the end of the
5223 // string. In this case, add one to the character count.
5224 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5225 count++;
5226
5227 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005228}
5229
5230/*
5231 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005232 * byte index. Works only for loaded buffers. Returns -1 on failure.
5233 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005234 */
5235 int
5236buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5237{
5238 char_u *str;
5239 char_u *t;
5240
5241 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5242 return -1;
5243
5244 if (lnum > buf->b_ml.ml_line_count)
5245 lnum = buf->b_ml.ml_line_count;
5246
5247 str = ml_get_buf(buf, lnum, FALSE);
5248 if (str == NULL)
5249 return -1;
5250
5251 // Convert the character offset to a byte offset
5252 t = str;
5253 while (*t != NUL && --charidx > 0)
5254 t += mb_ptr2len(t);
5255
Bram Moolenaar91458462021-01-13 20:08:38 +01005256 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005257}
5258
5259/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005261 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005263 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005264var2fpos(
5265 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005266 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005267 int *fnum, // set to fnum for '0, 'A, etc.
5268 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005270 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005272 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005274 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005275 if (varp->v_type == VAR_LIST)
5276 {
5277 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005278 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005279 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005280 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005281
5282 l = varp->vval.v_list;
5283 if (l == NULL)
5284 return NULL;
5285
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005286 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005287 pos.lnum = list_find_nr(l, 0L, &error);
5288 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005289 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005290 if (charcol)
5291 len = (long)mb_charlen(ml_get(pos.lnum));
5292 else
5293 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005294
Bram Moolenaarec65d772020-08-20 22:29:12 +02005295 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005296 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005297 li = list_find(l, 1L);
5298 if (li != NULL && li->li_tv.v_type == VAR_STRING
5299 && li->li_tv.vval.v_string != NULL
5300 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005301 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005302 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005303 }
5304 else
5305 {
5306 pos.col = list_find_nr(l, 1L, &error);
5307 if (error)
5308 return NULL;
5309 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005310
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005311 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005312 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005313 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005314 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005315
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005316 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005317 pos.coladd = list_find_nr(l, 2L, &error);
5318 if (error)
5319 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005320
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005321 return &pos;
5322 }
5323
Bram Moolenaarc5809432021-03-27 21:23:30 +01005324 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5325 return NULL;
5326
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005327 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005328 if (name == NULL)
5329 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005330 if (name[0] == '.') // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005331 {
5332 pos = curwin->w_cursor;
5333 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005334 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005335 return &pos;
5336 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005337 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005338 {
5339 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005340 pos = VIsual;
5341 else
5342 pos = curwin->w_cursor;
5343 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005344 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005345 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005346 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005347 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005349 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5351 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005352 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005353 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 return pp;
5355 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005356
Bram Moolenaara5525202006-03-02 22:52:09 +00005357 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005358
Bram Moolenaar477933c2007-07-17 14:32:23 +00005359 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005360 {
5361 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005362 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005363 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005364 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005365 // In silent Ex mode topline is zero, but that's not a valid line
5366 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005367 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005368 return &pos;
5369 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005370 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005371 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005372 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005373 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005374 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005375 return &pos;
5376 }
5377 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005378 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005380 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 {
5382 pos.lnum = curbuf->b_ml.ml_line_count;
5383 pos.col = 0;
5384 }
5385 else
5386 {
5387 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005388 if (charcol)
5389 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5390 else
5391 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 }
5393 return &pos;
5394 }
5395 return NULL;
5396}
5397
5398/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005399 * Convert list in "arg" into a position and optional file number.
5400 * When "fnump" is NULL there is no file number, only 3 items.
5401 * Note that the column is passed on as-is, the caller may want to decrement
5402 * it to use 1 for the first column.
5403 * Return FAIL when conversion is not possible, doesn't check the position for
5404 * validity.
5405 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005406 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005407list2fpos(
5408 typval_T *arg,
5409 pos_T *posp,
5410 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005411 colnr_T *curswantp,
5412 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005413{
5414 list_T *l = arg->vval.v_list;
5415 long i = 0;
5416 long n;
5417
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005418 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5419 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005420 if (arg->v_type != VAR_LIST
5421 || l == NULL
5422 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005423 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005424 return FAIL;
5425
5426 if (fnump != NULL)
5427 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005428 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005429 if (n < 0)
5430 return FAIL;
5431 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005432 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005433 *fnump = n;
5434 }
5435
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005436 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005437 if (n < 0)
5438 return FAIL;
5439 posp->lnum = n;
5440
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005441 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005442 if (n < 0)
5443 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005444 // If character position is specified, then convert to byte position
5445 if (charcol)
5446 {
5447 buf_T *buf;
5448
5449 // Get the text for the specified line in a loaded buffer
5450 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5451 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5452 return FAIL;
5453
Bram Moolenaar91458462021-01-13 20:08:38 +01005454 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005455 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005456 posp->col = n;
5457
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005458 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005459 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005460 posp->coladd = 0;
5461 else
5462 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005463
Bram Moolenaar493c1782014-05-28 14:34:46 +02005464 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005465 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005466
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005467 return OK;
5468}
5469
5470/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 * Get the length of an environment variable name.
5472 * Advance "arg" to the first character after the name.
5473 * Return 0 for error.
5474 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005475 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005476get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477{
5478 char_u *p;
5479 int len;
5480
5481 for (p = *arg; vim_isIDc(*p); ++p)
5482 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005483 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 return 0;
5485
5486 len = (int)(p - *arg);
5487 *arg = p;
5488 return len;
5489}
5490
5491/*
5492 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005493 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 * Return 0 if something is wrong.
5495 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005496 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005497get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498{
5499 char_u *p;
5500 int len;
5501
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005502 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005504 {
5505 if (*p == ':')
5506 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005507 // "s:" is start of "s:var", but "n:" is not and can be used in
5508 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005509 len = (int)(p - *arg);
5510 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5511 || len > 1)
5512 break;
5513 }
5514 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005515 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 return 0;
5517
5518 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005519 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520
5521 return len;
5522}
5523
5524/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005525 * Get the length of the name of a variable or function.
5526 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005528 * Return -1 if curly braces expansion failed.
5529 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 * If the name contains 'magic' {}'s, expand them and return the
5531 * expanded name in an allocated string via 'alias' - caller must free.
5532 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005533 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005534get_name_len(
5535 char_u **arg,
5536 char_u **alias,
5537 int evaluate,
5538 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539{
5540 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 char_u *p;
5542 char_u *expr_start;
5543 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005545 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546
5547 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5548 && (*arg)[2] == (int)KE_SNR)
5549 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005550 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 *arg += 3;
5552 return get_id_len(arg) + 3;
5553 }
5554 len = eval_fname_script(*arg);
5555 if (len > 0)
5556 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005557 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 *arg += len;
5559 }
5560
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005562 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005564 p = find_name_end(*arg, &expr_start, &expr_end,
5565 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 if (expr_start != NULL)
5567 {
5568 char_u *temp_string;
5569
5570 if (!evaluate)
5571 {
5572 len += (int)(p - *arg);
5573 *arg = skipwhite(p);
5574 return len;
5575 }
5576
5577 /*
5578 * Include any <SID> etc in the expanded string:
5579 * Thus the -len here.
5580 */
5581 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5582 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005583 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 *alias = temp_string;
5585 *arg = skipwhite(p);
5586 return (int)STRLEN(temp_string);
5587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588
5589 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005590 // Only give an error when there is something, otherwise it will be
5591 // reported at a higher level.
5592 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005593 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594
5595 return len;
5596}
5597
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005598/*
5599 * Find the end of a variable or function name, taking care of magic braces.
5600 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5601 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005602 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005603 * Return a pointer to just after the name. Equal to "arg" if there is no
5604 * valid name.
5605 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005606 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005607find_name_end(
5608 char_u *arg,
5609 char_u **expr_start,
5610 char_u **expr_end,
5611 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005613 int mb_nest = 0;
5614 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005616 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005617 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005619 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005621 *expr_start = NULL;
5622 *expr_end = NULL;
5623 }
5624
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005625 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005626 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5627 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005628 return arg;
5629
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005630 for (p = arg; *p != NUL
5631 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005632 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005633 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005634 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005635 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005636 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005637 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005638 if (*p == '\'')
5639 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005640 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005641 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005642 ;
5643 if (*p == NUL)
5644 break;
5645 }
5646 else if (*p == '"')
5647 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005648 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005649 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005650 if (*p == '\\' && p[1] != NUL)
5651 ++p;
5652 if (*p == NUL)
5653 break;
5654 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005655 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5656 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005657 // "s:" is start of "s:var", but "n:" is not and can be used in
5658 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005659 len = (int)(p - arg);
5660 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005661 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005662 break;
5663 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005664
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005665 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005667 if (*p == '[')
5668 ++br_nest;
5669 else if (*p == ']')
5670 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005672
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005673 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005675 if (*p == '{')
5676 {
5677 mb_nest++;
5678 if (expr_start != NULL && *expr_start == NULL)
5679 *expr_start = p;
5680 }
5681 else if (*p == '}')
5682 {
5683 mb_nest--;
5684 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5685 *expr_end = p;
5686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 }
5689
5690 return p;
5691}
5692
5693/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005694 * Expands out the 'magic' {}'s in a variable/function name.
5695 * Note that this can call itself recursively, to deal with
5696 * constructs like foo{bar}{baz}{bam}
5697 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5698 * "in_start" ^
5699 * "expr_start" ^
5700 * "expr_end" ^
5701 * "in_end" ^
5702 *
5703 * Returns a new allocated string, which the caller must free.
5704 * Returns NULL for failure.
5705 */
5706 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005707make_expanded_name(
5708 char_u *in_start,
5709 char_u *expr_start,
5710 char_u *expr_end,
5711 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005712{
5713 char_u c1;
5714 char_u *retval = NULL;
5715 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005716
5717 if (expr_end == NULL || in_end == NULL)
5718 return NULL;
5719 *expr_start = NUL;
5720 *expr_end = NUL;
5721 c1 = *in_end;
5722 *in_end = NUL;
5723
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005724 temp_result = eval_to_string(expr_start + 1, FALSE);
5725 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005726 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005727 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5728 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005729 if (retval != NULL)
5730 {
5731 STRCPY(retval, in_start);
5732 STRCAT(retval, temp_result);
5733 STRCAT(retval, expr_end + 1);
5734 }
5735 }
5736 vim_free(temp_result);
5737
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005738 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005739 *expr_start = '{';
5740 *expr_end = '}';
5741
5742 if (retval != NULL)
5743 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005744 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005745 if (expr_start != NULL)
5746 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005747 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005748 temp_result = make_expanded_name(retval, expr_start,
5749 expr_end, temp_result);
5750 vim_free(retval);
5751 retval = temp_result;
5752 }
5753 }
5754
5755 return retval;
5756}
5757
5758/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005760 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005762 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005763eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005765 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005766}
5767
5768/*
5769 * Return TRUE if character "c" can be used as the first character in a
5770 * variable or function name (excluding '{' and '}').
5771 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005772 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005773eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005774{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005775 return ASCII_ISALPHA(c) || c == '_';
5776}
5777
5778/*
5779 * Return TRUE if character "c" can be used as the first character of a
5780 * dictionary key.
5781 */
5782 int
5783eval_isdictc(int c)
5784{
5785 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786}
5787
5788/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005789 * Handle:
5790 * - expr[expr], expr[expr:expr] subscript
5791 * - ".name" lookup
5792 * - function call with Funcref variable: func(expr)
5793 * - method call: var->method()
5794 *
5795 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005796 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005797 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005798handle_subscript(
5799 char_u **arg,
5800 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005801 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005802 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005803{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005804 int evaluate = evalarg != NULL
5805 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005806 int ret = OK;
5807 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005808 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005809 int getnext;
5810 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005811
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005812 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005813 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005814 // When at the end of the line and ".name" or "->{" or "->X" follows in
5815 // the next line then consume the line break.
5816 p = eval_next_non_blank(*arg, evalarg, &getnext);
5817 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005818 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02005819 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
5820 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
5821 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005822 {
5823 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005824 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005825 check_white = FALSE;
5826 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005827
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01005828 if (rettv->v_type == VAR_ANY)
5829 {
5830 char_u *exp_name;
5831 int cc;
5832 int idx;
5833 ufunc_T *ufunc;
5834 type_T *type;
5835
5836 // Found script from "import * as {name}", script item name must
5837 // follow.
5838 if (**arg != '.')
5839 {
5840 if (verbose)
5841 semsg(_(e_expected_str_but_got_str), "'.'", *arg);
5842 ret = FAIL;
5843 break;
5844 }
5845 ++*arg;
5846 if (IS_WHITE_OR_NUL(**arg))
5847 {
5848 if (verbose)
5849 emsg(_(e_no_white_space_allowed_after_dot));
5850 ret = FAIL;
5851 break;
5852 }
5853
5854 // isolate the name
5855 exp_name = *arg;
5856 while (eval_isnamec(**arg))
5857 ++*arg;
5858 cc = **arg;
5859 **arg = NUL;
5860
5861 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
5862 evalarg->eval_cctx, verbose);
5863 **arg = cc;
5864 *arg = skipwhite(*arg);
5865
5866 if (idx < 0 && ufunc == NULL)
5867 {
5868 ret = FAIL;
5869 break;
5870 }
5871 if (idx >= 0)
5872 {
5873 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
5874 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
5875
5876 copy_tv(sv->sv_tv, rettv);
5877 }
5878 else
5879 {
5880 rettv->v_type = VAR_FUNC;
5881 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
5882 }
5883 }
5884
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005885 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5886 || rettv->v_type == VAR_PARTIAL))
5887 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005888 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005889 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5890 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005891
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005892 // Stop the expression evaluation when immediately aborting on
5893 // error, or when an interrupt occurred or an exception was thrown
5894 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005895 if (aborting())
5896 {
5897 if (ret == OK)
5898 clear_tv(rettv);
5899 ret = FAIL;
5900 }
5901 dict_unref(selfdict);
5902 selfdict = NULL;
5903 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005904 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005905 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005906 if (in_vim9script())
5907 *arg = skipwhite(p + 2);
5908 else
5909 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005910 if (ret == OK)
5911 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005912 if (VIM_ISWHITE(**arg))
5913 {
5914 emsg(_(e_nowhitespace));
5915 ret = FAIL;
5916 }
5917 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005918 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005919 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005920 else
5921 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005922 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005923 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005924 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005925 // "." is ".name" lookup when we found a dict or when evaluating and
5926 // scriptversion is at least 2, where string concatenation is "..".
5927 else if (**arg == '['
5928 || (**arg == '.' && (rettv->v_type == VAR_DICT
5929 || (!evaluate
5930 && (*arg)[1] != '.'
5931 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005932 {
5933 dict_unref(selfdict);
5934 if (rettv->v_type == VAR_DICT)
5935 {
5936 selfdict = rettv->vval.v_dict;
5937 if (selfdict != NULL)
5938 ++selfdict->dv_refcount;
5939 }
5940 else
5941 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005942 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005943 {
5944 clear_tv(rettv);
5945 ret = FAIL;
5946 }
5947 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005948 else
5949 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005950 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005951
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005952 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5953 // Don't do this when "Func" is already a partial that was bound
5954 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005955 if (selfdict != NULL
5956 && (rettv->v_type == VAR_FUNC
5957 || (rettv->v_type == VAR_PARTIAL
5958 && (rettv->vval.v_partial->pt_auto
5959 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005960 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005961
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005962 dict_unref(selfdict);
5963 return ret;
5964}
5965
5966/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005967 * Make a copy of an item.
5968 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005969 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5970 * reference to an already copied list/dict can be used.
5971 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005972 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005973 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005974item_copy(
5975 typval_T *from,
5976 typval_T *to,
5977 int deep,
5978 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005979{
5980 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005981 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005982
Bram Moolenaar33570922005-01-25 22:26:29 +00005983 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005984 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005985 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005986 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005987 }
5988 ++recurse;
5989
5990 switch (from->v_type)
5991 {
5992 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005993 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005994 case VAR_STRING:
5995 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005996 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005997 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005998 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005999 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006000 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006001 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006002 copy_tv(from, to);
6003 break;
6004 case VAR_LIST:
6005 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006006 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006007 if (from->vval.v_list == NULL)
6008 to->vval.v_list = NULL;
6009 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6010 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006011 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006012 to->vval.v_list = from->vval.v_list->lv_copylist;
6013 ++to->vval.v_list->lv_refcount;
6014 }
6015 else
6016 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
6017 if (to->vval.v_list == NULL)
6018 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006019 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006020 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006021 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006022 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006023 case VAR_DICT:
6024 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006025 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006026 if (from->vval.v_dict == NULL)
6027 to->vval.v_dict = NULL;
6028 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6029 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006030 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006031 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6032 ++to->vval.v_dict->dv_refcount;
6033 }
6034 else
6035 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
6036 if (to->vval.v_dict == NULL)
6037 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006038 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006039 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006040 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006041 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006042 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006043 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006044 }
6045 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006046 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006047}
6048
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006049 void
6050echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6051{
6052 char_u *tofree;
6053 char_u numbuf[NUMBUFLEN];
6054 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6055
6056 if (*atstart)
6057 {
6058 *atstart = FALSE;
6059 // Call msg_start() after eval1(), evaluating the expression
6060 // may cause a message to appear.
6061 if (with_space)
6062 {
6063 // Mark the saved text as finishing the line, so that what
6064 // follows is displayed on a new line when scrolling back
6065 // at the more prompt.
6066 msg_sb_eol();
6067 msg_start();
6068 }
6069 }
6070 else if (with_space)
6071 msg_puts_attr(" ", echo_attr);
6072
6073 if (p != NULL)
6074 for ( ; *p != NUL && !got_int; ++p)
6075 {
6076 if (*p == '\n' || *p == '\r' || *p == TAB)
6077 {
6078 if (*p != TAB && *needclr)
6079 {
6080 // remove any text still there from the command
6081 msg_clr_eos();
6082 *needclr = FALSE;
6083 }
6084 msg_putchar_attr(*p, echo_attr);
6085 }
6086 else
6087 {
6088 if (has_mbyte)
6089 {
6090 int i = (*mb_ptr2len)(p);
6091
6092 (void)msg_outtrans_len_attr(p, i, echo_attr);
6093 p += i - 1;
6094 }
6095 else
6096 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6097 }
6098 }
6099 vim_free(tofree);
6100}
6101
Bram Moolenaare9a41262005-01-15 22:18:47 +00006102/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 * ":echo expr1 ..." print each argument separated with a space, add a
6104 * newline at the end.
6105 * ":echon expr1 ..." print each argument plain.
6106 */
6107 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006108ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109{
6110 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006111 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006112 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 int needclr = TRUE;
6114 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006115 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006116 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006117 evalarg_T evalarg;
6118
Bram Moolenaare707c882020-07-01 13:07:37 +02006119 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120
6121 if (eap->skip)
6122 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006123 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006125 // If eval1() causes an error message the text from the command may
6126 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006127 need_clr_eos = needclr;
6128
Bram Moolenaar68db9962021-05-09 23:19:22 +02006129 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006130 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 {
6132 /*
6133 * Report the invalid expression unless the expression evaluation
6134 * has been cancelled due to an aborting error, an interrupt, or an
6135 * exception.
6136 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006137 if (!aborting() && did_emsg == did_emsg_before
6138 && called_emsg == called_emsg_before)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006139 semsg(_(e_invexpr2), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006140 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 break;
6142 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006143 need_clr_eos = FALSE;
6144
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006146 {
6147 if (rettv.v_type == VAR_VOID)
6148 {
6149 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6150 break;
6151 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006152 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006153 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006154
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006155 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 arg = skipwhite(arg);
6157 }
6158 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006159 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160
6161 if (eap->skip)
6162 --emsg_skip;
6163 else
6164 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006165 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 if (needclr)
6167 msg_clr_eos();
6168 if (eap->cmdidx == CMD_echo)
6169 msg_end();
6170 }
6171}
6172
6173/*
6174 * ":echohl {name}".
6175 */
6176 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006177ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006179 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180}
6181
6182/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006183 * Returns the :echo attribute
6184 */
6185 int
6186get_echo_attr(void)
6187{
6188 return echo_attr;
6189}
6190
6191/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 * ":execute expr1 ..." execute the result of an expression.
6193 * ":echomsg expr1 ..." Print a message
6194 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006195 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 * Each gets spaces around each argument and a newline at the end for
6197 * echo commands
6198 */
6199 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006200ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201{
6202 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006203 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 int ret = OK;
6205 char_u *p;
6206 garray_T ga;
6207 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208
6209 ga_init2(&ga, 1, 80);
6210
6211 if (eap->skip)
6212 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006213 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006215 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006216 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218
6219 if (!eap->skip)
6220 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006221 char_u buf[NUMBUFLEN];
6222
6223 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006224 {
6225 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6226 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006227 semsg(_(e_using_invalid_value_as_string_str),
6228 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006229 p = NULL;
6230 }
6231 else
6232 p = tv_get_string_buf(&rettv, buf);
6233 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006234 else
6235 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006236 if (p == NULL)
6237 {
6238 clear_tv(&rettv);
6239 ret = FAIL;
6240 break;
6241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 len = (int)STRLEN(p);
6243 if (ga_grow(&ga, len + 2) == FAIL)
6244 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006245 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 ret = FAIL;
6247 break;
6248 }
6249 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 ga.ga_len += len;
6253 }
6254
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006255 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 arg = skipwhite(arg);
6257 }
6258
6259 if (ret != FAIL && ga.ga_data != NULL)
6260 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006261 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6262 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006263 // Mark the already saved text as finishing the line, so that what
6264 // follows is displayed on a new line when scrolling back at the
6265 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006266 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006267 }
6268
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006270 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006271 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006272 out_flush();
6273 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006274 else if (eap->cmdidx == CMD_echoconsole)
6275 {
6276 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6277 ui_write((char_u *)"\r\n", 2, TRUE);
6278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 else if (eap->cmdidx == CMD_echoerr)
6280 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006281 int save_did_emsg = did_emsg;
6282
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006283 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006284 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 if (!force_abort)
6286 did_emsg = save_did_emsg;
6287 }
6288 else if (eap->cmdidx == CMD_execute)
6289 do_cmdline((char_u *)ga.ga_data,
6290 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6291 }
6292
6293 ga_clear(&ga);
6294
6295 if (eap->skip)
6296 --emsg_skip;
6297
6298 eap->nextcmd = check_nextcmd(arg);
6299}
6300
6301/*
6302 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6303 * "arg" points to the "&" or '+' when called, to "option" when returning.
6304 * Returns NULL when no option name found. Otherwise pointer to the char
6305 * after the option name.
6306 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006307 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006308find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309{
6310 char_u *p = *arg;
6311
6312 ++p;
6313 if (*p == 'g' && p[1] == ':')
6314 {
6315 *opt_flags = OPT_GLOBAL;
6316 p += 2;
6317 }
6318 else if (*p == 'l' && p[1] == ':')
6319 {
6320 *opt_flags = OPT_LOCAL;
6321 p += 2;
6322 }
6323 else
6324 *opt_flags = 0;
6325
6326 if (!ASCII_ISALPHA(*p))
6327 return NULL;
6328 *arg = p;
6329
6330 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006331 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 else
6333 while (ASCII_ISALPHA(*p))
6334 ++p;
6335 return p;
6336}
6337
6338/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006339 * Display script name where an item was last set.
6340 * Should only be invoked when 'verbose' is non-zero.
6341 */
6342 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006343last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006344{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006345 char_u *p;
6346
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006347 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006348 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006349 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006350 if (p != NULL)
6351 {
6352 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006353 msg_puts(_("\n\tLast set from "));
6354 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006355 if (script_ctx.sc_lnum > 0)
6356 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006357 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006358 msg_outnum((long)script_ctx.sc_lnum);
6359 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006360 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006361 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006362 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006363 }
6364}
6365
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006366#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367
6368/*
6369 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006370 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 * "flags" can be "g" to do a global substitute.
6372 * Returns an allocated string, NULL for error.
6373 */
6374 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006375do_string_sub(
6376 char_u *str,
6377 char_u *pat,
6378 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006379 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006380 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381{
6382 int sublen;
6383 regmatch_T regmatch;
6384 int i;
6385 int do_all;
6386 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006387 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 garray_T ga;
6389 char_u *ret;
6390 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006391 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006393 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006395 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396
6397 ga_init2(&ga, 1, 200);
6398
6399 do_all = (flags[0] == 'g');
6400
6401 regmatch.rm_ic = p_ic;
6402 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6403 if (regmatch.regprog != NULL)
6404 {
6405 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006406 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6408 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006409 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006410 if (regmatch.startp[0] == regmatch.endp[0])
6411 {
6412 if (zero_width == regmatch.startp[0])
6413 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006414 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006415 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006416 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6417 (size_t)i);
6418 ga.ga_len += i;
6419 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006420 continue;
6421 }
6422 zero_width = regmatch.startp[0];
6423 }
6424
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 /*
6426 * Get some space for a temporary buffer to do the substitution
6427 * into. It will contain:
6428 * - The text up to where the match is.
6429 * - The substituted text.
6430 * - The text after the match.
6431 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006432 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006433 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6435 {
6436 ga_clear(&ga);
6437 break;
6438 }
6439
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006440 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 i = (int)(regmatch.startp[0] - tail);
6442 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006443 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006444 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 + ga.ga_len + i, TRUE, TRUE, FALSE);
6446 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006447 tail = regmatch.endp[0];
6448 if (*tail == NUL)
6449 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 if (!do_all)
6451 break;
6452 }
6453
6454 if (ga.ga_data != NULL)
6455 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6456
Bram Moolenaar473de612013-06-08 18:19:48 +02006457 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 }
6459
6460 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6461 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006462 if (p_cpo == empty_option)
6463 p_cpo = save_cpo;
6464 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006465 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006466 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006467 // If it's still empty it was changed and restored, need to restore in
6468 // the complicated way.
6469 if (*p_cpo == NUL)
6470 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006471 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473
6474 return ret;
6475}