blob: ca181d524f05e4c938d66de22205387829d06361 [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 Moolenaar7480b5c2005-01-16 22:07:38 +0000927 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000928 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000929 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
Bram Moolenaar81ed4962020-09-23 15:56:50 +0200930 && !(lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100931 && !(lp->ll_tv->v_type == VAR_BLOB
932 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000933 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000934 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100935 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000936 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000937 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000938 if (lp->ll_range)
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(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000942 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000943 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000944
Bram Moolenaar10c65862020-10-08 21:16:42 +0200945 if (in_vim9script() && lp->ll_valtype == NULL
946 && lp->ll_tv == &v->di_tv
947 && ht != NULL && ht == get_script_local_ht())
948 {
949 svar_T *sv = find_typval_in_script(lp->ll_tv);
950
951 // Vim9 script local variable: get the type
952 if (sv != NULL)
953 lp->ll_valtype = sv->sv_type;
954 }
955
Bram Moolenaar8c711452005-01-14 21:53:12 +0000956 len = -1;
957 if (*p == '.')
958 {
959 key = p + 1;
960 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
961 ;
962 if (len == 0)
963 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000964 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100965 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000966 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000967 }
968 p = key + len;
969 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000970 else
971 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100972 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000973 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000974 if (*p == ':')
975 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000976 else
977 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000978 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200979 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000980 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100981 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000982 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100983 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000984 clear_tv(&var1);
985 return NULL;
986 }
Bram Moolenaar6a250262020-08-04 15:53:01 +0200987 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000988 }
989
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100990 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000991 if (*p == ':')
992 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000993 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000994 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000995 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +0200996 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100997 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000998 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000999 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001000 if (rettv != NULL
1001 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001002 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001003 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001004 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001005 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001006 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001007 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001008 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001009 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001010 }
1011 p = skipwhite(p + 1);
1012 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001013 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001014 else
1015 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001016 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001017 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001018 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001019 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001020 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001021 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001022 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001023 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001024 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001025 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001026 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001027 clear_tv(&var2);
1028 return NULL;
1029 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001030 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001031 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001032 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001033 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001034 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001035
Bram Moolenaar8c711452005-01-14 21:53:12 +00001036 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001037 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001038 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001039 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001040 clear_tv(&var1);
1041 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001042 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001043 }
1044
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001045 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001046 ++p;
1047 }
1048
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001049 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001050 {
1051 if (len == -1)
1052 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001053 // "[key]": get key from "var1"
1054 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001055 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001056 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001057 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001058 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001059 }
1060 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001061 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001062
1063 // a NULL dict is equivalent with an empty dict
1064 if (lp->ll_tv->vval.v_dict == NULL)
1065 {
1066 lp->ll_tv->vval.v_dict = dict_alloc();
1067 if (lp->ll_tv->vval.v_dict == NULL)
1068 {
1069 clear_tv(&var1);
1070 return NULL;
1071 }
1072 ++lp->ll_tv->vval.v_dict->dv_refcount;
1073 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001074 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001075
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001076 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001077
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001078 // When assigning to a scope dictionary check that a function and
1079 // variable name is valid (only variable name unless it is l: or
1080 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001081 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001082 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001083 int prevval;
1084 int wrong;
1085
1086 if (len != -1)
1087 {
1088 prevval = key[len];
1089 key[len] = NUL;
1090 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001091 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001092 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001093 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1094 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001095 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar03290b82020-12-19 16:30:44 +01001096 || !valid_varname(key, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001097 if (len != -1)
1098 key[len] = prevval;
1099 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001100 {
1101 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001102 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001103 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001104 }
1105
Bram Moolenaar10c65862020-10-08 21:16:42 +02001106 if (lp->ll_valtype != NULL)
1107 // use the type of the member
1108 lp->ll_valtype = lp->ll_valtype->tt_member;
1109
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001110 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001111 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001112 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001113 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001114 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001115 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001116 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001117 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001118 return NULL;
1119 }
1120
Bram Moolenaar31b81602019-02-10 22:14:27 +01001121 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001122 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001123 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001124 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001125 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001126 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001127 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001128 }
1129 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001130 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001131 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001132 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001133 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001134 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001135 p = NULL;
1136 break;
1137 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001138 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001139 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001140 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1141 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001142 {
1143 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001144 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001145 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001146
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001147 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001148 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001149 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001150 else if (lp->ll_tv->v_type == VAR_BLOB)
1151 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001152 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1153
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001154 /*
1155 * Get the number and item for the only or first index of the List.
1156 */
1157 if (empty1)
1158 lp->ll_n1 = 0;
1159 else
1160 // is number or string
1161 lp->ll_n1 = (long)tv_get_number(&var1);
1162 clear_tv(&var1);
1163
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001164 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001165 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001166 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001167 return NULL;
1168 }
1169 if (lp->ll_range && !lp->ll_empty2)
1170 {
1171 lp->ll_n2 = (long)tv_get_number(&var2);
1172 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001173 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1174 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001175 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001176 }
1177 lp->ll_blob = lp->ll_tv->vval.v_blob;
1178 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001179 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001180 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001181 else
1182 {
1183 /*
1184 * Get the number and item for the only or first index of the List.
1185 */
1186 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001187 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001188 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001189 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001190 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001191 clear_tv(&var1);
1192
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001193 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001194 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001195 lp->ll_li = list_find_index(lp->ll_list, &lp->ll_n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001196 if (lp->ll_li == NULL)
1197 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001198 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001199 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001200 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001201 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001202 }
1203
Bram Moolenaar10c65862020-10-08 21:16:42 +02001204 if (lp->ll_valtype != NULL)
1205 // use the type of the member
1206 lp->ll_valtype = lp->ll_valtype->tt_member;
1207
Bram Moolenaar8c711452005-01-14 21:53:12 +00001208 /*
1209 * May need to find the item or absolute index for the second
1210 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001211 * When no index given: "lp->ll_empty2" is TRUE.
1212 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001213 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001214 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001215 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001216 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001217 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001218 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001219 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001220 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001221 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001222 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001223 {
1224 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001225 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001226 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001227 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001228 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001229 }
1230
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001231 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001232 if (lp->ll_n1 < 0)
1233 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1234 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001235 {
1236 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001237 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001238 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001239 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001240 }
1241
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001242 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001243 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001244 }
1245
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001246 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001247 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001248 return p;
1249}
1250
1251/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001252 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001253 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001254 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001255clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001256{
1257 vim_free(lp->ll_exp_name);
1258 vim_free(lp->ll_newkey);
1259}
1260
1261/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001262 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001263 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001264 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1265 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001266 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001267 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001268set_var_lval(
1269 lval_T *lp,
1270 char_u *endp,
1271 typval_T *rettv,
1272 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001273 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1274 char_u *op,
1275 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001276{
1277 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001278 listitem_T *ri;
1279 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001280
1281 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001282 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001283 cc = *endp;
1284 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001285 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1286 return;
1287
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001288 if (lp->ll_blob != NULL)
1289 {
1290 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001291
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001292 if (op != NULL && *op != '=')
1293 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001294 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001295 return;
1296 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001297 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001298 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001299
1300 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1301 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001302 if (lp->ll_empty2)
1303 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1304
Bram Moolenaar68452172021-04-12 21:21:02 +02001305 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1306 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001307 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001308 }
1309 else
1310 {
1311 val = (int)tv_get_number_chk(rettv, &error);
1312 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001313 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001314 }
1315 }
1316 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001317 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001318 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001319
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001320 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1321 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001322 {
1323 emsg(_(e_cannot_mod));
1324 *endp = cc;
1325 return;
1326 }
1327
Bram Moolenaarff697e62019-02-12 22:28:33 +01001328 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001329 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001330 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001331 &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001332 {
1333 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001334 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1335 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001336 && tv_op(&tv, rettv, op) == OK)
1337 set_var(lp->ll_name, &tv, FALSE);
1338 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001339 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001340 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001341 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001342 {
1343 if (lp->ll_type != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001344 && check_typval_arg_type(lp->ll_type, rettv, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001345 return;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001346 set_var_const(lp->ll_name, lp->ll_type, rettv, copy,
1347 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001348 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001349 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001350 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001351 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001352 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001353 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001354 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001355 else if (lp->ll_range)
1356 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001357 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001358 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001359
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001360 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1361 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001362 {
1363 emsg(_("E996: Cannot lock a range"));
1364 return;
1365 }
1366
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001367 /*
1368 * Check whether any of the list items is locked
1369 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001370 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001371 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001372 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001373 return;
1374 ri = ri->li_next;
1375 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1376 break;
1377 ll_li = ll_li->li_next;
1378 ++ll_n1;
1379 }
1380
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001381 /*
1382 * Assign the List values to the list items.
1383 */
1384 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001385 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001386 if (op != NULL && *op != '=')
1387 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1388 else
1389 {
1390 clear_tv(&lp->ll_li->li_tv);
1391 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1392 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001393 ri = ri->li_next;
1394 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1395 break;
1396 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001397 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001398 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001399 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001400 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001401 ri = NULL;
1402 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001403 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001404 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001405 lp->ll_li = lp->ll_li->li_next;
1406 ++lp->ll_n1;
1407 }
1408 if (ri != NULL)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001409 emsg(_(e_list_value_has_more_items_than_targets));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001410 else if (lp->ll_empty2
1411 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001412 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001413 emsg(_(e_list_value_does_not_have_enough_items));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001414 }
1415 else
1416 {
1417 /*
1418 * Assign to a List or Dictionary item.
1419 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001420 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1421 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001422 {
1423 emsg(_("E996: Cannot lock a list or dict"));
1424 return;
1425 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001426
1427 if (lp->ll_valtype != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001428 && check_typval_arg_type(lp->ll_valtype, rettv, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001429 return;
1430
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001431 if (lp->ll_newkey != NULL)
1432 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001433 if (op != NULL && *op != '=')
1434 {
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02001435 semsg(_(e_dictkey), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001436 return;
1437 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001438 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1439 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001440 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001441
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001442 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001443 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001444 if (di == NULL)
1445 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001446 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1447 {
1448 vim_free(di);
1449 return;
1450 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001451 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001452 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001453 else if (op != NULL && *op != '=')
1454 {
1455 tv_op(lp->ll_tv, rettv, op);
1456 return;
1457 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001458 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001459 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001460
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001461 /*
1462 * Assign the value to the variable or list item.
1463 */
1464 if (copy)
1465 copy_tv(rettv, lp->ll_tv);
1466 else
1467 {
1468 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001469 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001470 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001471 }
1472 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001473}
1474
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001475/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001476 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1477 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001478 * Returns OK or FAIL.
1479 */
1480 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001481tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001482{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001483 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001484 char_u numbuf[NUMBUFLEN];
1485 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001486 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001487
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001488 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001489 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001490 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001491 {
1492 switch (tv1->v_type)
1493 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001494 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001495 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001496 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001497 case VAR_DICT:
1498 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001499 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001500 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001501 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001502 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001503 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001504 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001505 break;
1506
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001507 case VAR_BLOB:
1508 if (*op != '+' || tv2->v_type != VAR_BLOB)
1509 break;
1510 // BLOB += BLOB
1511 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1512 {
1513 blob_T *b1 = tv1->vval.v_blob;
1514 blob_T *b2 = tv2->vval.v_blob;
1515 int i, len = blob_len(b2);
1516 for (i = 0; i < len; i++)
1517 ga_append(&b1->bv_ga, blob_get(b2, i));
1518 }
1519 return OK;
1520
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001521 case VAR_LIST:
1522 if (*op != '+' || tv2->v_type != VAR_LIST)
1523 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001524 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001525 if (tv2->vval.v_list != NULL)
1526 {
1527 if (tv1->vval.v_list == NULL)
1528 {
1529 tv1->vval.v_list = tv2->vval.v_list;
1530 ++tv1->vval.v_list->lv_refcount;
1531 }
1532 else
1533 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1534 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001535 return OK;
1536
1537 case VAR_NUMBER:
1538 case VAR_STRING:
1539 if (tv2->v_type == VAR_LIST)
1540 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001541 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001542 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001543 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001544 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001545#ifdef FEAT_FLOAT
1546 if (tv2->v_type == VAR_FLOAT)
1547 {
1548 float_T f = n;
1549
Bram Moolenaarff697e62019-02-12 22:28:33 +01001550 if (*op == '%')
1551 break;
1552 switch (*op)
1553 {
1554 case '+': f += tv2->vval.v_float; break;
1555 case '-': f -= tv2->vval.v_float; break;
1556 case '*': f *= tv2->vval.v_float; break;
1557 case '/': f /= tv2->vval.v_float; break;
1558 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001559 clear_tv(tv1);
1560 tv1->v_type = VAR_FLOAT;
1561 tv1->vval.v_float = f;
1562 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001563 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001564#endif
1565 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001566 switch (*op)
1567 {
1568 case '+': n += tv_get_number(tv2); break;
1569 case '-': n -= tv_get_number(tv2); break;
1570 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001571 case '/': n = num_divide(n, tv_get_number(tv2),
1572 &failed); break;
1573 case '%': n = num_modulus(n, tv_get_number(tv2),
1574 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001575 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001576 clear_tv(tv1);
1577 tv1->v_type = VAR_NUMBER;
1578 tv1->vval.v_number = n;
1579 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001580 }
1581 else
1582 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001583 if (tv2->v_type == VAR_FLOAT)
1584 break;
1585
Bram Moolenaarff697e62019-02-12 22:28:33 +01001586 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001587 s = tv_get_string(tv1);
1588 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001589 clear_tv(tv1);
1590 tv1->v_type = VAR_STRING;
1591 tv1->vval.v_string = s;
1592 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001593 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001594
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001595 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001596#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001597 {
1598 float_T f;
1599
Bram Moolenaarff697e62019-02-12 22:28:33 +01001600 if (*op == '%' || *op == '.'
1601 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001602 && tv2->v_type != VAR_NUMBER
1603 && tv2->v_type != VAR_STRING))
1604 break;
1605 if (tv2->v_type == VAR_FLOAT)
1606 f = tv2->vval.v_float;
1607 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001608 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001609 switch (*op)
1610 {
1611 case '+': tv1->vval.v_float += f; break;
1612 case '-': tv1->vval.v_float -= f; break;
1613 case '*': tv1->vval.v_float *= f; break;
1614 case '/': tv1->vval.v_float /= f; break;
1615 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001616 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001617#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001618 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001619 }
1620 }
1621
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001622 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001623 return FAIL;
1624}
1625
1626/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001627 * Evaluate the expression used in a ":for var in expr" command.
1628 * "arg" points to "var".
1629 * Set "*errp" to TRUE for an error, FALSE otherwise;
1630 * Return a pointer that holds the info. Null when there is an error.
1631 */
1632 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001633eval_for_line(
1634 char_u *arg,
1635 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001636 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001637 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001638{
Bram Moolenaar33570922005-01-25 22:26:29 +00001639 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001640 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001641 typval_T tv;
1642 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001643 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001644
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001645 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001646
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001647 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001648 if (fi == NULL)
1649 return NULL;
1650
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001651 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001652 if (expr == NULL)
1653 return fi;
1654
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001655 expr = skipwhite_and_linebreak(expr, evalarg);
1656 if (expr[0] != 'i' || expr[1] != 'n'
1657 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001658 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001659 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001660 return fi;
1661 }
1662
1663 if (skip)
1664 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001665 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1666 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001667 {
1668 *errp = FALSE;
1669 if (!skip)
1670 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001671 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001672 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001673 l = tv.vval.v_list;
1674 if (l == NULL)
1675 {
1676 // a null list is like an empty list: do nothing
1677 clear_tv(&tv);
1678 }
1679 else
1680 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001681 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001682 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001683
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001684 // No need to increment the refcount, it's already set for
1685 // the list being used in "tv".
1686 fi->fi_list = l;
1687 list_add_watch(l, &fi->fi_lw);
1688 fi->fi_lw.lw_item = l->lv_first;
1689 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001690 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001691 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001692 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001693 fi->fi_bi = 0;
1694 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001695 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001696 typval_T btv;
1697
1698 // Make a copy, so that the iteration still works when the
1699 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001700 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001701 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001702 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001703 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001704 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001705 else if (tv.v_type == VAR_STRING)
1706 {
1707 fi->fi_byte_idx = 0;
1708 fi->fi_string = tv.vval.v_string;
1709 tv.vval.v_string = NULL;
1710 if (fi->fi_string == NULL)
1711 fi->fi_string = vim_strsave((char_u *)"");
1712 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001713 else
1714 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001715 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001716 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001717 }
1718 }
1719 }
1720 if (skip)
1721 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001722 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001723
1724 return fi;
1725}
1726
1727/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001728 * Used when looping over a :for line, skip the "in expr" part.
1729 */
1730 void
1731skip_for_lines(void *fi_void, evalarg_T *evalarg)
1732{
1733 forinfo_T *fi = (forinfo_T *)fi_void;
1734 int i;
1735
1736 for (i = 0; i < fi->fi_break_count; ++i)
1737 eval_next_line(evalarg);
1738}
1739
1740/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001741 * Use the first item in a ":for" list. Advance to the next.
1742 * Assign the values to the variable (list). "arg" points to the first one.
1743 * Return TRUE when a valid item was found, FALSE when at end of list or
1744 * something wrong.
1745 */
1746 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001747next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001748{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001749 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001750 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001751 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
1752 ? (ASSIGN_FINAL | ASSIGN_DECL | ASSIGN_NO_MEMBER_TYPE)
1753 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001754 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001755
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001756 if (fi->fi_blob != NULL)
1757 {
1758 typval_T tv;
1759
1760 if (fi->fi_bi >= blob_len(fi->fi_blob))
1761 return FALSE;
1762 tv.v_type = VAR_NUMBER;
1763 tv.v_lock = VAR_FIXED;
1764 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1765 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001766 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001767 fi->fi_varcount, flag, NULL) == OK;
1768 }
1769
1770 if (fi->fi_string != NULL)
1771 {
1772 typval_T tv;
1773 int len;
1774
1775 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1776 if (len == 0)
1777 return FALSE;
1778 tv.v_type = VAR_STRING;
1779 tv.v_lock = VAR_FIXED;
1780 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1781 fi->fi_byte_idx += len;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001782 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001783 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001784 vim_free(tv.vval.v_string);
1785 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001786 }
1787
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001788 item = fi->fi_lw.lw_item;
1789 if (item == NULL)
1790 result = FALSE;
1791 else
1792 {
1793 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001794 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001795 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001796 }
1797 return result;
1798}
1799
1800/*
1801 * Free the structure used to store info used by ":for".
1802 */
1803 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001804free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001805{
Bram Moolenaar33570922005-01-25 22:26:29 +00001806 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001807
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001808 if (fi == NULL)
1809 return;
1810 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001811 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001812 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001813 list_unref(fi->fi_list);
1814 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001815 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001816 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001817 else
1818 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001819 vim_free(fi);
1820}
1821
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001823set_context_for_expression(
1824 expand_T *xp,
1825 char_u *arg,
1826 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001828 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001830 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001832 if (cmdidx == CMD_let || cmdidx == CMD_var
1833 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001834 {
1835 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001836 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001837 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001838 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001839 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001840 {
1841 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001842 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001843 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001844 break;
1845 }
1846 return;
1847 }
1848 }
1849 else
1850 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1851 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 while ((xp->xp_pattern = vim_strpbrk(arg,
1853 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1854 {
1855 c = *xp->xp_pattern;
1856 if (c == '&')
1857 {
1858 c = xp->xp_pattern[1];
1859 if (c == '&')
1860 {
1861 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001862 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 }
1864 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001865 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001867 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1868 xp->xp_pattern += 2;
1869
1870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 }
1872 else if (c == '$')
1873 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001874 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 xp->xp_context = EXPAND_ENV_VARS;
1876 }
1877 else if (c == '=')
1878 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001879 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 xp->xp_context = EXPAND_EXPRESSION;
1881 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001882 else if (c == '#'
1883 && xp->xp_context == EXPAND_EXPRESSION)
1884 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001885 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001886 break;
1887 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001888 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 && xp->xp_context == EXPAND_FUNCTIONS
1890 && vim_strchr(xp->xp_pattern, '(') == NULL)
1891 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001892 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 break;
1894 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001895 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001897 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 {
1899 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1900 if (c == '\\' && xp->xp_pattern[1] != NUL)
1901 ++xp->xp_pattern;
1902 xp->xp_context = EXPAND_NOTHING;
1903 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001904 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001906 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1908 /* skip */ ;
1909 xp->xp_context = EXPAND_NOTHING;
1910 }
1911 else if (c == '|')
1912 {
1913 if (xp->xp_pattern[1] == '|')
1914 {
1915 ++xp->xp_pattern;
1916 xp->xp_context = EXPAND_EXPRESSION;
1917 }
1918 else
1919 xp->xp_context = EXPAND_COMMANDS;
1920 }
1921 else
1922 xp->xp_context = EXPAND_EXPRESSION;
1923 }
1924 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001925 // Doesn't look like something valid, expand as an expression
1926 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001927 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 arg = xp->xp_pattern;
1929 if (*arg != NUL)
1930 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1931 /* skip */ ;
1932 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01001933
1934 // ":exe one two" completes "two"
1935 if ((cmdidx == CMD_execute
1936 || cmdidx == CMD_echo
1937 || cmdidx == CMD_echon
1938 || cmdidx == CMD_echomsg)
1939 && xp->xp_context == EXPAND_EXPRESSION)
1940 {
1941 for (;;)
1942 {
1943 char_u *n = skiptowhite(arg);
1944
1945 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
1946 break;
1947 arg = skipwhite(n);
1948 }
1949 }
1950
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 xp->xp_pattern = arg;
1952}
1953
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001955 * Return TRUE if "pat" matches "text".
1956 * Does not use 'cpo' and always uses 'magic'.
1957 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001958 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001959pattern_match(char_u *pat, char_u *text, int ic)
1960{
1961 int matches = FALSE;
1962 char_u *save_cpo;
1963 regmatch_T regmatch;
1964
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001965 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001966 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01001967 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001968 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1969 if (regmatch.regprog != NULL)
1970 {
1971 regmatch.rm_ic = ic;
1972 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1973 vim_regfree(regmatch.regprog);
1974 }
1975 p_cpo = save_cpo;
1976 return matches;
1977}
1978
1979/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001980 * Handle a name followed by "(". Both for just "name(arg)" and for
1981 * "expr->name(arg)".
1982 * Returns OK or FAIL.
1983 */
1984 static int
1985eval_func(
1986 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02001987 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001988 char_u *name,
1989 int name_len,
1990 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001991 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001992 typval_T *basetv) // "expr" for "expr->name(arg)"
1993{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001994 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001995 char_u *s = name;
1996 int len = name_len;
1997 partial_T *partial;
1998 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001999 type_T *type = NULL;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002000
2001 if (!evaluate)
2002 check_vars(s, len);
2003
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002004 // If "s" is the name of a variable of type VAR_FUNC
2005 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002006 s = deref_func_name(s, &len, &partial,
2007 in_vim9script() ? &type : NULL, !evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002008
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002009 // Need to make a copy, in case evaluating the arguments makes
2010 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002011 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002012 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002013 ret = FAIL;
2014 else
2015 {
2016 funcexe_T funcexe;
2017
2018 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002019 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002020 funcexe.firstline = curwin->w_cursor.lnum;
2021 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002022 funcexe.evaluate = evaluate;
2023 funcexe.partial = partial;
2024 funcexe.basetv = basetv;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002025 funcexe.check_type = type;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002026 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002027 }
2028 vim_free(s);
2029
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002030 // If evaluate is FALSE rettv->v_type was not set in
2031 // get_func_tv, but it's needed in handle_subscript() to parse
2032 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002033 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2034 {
2035 rettv->vval.v_string = NULL;
2036 rettv->v_type = VAR_FUNC;
2037 }
2038
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002039 // Stop the expression evaluation when immediately
2040 // aborting on error, or when an interrupt occurred or
2041 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002042 if (evaluate && aborting())
2043 {
2044 if (ret == OK)
2045 clear_tv(rettv);
2046 ret = FAIL;
2047 }
2048 return ret;
2049}
2050
2051/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002052 * Get the next line source line without advancing. But do skip over comment
2053 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002054 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002055 */
2056 static char_u *
2057getline_peek_skip_comments(evalarg_T *evalarg)
2058{
2059 for (;;)
2060 {
2061 char_u *next = getline_peek(evalarg->eval_getline,
2062 evalarg->eval_cookie);
2063 char_u *p;
2064
2065 if (next == NULL)
2066 break;
2067 p = skipwhite(next);
2068 if (*p != NUL && !vim9_comment_start(p))
2069 return next;
2070 (void)eval_next_line(evalarg);
2071 }
2072 return NULL;
2073}
2074
2075/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002076 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2077 * comment) and there is a next line, return the next line (skipping blanks)
2078 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002079 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2080 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002081 * "arg" must point somewhere inside a line, not at the start.
2082 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002083 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002084eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2085{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002086 char_u *p = skipwhite(arg);
2087
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002088 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002089 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002090 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002091 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002092 && (*p == NUL || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002093 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002094 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002095
2096 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002097 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002098 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002099 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002100
Bram Moolenaar9d489562020-07-30 20:08:50 +02002101 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002102 {
2103 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002104 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002105 }
2106 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002107 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002108}
2109
2110/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002111 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002112 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002113 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002114 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002115eval_next_line(evalarg_T *evalarg)
2116{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002117 garray_T *gap = &evalarg->eval_ga;
2118 char_u *line;
2119
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002120 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002121 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2122 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002123 else
2124 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002125 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002126 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2127 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002128 char_u *p = skipwhite(line);
2129
2130 // Going to concatenate the lines after parsing. For an empty or
2131 // comment line use an empty string.
2132 if (*p == NUL || vim9_comment_start(p))
2133 {
2134 vim_free(line);
2135 line = vim_strsave((char_u *)"");
2136 }
2137
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002138 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2139 ++gap->ga_len;
2140 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002141 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002142 {
2143 vim_free(evalarg->eval_tofree);
2144 evalarg->eval_tofree = line;
2145 }
2146 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002147}
2148
Bram Moolenaare6b53242020-07-01 17:28:33 +02002149/*
2150 * Call eval_next_non_blank() and get the next line if needed.
2151 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002152 char_u *
2153skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2154{
2155 int getnext;
2156 char_u *p = skipwhite(arg);
2157
Bram Moolenaar962d7212020-07-04 14:15:00 +02002158 if (evalarg == NULL)
2159 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002160 eval_next_non_blank(p, evalarg, &getnext);
2161 if (getnext)
2162 return eval_next_line(evalarg);
2163 return p;
2164}
2165
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002166/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002167 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002168 */
2169 void
2170clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2171{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002172 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002173 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002174 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002175 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002176 if (eap != NULL)
2177 {
2178 // We may need to keep the original command line, e.g. for
2179 // ":let" it has the variable names. But we may also need the
2180 // new one, "nextcmd" points into it. Keep both.
2181 vim_free(eap->cmdline_tofree);
2182 eap->cmdline_tofree = *eap->cmdlinep;
2183 *eap->cmdlinep = evalarg->eval_tofree;
2184 }
2185 else
2186 vim_free(evalarg->eval_tofree);
2187 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002188 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002189
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002190 VIM_CLEAR(evalarg->eval_tofree_cmdline);
2191 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002192 }
2193}
2194
2195/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002197 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2199 */
2200
2201/*
2202 * Handle zero level expression.
2203 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002204 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002205 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002206 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 * Return OK or FAIL.
2208 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002209 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002210eval0(
2211 char_u *arg,
2212 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002213 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002214 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215{
2216 int ret;
2217 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002218 int did_emsg_before = did_emsg;
2219 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002220 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221
2222 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002223 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002224 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002225
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002226 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 {
2228 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002229 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 /*
2231 * Report the invalid expression unless the expression evaluation has
2232 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002233 * exception, or we already gave a more specific error.
2234 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002236 if (!aborting()
2237 && did_emsg == did_emsg_before
2238 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002239 && (flags & EVAL_CONSTANT) == 0
2240 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002241 semsg(_(e_invexpr2), arg);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002242
2243 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002244 // a next command to avoid more errors, unless "|" is following, which
2245 // could only be a command separator.
2246 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2247 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002248 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002250
2251 if (eap != NULL)
2252 eap->nextcmd = check_nextcmd(p);
2253
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 return ret;
2255}
2256
2257/*
2258 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002259 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002260 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 *
2262 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002263 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002265 * Note: "rettv.v_lock" is not set.
2266 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 * Return OK or FAIL.
2268 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002269 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002270eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002272 char_u *p;
2273 int getnext;
2274
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002275 CLEAR_POINTER(rettv);
2276
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 /*
2278 * Get the first variable.
2279 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002280 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 return FAIL;
2282
Bram Moolenaar793648f2020-06-26 21:28:25 +02002283 p = eval_next_non_blank(*arg, evalarg, &getnext);
2284 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002286 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002287 int result;
2288 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002289 evalarg_T *evalarg_used = evalarg;
2290 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002291 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002292 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002293 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002294
2295 if (evalarg == NULL)
2296 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002297 CLEAR_FIELD(local_evalarg);
2298 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002299 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002300 orig_flags = evalarg_used->eval_flags;
2301 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002302
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002303 if (getnext)
2304 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002305 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002306 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002307 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002308 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002309 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002310 clear_tv(rettv);
2311 return FAIL;
2312 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002313 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002314 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002315
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002317 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002319 int error = FALSE;
2320
Bram Moolenaar13106602020-10-04 16:06:05 +02002321 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002322 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002323 else if (vim9script)
2324 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002325 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002327 if (error || !op_falsy || !result)
2328 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002329 if (error)
2330 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 }
2332
2333 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002334 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002336 if (op_falsy)
2337 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002338 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002339 {
mityu4ac198c2021-05-28 17:52:40 +02002340 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002341 clear_tv(rettv);
2342 return FAIL;
2343 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002344 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002345 evalarg_used->eval_flags = (op_falsy ? !result : result)
2346 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2347 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002348 {
2349 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002351 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002352 if (!op_falsy || !result)
2353 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002355 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002357 /*
2358 * Check for the ":".
2359 */
2360 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2361 if (*p != ':')
2362 {
2363 emsg(_(e_missing_colon));
2364 if (evaluate && result)
2365 clear_tv(rettv);
2366 evalarg_used->eval_flags = orig_flags;
2367 return FAIL;
2368 }
2369 if (getnext)
2370 *arg = eval_next_line(evalarg_used);
2371 else
2372 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002373 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002374 {
2375 error_white_both(p, 1);
2376 clear_tv(rettv);
2377 evalarg_used->eval_flags = orig_flags;
2378 return FAIL;
2379 }
2380 *arg = p;
2381 }
2382
2383 /*
2384 * Get the third variable. Recursive!
2385 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002386 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002387 {
mityu4ac198c2021-05-28 17:52:40 +02002388 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002389 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002390 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002391 return FAIL;
2392 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002393 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2394 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002395 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002396 if (eval1(arg, &var2, evalarg_used) == FAIL)
2397 {
2398 if (evaluate && result)
2399 clear_tv(rettv);
2400 evalarg_used->eval_flags = orig_flags;
2401 return FAIL;
2402 }
2403 if (evaluate && !result)
2404 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002406
2407 if (evalarg == NULL)
2408 clear_evalarg(&local_evalarg, NULL);
2409 else
2410 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 }
2412
2413 return OK;
2414}
2415
2416/*
2417 * Handle first level expression:
2418 * expr2 || expr2 || expr2 logical OR
2419 *
2420 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002421 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 *
2423 * Return OK or FAIL.
2424 */
2425 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002426eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002428 char_u *p;
2429 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430
2431 /*
2432 * Get the first variable.
2433 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002434 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 return FAIL;
2436
2437 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002438 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002440 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002441 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002443 evalarg_T *evalarg_used = evalarg;
2444 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002445 int evaluate;
2446 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002447 long result = FALSE;
2448 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002449 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002450 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002451
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002452 if (evalarg == NULL)
2453 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002454 CLEAR_FIELD(local_evalarg);
2455 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002456 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002457 orig_flags = evalarg_used->eval_flags;
2458 evaluate = orig_flags & EVAL_EVALUATE;
2459 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002460 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002461 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002462 result = tv_get_bool_chk(rettv, &error);
2463 else if (tv_get_number_chk(rettv, &error) != 0)
2464 result = TRUE;
2465 clear_tv(rettv);
2466 if (error)
2467 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 }
2469
2470 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002471 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002473 while (p[0] == '|' && p[1] == '|')
2474 {
2475 if (getnext)
2476 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002477 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002478 {
2479 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2480 {
2481 error_white_both(p, 2);
2482 clear_tv(rettv);
2483 return FAIL;
2484 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002485 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002486 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002487
2488 /*
2489 * Get the second variable.
2490 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002491 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2492 {
mityu4ac198c2021-05-28 17:52:40 +02002493 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002494 clear_tv(rettv);
2495 return FAIL;
2496 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002497 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2498 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002499 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002500 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002501 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002502
2503 /*
2504 * Compute the result.
2505 */
2506 if (evaluate && !result)
2507 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002508 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002509 result = tv_get_bool_chk(&var2, &error);
2510 else if (tv_get_number_chk(&var2, &error) != 0)
2511 result = TRUE;
2512 clear_tv(&var2);
2513 if (error)
2514 return FAIL;
2515 }
2516 if (evaluate)
2517 {
2518 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002519 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002520 rettv->v_type = VAR_BOOL;
2521 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002522 }
2523 else
2524 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002525 rettv->v_type = VAR_NUMBER;
2526 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002527 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002528 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002529
2530 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002532
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002533 if (evalarg == NULL)
2534 clear_evalarg(&local_evalarg, NULL);
2535 else
2536 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 }
2538
2539 return OK;
2540}
2541
2542/*
2543 * Handle second level expression:
2544 * expr3 && expr3 && expr3 logical AND
2545 *
2546 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002547 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 *
2549 * Return OK or FAIL.
2550 */
2551 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002552eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002554 char_u *p;
2555 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556
2557 /*
2558 * Get the first variable.
2559 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002560 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 return FAIL;
2562
2563 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002564 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002566 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002567 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002569 evalarg_T *evalarg_used = evalarg;
2570 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002571 int orig_flags;
2572 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002573 long result = TRUE;
2574 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002575 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002576 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002577
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002578 if (evalarg == NULL)
2579 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002580 CLEAR_FIELD(local_evalarg);
2581 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002582 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002583 orig_flags = evalarg_used->eval_flags;
2584 evaluate = orig_flags & EVAL_EVALUATE;
2585 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002586 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002587 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002588 result = tv_get_bool_chk(rettv, &error);
2589 else if (tv_get_number_chk(rettv, &error) == 0)
2590 result = FALSE;
2591 clear_tv(rettv);
2592 if (error)
2593 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 }
2595
2596 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002597 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002599 while (p[0] == '&' && p[1] == '&')
2600 {
2601 if (getnext)
2602 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002603 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002604 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002605 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002606 {
2607 error_white_both(p, 2);
2608 clear_tv(rettv);
2609 return FAIL;
2610 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002611 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002612 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002613
2614 /*
2615 * Get the second variable.
2616 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002617 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2618 {
mityu4ac198c2021-05-28 17:52:40 +02002619 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002620 clear_tv(rettv);
2621 return FAIL;
2622 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002623 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2624 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002625 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002626 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002627 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002628 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002629
2630 /*
2631 * Compute the result.
2632 */
2633 if (evaluate && result)
2634 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002635 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002636 result = tv_get_bool_chk(&var2, &error);
2637 else if (tv_get_number_chk(&var2, &error) == 0)
2638 result = FALSE;
2639 clear_tv(&var2);
2640 if (error)
2641 return FAIL;
2642 }
2643 if (evaluate)
2644 {
2645 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002646 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002647 rettv->v_type = VAR_BOOL;
2648 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002649 }
2650 else
2651 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002652 rettv->v_type = VAR_NUMBER;
2653 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002654 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002655 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002656
2657 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002659
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002660 if (evalarg == NULL)
2661 clear_evalarg(&local_evalarg, NULL);
2662 else
2663 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 }
2665
2666 return OK;
2667}
2668
2669/*
2670 * Handle third level expression:
2671 * var1 == var2
2672 * var1 =~ var2
2673 * var1 != var2
2674 * var1 !~ var2
2675 * var1 > var2
2676 * var1 >= var2
2677 * var1 < var2
2678 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002679 * var1 is var2
2680 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 *
2682 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002683 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 *
2685 * Return OK or FAIL.
2686 */
2687 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002688eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002691 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002692 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002694 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695
2696 /*
2697 * Get the first variable.
2698 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002699 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 return FAIL;
2701
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002702 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002703 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704
2705 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002706 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002708 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002710 typval_T var2;
2711 int ic;
2712 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002713 int evaluate = evalarg == NULL
2714 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002715
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002716 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002717 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002718 *arg = eval_next_line(evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002719 p = *arg;
2720 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002721 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2722 {
mityu4ac198c2021-05-28 17:52:40 +02002723 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002724 clear_tv(rettv);
2725 return FAIL;
2726 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002727
Bram Moolenaar696ba232020-07-29 21:20:41 +02002728 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2729 {
2730 semsg(_(e_invexpr2), p);
2731 clear_tv(rettv);
2732 return FAIL;
2733 }
2734
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002735 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 if (p[len] == '?')
2737 {
2738 ic = TRUE;
2739 ++len;
2740 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002741 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 else if (p[len] == '#')
2743 {
2744 ic = FALSE;
2745 ++len;
2746 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002747 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002749 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750
2751 /*
2752 * Get the second variable.
2753 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002754 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2755 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002756 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002757 clear_tv(rettv);
2758 return FAIL;
2759 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002760 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002761 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002763 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 return FAIL;
2765 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002766 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002767 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002768 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002769
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002770 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002771 {
2772 ret = FAIL;
2773 clear_tv(rettv);
2774 }
2775 else
2776 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002777 clear_tv(&var2);
2778 return ret;
2779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 }
2781
2782 return OK;
2783}
2784
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002785/*
2786 * Make a copy of blob "tv1" and append blob "tv2".
2787 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002788 void
2789eval_addblob(typval_T *tv1, typval_T *tv2)
2790{
2791 blob_T *b1 = tv1->vval.v_blob;
2792 blob_T *b2 = tv2->vval.v_blob;
2793 blob_T *b = blob_alloc();
2794 int i;
2795
2796 if (b != NULL)
2797 {
2798 for (i = 0; i < blob_len(b1); i++)
2799 ga_append(&b->bv_ga, blob_get(b1, i));
2800 for (i = 0; i < blob_len(b2); i++)
2801 ga_append(&b->bv_ga, blob_get(b2, i));
2802
2803 clear_tv(tv1);
2804 rettv_blob_set(tv1, b);
2805 }
2806}
2807
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002808/*
2809 * Make a copy of list "tv1" and append list "tv2".
2810 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002811 int
2812eval_addlist(typval_T *tv1, typval_T *tv2)
2813{
2814 typval_T var3;
2815
2816 // concatenate Lists
2817 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2818 {
2819 clear_tv(tv1);
2820 clear_tv(tv2);
2821 return FAIL;
2822 }
2823 clear_tv(tv1);
2824 *tv1 = var3;
2825 return OK;
2826}
2827
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828/*
2829 * Handle fourth level expression:
2830 * + number addition
2831 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002832 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002833 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 *
2835 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002836 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 *
2838 * Return OK or FAIL.
2839 */
2840 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002841eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 /*
2844 * Get the first variable.
2845 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002846 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 return FAIL;
2848
2849 /*
2850 * Repeat computing, until no '+', '-' or '.' is following.
2851 */
2852 for (;;)
2853 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002854 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002855 int getnext;
2856 char_u *p;
2857 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002858 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002859 int concat;
2860 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002861 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002862
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002863 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002864 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002865 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002866 p = eval_next_non_blank(*arg, evalarg, &getnext);
2867 op = *p;
2868 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002869 if ((op != '+' && op != '-' && !concat) || p[1] == '='
2870 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002872 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
2873 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002874
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002875 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002876 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002877 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002878 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002879 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002880 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002881 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002882 {
mityu4ac198c2021-05-28 17:52:40 +02002883 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002884 clear_tv(rettv);
2885 return FAIL;
2886 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002887 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002888 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002889 if ((op != '+' || (rettv->v_type != VAR_LIST
2890 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002891#ifdef FEAT_FLOAT
2892 && (op == '.' || rettv->v_type != VAR_FLOAT)
2893#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002894 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002895 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002896 int error = FALSE;
2897
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002898 // For "list + ...", an illegal use of the first operand as
2899 // a number cannot be determined before evaluating the 2nd
2900 // operand: if this is also a list, all is ok.
2901 // For "something . ...", "something - ..." or "non-list + ...",
2902 // we know that the first operand needs to be a string or number
2903 // without evaluating the 2nd operand. So check before to avoid
2904 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002905 if (op != '.')
2906 tv_get_number_chk(rettv, &error);
2907 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002908 {
2909 clear_tv(rettv);
2910 return FAIL;
2911 }
2912 }
2913
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 /*
2915 * Get the second variable.
2916 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002917 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002918 {
mityu89dcb4d2021-05-28 13:50:17 +02002919 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002920 clear_tv(rettv);
2921 return FAIL;
2922 }
2923 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002924 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002926 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 return FAIL;
2928 }
2929
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002930 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 {
2932 /*
2933 * Compute the result.
2934 */
2935 if (op == '.')
2936 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002937 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2938 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002939 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002940
Bram Moolenaar2e086612020-08-25 22:37:48 +02002941 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002942 || var2.v_type == VAR_CHANNEL
2943 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02002944 semsg(_(e_using_invalid_value_as_string_str),
2945 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002946#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02002947 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002948 {
2949 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2950 var2.vval.v_float);
2951 s2 = buf2;
2952 }
2953#endif
2954 else
2955 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002956 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002957 {
2958 clear_tv(rettv);
2959 clear_tv(&var2);
2960 return FAIL;
2961 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002962 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002963 clear_tv(rettv);
2964 rettv->v_type = VAR_STRING;
2965 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002967 else if (op == '+' && rettv->v_type == VAR_BLOB
2968 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002969 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002970 else if (op == '+' && rettv->v_type == VAR_LIST
2971 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002972 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002973 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002974 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 else
2977 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002978 int error = FALSE;
2979 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002980#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002981 float_T f1 = 0, f2 = 0;
2982
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002983 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002984 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002985 f1 = rettv->vval.v_float;
2986 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002987 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002988 else
2989#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002990 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002991 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002992 if (error)
2993 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02002994 // This can only happen for "list + non-list" or
2995 // "blob + non-blob". For "non-list + ..." or
2996 // "something - ...", we returned before evaluating the
2997 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002998 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02002999 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003000 return FAIL;
3001 }
3002#ifdef FEAT_FLOAT
3003 if (var2.v_type == VAR_FLOAT)
3004 f1 = n1;
3005#endif
3006 }
3007#ifdef FEAT_FLOAT
3008 if (var2.v_type == VAR_FLOAT)
3009 {
3010 f2 = var2.vval.v_float;
3011 n2 = 0;
3012 }
3013 else
3014#endif
3015 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003016 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003017 if (error)
3018 {
3019 clear_tv(rettv);
3020 clear_tv(&var2);
3021 return FAIL;
3022 }
3023#ifdef FEAT_FLOAT
3024 if (rettv->v_type == VAR_FLOAT)
3025 f2 = n2;
3026#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003027 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003028 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003029
3030#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003031 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003032 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3033 {
3034 if (op == '+')
3035 f1 = f1 + f2;
3036 else
3037 f1 = f1 - f2;
3038 rettv->v_type = VAR_FLOAT;
3039 rettv->vval.v_float = f1;
3040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003042#endif
3043 {
3044 if (op == '+')
3045 n1 = n1 + n2;
3046 else
3047 n1 = n1 - n2;
3048 rettv->v_type = VAR_NUMBER;
3049 rettv->vval.v_number = n1;
3050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003052 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 }
3054 }
3055 return OK;
3056}
3057
3058/*
3059 * Handle fifth level expression:
3060 * * number multiplication
3061 * / number division
3062 * % number modulo
3063 *
3064 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003065 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 *
3067 * Return OK or FAIL.
3068 */
3069 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003070eval6(
3071 char_u **arg,
3072 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003073 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003074 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003076#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003077 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003078#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079
3080 /*
3081 * Get the first variable.
3082 */
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003083 if (eval7t(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 return FAIL;
3085
3086 /*
3087 * Repeat computing, until no '*', '/' or '%' is following.
3088 */
3089 for (;;)
3090 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003091 int evaluate;
3092 int getnext;
3093 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003094 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003095 int op;
3096 varnumber_T n1, n2;
3097#ifdef FEAT_FLOAT
3098 float_T f1, f2;
3099#endif
3100 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003101
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003102 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003103 p = eval_next_non_blank(*arg, evalarg, &getnext);
3104 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003105 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003107
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003108 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003109 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003110 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003111 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003112 {
3113 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3114 {
mityu4ac198c2021-05-28 17:52:40 +02003115 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003116 clear_tv(rettv);
3117 return FAIL;
3118 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003119 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003122#ifdef FEAT_FLOAT
3123 f1 = 0;
3124 f2 = 0;
3125#endif
3126 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003127 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003129#ifdef FEAT_FLOAT
3130 if (rettv->v_type == VAR_FLOAT)
3131 {
3132 f1 = rettv->vval.v_float;
3133 use_float = TRUE;
3134 n1 = 0;
3135 }
3136 else
3137#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003138 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003139 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003140 if (error)
3141 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 }
3143 else
3144 n1 = 0;
3145
3146 /*
3147 * Get the second variable.
3148 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003149 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3150 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003151 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003152 clear_tv(rettv);
3153 return FAIL;
3154 }
3155 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003156 if (eval7t(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 return FAIL;
3158
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003159 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003161#ifdef FEAT_FLOAT
3162 if (var2.v_type == VAR_FLOAT)
3163 {
3164 if (!use_float)
3165 {
3166 f1 = n1;
3167 use_float = TRUE;
3168 }
3169 f2 = var2.vval.v_float;
3170 n2 = 0;
3171 }
3172 else
3173#endif
3174 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003175 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003176 clear_tv(&var2);
3177 if (error)
3178 return FAIL;
3179#ifdef FEAT_FLOAT
3180 if (use_float)
3181 f2 = n2;
3182#endif
3183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184
3185 /*
3186 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003187 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003189#ifdef FEAT_FLOAT
3190 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003192 if (op == '*')
3193 f1 = f1 * f2;
3194 else if (op == '/')
3195 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003196# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003197 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003198 if (f2 == 0.0)
3199 {
3200 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003201 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003202 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003203 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003204 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003205 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003206 }
3207 else
3208 f1 = f1 / f2;
3209# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003210 // We rely on the floating point library to handle divide
3211 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003212 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003213# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003216 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003217 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003218 return FAIL;
3219 }
3220 rettv->v_type = VAR_FLOAT;
3221 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 }
3223 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003226 int failed = FALSE;
3227
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003228 if (op == '*')
3229 n1 = n1 * n2;
3230 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003231 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003233 n1 = num_modulus(n1, n2, &failed);
3234 if (failed)
3235 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003236
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003237 rettv->v_type = VAR_NUMBER;
3238 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 }
3241 }
3242
3243 return OK;
3244}
3245
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003246/*
3247 * Handle a type cast before a base level expression.
3248 * "arg" must point to the first non-white of the expression.
3249 * "arg" is advanced to just after the recognized expression.
3250 * Return OK or FAIL.
3251 */
3252 static int
3253eval7t(
3254 char_u **arg,
3255 typval_T *rettv,
3256 evalarg_T *evalarg,
3257 int want_string) // after "." operator
3258{
3259 type_T *want_type = NULL;
3260 garray_T type_list; // list of pointers to allocated types
3261 int res;
3262 int evaluate = evalarg == NULL ? 0
3263 : (evalarg->eval_flags & EVAL_EVALUATE);
3264
3265 // Recognize <type> in Vim9 script only.
3266 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1]))
3267 {
3268 ++*arg;
3269 ga_init2(&type_list, sizeof(type_T *), 10);
3270 want_type = parse_type(arg, &type_list, TRUE);
3271 if (want_type == NULL && (evaluate || **arg != '>'))
3272 {
3273 clear_type_list(&type_list);
3274 return FAIL;
3275 }
3276
3277 if (**arg != '>')
3278 {
3279 if (*skipwhite(*arg) == '>')
3280 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3281 else
3282 emsg(_(e_missing_gt));
3283 clear_type_list(&type_list);
3284 return FAIL;
3285 }
3286 ++*arg;
3287 *arg = skipwhite_and_linebreak(*arg, evalarg);
3288 }
3289
3290 res = eval7(arg, rettv, evalarg, want_string);
3291
3292 if (want_type != NULL && evaluate)
3293 {
3294 if (res == OK)
3295 {
3296 type_T *actual = typval2type(rettv, get_copyID(), &type_list, TRUE);
3297
3298 if (!equal_type(want_type, actual))
3299 {
3300 if (want_type == &t_bool && actual != &t_bool
3301 && (actual->tt_flags & TTFLAG_BOOL_OK))
3302 {
3303 int n = tv2bool(rettv);
3304
3305 // can use "0" and "1" for boolean in some places
3306 clear_tv(rettv);
3307 rettv->v_type = VAR_BOOL;
3308 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3309 }
3310 else
3311 {
3312 where_T where;
3313
3314 where.wt_index = 0;
3315 where.wt_variable = TRUE;
3316 res = check_type(want_type, actual, TRUE, where);
3317 }
3318 }
3319 }
3320 clear_type_list(&type_list);
3321 }
3322
3323 return res;
3324}
3325
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003326 int
3327eval_leader(char_u **arg, int vim9)
3328{
3329 char_u *s = *arg;
3330 char_u *p = *arg;
3331
3332 while (*p == '!' || *p == '-' || *p == '+')
3333 {
3334 char_u *n = skipwhite(p + 1);
3335
3336 // ++, --, -+ and +- are not accepted in Vim9 script
3337 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3338 {
3339 semsg(_(e_invexpr2), s);
3340 return FAIL;
3341 }
3342 p = n;
3343 }
3344 *arg = p;
3345 return OK;
3346}
3347
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348/*
3349 * Handle sixth level expression:
3350 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003351 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003352 * "string" string constant
3353 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 * &option-name option value
3355 * @r register contents
3356 * identifier variable value
3357 * function() function call
3358 * $VAR environment variable
3359 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003360 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003361 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003362 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003363 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 *
3365 * Also handle:
3366 * ! in front logical NOT
3367 * - in front unary minus
3368 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003369 * trailing [] subscript in String or List
3370 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003371 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 *
3373 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003374 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 *
3376 * Return OK or FAIL.
3377 */
3378 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003379eval7(
3380 char_u **arg,
3381 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003382 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003383 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003385 int evaluate = evalarg != NULL
3386 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 int len;
3388 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 char_u *start_leader, *end_leader;
3390 int ret = OK;
3391 char_u *alias;
3392
3393 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003394 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003395 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003397 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398
3399 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003400 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 */
3402 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003403 if (eval_leader(arg, in_vim9script()) == FAIL)
3404 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 end_leader = *arg;
3406
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003407 if (**arg == '.' && (!isdigit(*(*arg + 1))
3408#ifdef FEAT_FLOAT
3409 || current_sctx.sc_version < 2
3410#endif
3411 ))
3412 {
3413 semsg(_(e_invexpr2), *arg);
3414 ++*arg;
3415 return FAIL;
3416 }
3417
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 switch (**arg)
3419 {
3420 /*
3421 * Number constant.
3422 */
3423 case '0':
3424 case '1':
3425 case '2':
3426 case '3':
3427 case '4':
3428 case '5':
3429 case '6':
3430 case '7':
3431 case '8':
3432 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003433 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003434
3435 // Apply prefixed "-" and "+" now. Matters especially when
3436 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003437 if (ret == OK && evaluate && end_leader > start_leader
3438 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003439 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 break;
3441
3442 /*
3443 * String constant: "string".
3444 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003445 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 break;
3447
3448 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003449 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003451 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003452 break;
3453
3454 /*
3455 * List: [expr, expr]
3456 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003457 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 break;
3459
3460 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003461 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003462 */
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003463 case '#': if (in_vim9script())
3464 {
3465 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3466 }
3467 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003468 {
3469 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003470 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003471 }
3472 else
3473 ret = NOTDONE;
3474 break;
3475
3476 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003477 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003478 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003479 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003480 case '{': if (in_vim9script())
3481 ret = NOTDONE;
3482 else
3483 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003484 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003485 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003486 break;
3487
3488 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003489 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003491 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 break;
3493
3494 /*
3495 * Environment variable: $VAR.
3496 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003497 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 break;
3499
3500 /*
3501 * Register contents: @r.
3502 */
3503 case '@': ++*arg;
3504 if (evaluate)
3505 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003506 if (in_vim9script() && IS_WHITE_OR_NUL(**arg))
3507 semsg(_(e_syntax_error_at_str), *arg);
3508 else if (in_vim9script() && !valid_yank_reg(**arg, FALSE))
3509 emsg_invreg(**arg);
3510 else
3511 {
3512 rettv->v_type = VAR_STRING;
3513 rettv->vval.v_string = get_reg_contents(**arg,
3514 GREG_EXPR_SRC);
3515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 }
3517 if (**arg != NUL)
3518 ++*arg;
3519 break;
3520
3521 /*
3522 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003523 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003525 case '(': ret = NOTDONE;
3526 if (in_vim9script())
Bram Moolenaar06409502021-02-17 17:00:27 +01003527 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003528 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003529 if (ret == OK && evaluate)
3530 {
3531 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3532
3533 // compile it here to get the return type
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003534 if (compile_def_function(ufunc,
3535 TRUE, PROFILING(ufunc), NULL) == FAIL)
3536 {
3537 clear_tv(rettv);
3538 ret = FAIL;
3539 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003540 }
3541 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003542 if (ret == NOTDONE)
3543 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003544 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003545 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003546
Bram Moolenaar9215f012020-06-27 21:18:00 +02003547 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003548 if (**arg == ')')
3549 ++*arg;
3550 else if (ret == OK)
3551 {
3552 emsg(_(e_missing_close));
3553 clear_tv(rettv);
3554 ret = FAIL;
3555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 }
3557 break;
3558
Bram Moolenaar8c711452005-01-14 21:53:12 +00003559 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 break;
3561 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003562
3563 if (ret == NOTDONE)
3564 {
3565 /*
3566 * Must be a variable or function name.
3567 * Can also be a curly-braces kind of name: {expr}.
3568 */
3569 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003570 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003571 if (alias != NULL)
3572 s = alias;
3573
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003574 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003575 ret = FAIL;
3576 else
3577 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003578 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3579
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003580 if (evaluate && in_vim9script() && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003581 {
3582 emsg(_(e_cannot_use_underscore_here));
3583 ret = FAIL;
3584 }
3585 else if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003586 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003587 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003588 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003589 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003590 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003591 else if (flags & EVAL_CONSTANT)
3592 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003593 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003594 {
3595 // get the value of "true", "false" or a variable
3596 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3597 {
3598 rettv->v_type = VAR_BOOL;
3599 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003600 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003601 }
3602 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003603 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003604 {
3605 rettv->v_type = VAR_BOOL;
3606 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003607 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003608 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003609 else if (len == 4 && in_vim9script()
3610 && STRNCMP(s, "null", 4) == 0)
3611 {
3612 rettv->v_type = VAR_SPECIAL;
3613 rettv->vval.v_number = VVAL_NULL;
3614 ret = OK;
3615 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003616 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003617 ret = eval_variable(s, len, rettv, NULL,
3618 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003619 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003620 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003621 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003622 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003623 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003624 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003625 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003626 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003627 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003628 }
3629
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003630 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3631 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003632 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003633 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634
3635 /*
3636 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3637 */
3638 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003639 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003640 return ret;
3641}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003642
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003643/*
3644 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003645 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003646 * Adjusts "end_leaderp" until it is at "start_leader".
3647 */
3648 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003649eval7_leader(
3650 typval_T *rettv,
3651 int numeric_only,
3652 char_u *start_leader,
3653 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003654{
3655 char_u *end_leader = *end_leaderp;
3656 int ret = OK;
3657 int error = FALSE;
3658 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003659 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003660#ifdef FEAT_FLOAT
3661 float_T f = 0.0;
3662
3663 if (rettv->v_type == VAR_FLOAT)
3664 f = rettv->vval.v_float;
3665 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003666#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003667 {
3668 while (VIM_ISWHITE(end_leader[-1]))
3669 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003670 if (in_vim9script() && end_leader[-1] == '!')
3671 val = tv2bool(rettv);
3672 else
3673 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003674 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003675 if (error)
3676 {
3677 clear_tv(rettv);
3678 ret = FAIL;
3679 }
3680 else
3681 {
3682 while (end_leader > start_leader)
3683 {
3684 --end_leader;
3685 if (*end_leader == '!')
3686 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003687 if (numeric_only)
3688 {
3689 ++end_leader;
3690 break;
3691 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003692#ifdef FEAT_FLOAT
3693 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003694 {
3695 if (in_vim9script())
3696 {
3697 rettv->v_type = VAR_BOOL;
3698 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3699 }
3700 else
3701 f = !f;
3702 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003703 else
3704#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003705 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003706 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003707 type = VAR_BOOL;
3708 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003709 }
3710 else if (*end_leader == '-')
3711 {
3712#ifdef FEAT_FLOAT
3713 if (rettv->v_type == VAR_FLOAT)
3714 f = -f;
3715 else
3716#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003717 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003718 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003719 type = VAR_NUMBER;
3720 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003721 }
3722 }
3723#ifdef FEAT_FLOAT
3724 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003726 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003727 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003729 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003730#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003731 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003732 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003733 if (in_vim9script())
3734 rettv->v_type = type;
3735 else
3736 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003737 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003740 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 return ret;
3742}
3743
3744/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003745 * Call the function referred to in "rettv".
3746 */
3747 static int
3748call_func_rettv(
3749 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003750 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003751 typval_T *rettv,
3752 int evaluate,
3753 dict_T *selfdict,
3754 typval_T *basetv)
3755{
3756 partial_T *pt = NULL;
3757 funcexe_T funcexe;
3758 typval_T functv;
3759 char_u *s;
3760 int ret;
3761
3762 // need to copy the funcref so that we can clear rettv
3763 if (evaluate)
3764 {
3765 functv = *rettv;
3766 rettv->v_type = VAR_UNKNOWN;
3767
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003768 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003769 if (functv.v_type == VAR_PARTIAL)
3770 {
3771 pt = functv.vval.v_partial;
3772 s = partial_name(pt);
3773 }
3774 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003775 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003776 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003777 if (s == NULL || *s == NUL)
3778 {
3779 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02003780 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003781 goto theend;
3782 }
3783 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003784 }
3785 else
3786 s = (char_u *)"";
3787
Bram Moolenaara80faa82020-04-12 19:37:17 +02003788 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003789 funcexe.firstline = curwin->w_cursor.lnum;
3790 funcexe.lastline = curwin->w_cursor.lnum;
3791 funcexe.evaluate = evaluate;
3792 funcexe.partial = pt;
3793 funcexe.selfdict = selfdict;
3794 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003795 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003796
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003797theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003798 // Clear the funcref afterwards, so that deleting it while
3799 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003800 if (evaluate)
3801 clear_tv(&functv);
3802
3803 return ret;
3804}
3805
3806/*
3807 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003808 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003809 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3810 */
3811 static int
3812eval_lambda(
3813 char_u **arg,
3814 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003815 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003816 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003817{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003818 int evaluate = evalarg != NULL
3819 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003820 typval_T base = *rettv;
3821 int ret;
3822
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003823 rettv->v_type = VAR_UNKNOWN;
3824
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003825 if (**arg == '{')
3826 {
3827 // ->{lambda}()
3828 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3829 }
3830 else
3831 {
3832 // ->(lambda)()
3833 ++*arg;
3834 ret = eval1(arg, rettv, evalarg);
3835 *arg = skipwhite_and_linebreak(*arg, evalarg);
3836 if (**arg != ')')
3837 {
3838 emsg(_(e_missing_close));
3839 ret = FAIL;
3840 }
3841 ++*arg;
3842 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003843 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003844 return FAIL;
3845 else if (**arg != '(')
3846 {
3847 if (verbose)
3848 {
3849 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003850 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003851 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003852 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003853 }
3854 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003855 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003856 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003857 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003858 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003859
3860 // Clear the funcref afterwards, so that deleting it while
3861 // evaluating the arguments is possible (see test55).
3862 if (evaluate)
3863 clear_tv(&base);
3864
3865 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003866}
3867
3868/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003869 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003870 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02003871 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3872 */
3873 static int
3874eval_method(
3875 char_u **arg,
3876 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003877 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003878 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003879{
3880 char_u *name;
3881 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003882 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003883 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003884 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003885 int evaluate = evalarg != NULL
3886 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003887
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003888 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003889
Bram Moolenaarac92e252019-08-03 21:58:38 +02003890 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003891 len = get_name_len(arg, &alias, evaluate, TRUE);
3892 if (alias != NULL)
3893 name = alias;
3894
3895 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003896 {
3897 if (verbose)
3898 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003899 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003900 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003901 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003902 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003903 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003904 if (**arg != '(')
3905 {
3906 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003907 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003908 ret = FAIL;
3909 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003910 else if (VIM_ISWHITE((*arg)[-1]))
3911 {
3912 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003913 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003914 ret = FAIL;
3915 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003916 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003917 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003918 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003919 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003920
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003921 // Clear the funcref afterwards, so that deleting it while
3922 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003923 if (evaluate)
3924 clear_tv(&base);
3925
Bram Moolenaarac92e252019-08-03 21:58:38 +02003926 return ret;
3927}
3928
3929/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003930 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3931 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003932 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3933 */
3934 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003935eval_index(
3936 char_u **arg,
3937 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003938 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003939 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003940{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003941 int evaluate = evalarg != NULL
3942 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003943 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003944 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003945 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003946 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003947 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003948 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003949
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003950 if (check_can_index(rettv, evaluate, verbose) == FAIL)
3951 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003952
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003953 init_tv(&var1);
3954 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003955 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003956 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003957 /*
3958 * dict.name
3959 */
3960 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003961 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003962 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003963 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003964 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02003965 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003966 }
3967 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003968 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003969 /*
3970 * something[idx]
3971 *
3972 * Get the (first) variable from inside the [].
3973 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003974 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003975 if (**arg == ':')
3976 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003977 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003978 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003979 else if (vim9 && **arg == ':')
3980 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003981 semsg(_(e_white_space_required_before_and_after_str_at_str),
3982 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003983 clear_tv(&var1);
3984 return FAIL;
3985 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003986 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003987 {
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01003988#ifdef FEAT_FLOAT
3989 // allow for indexing with float
3990 if (vim9 && rettv->v_type == VAR_DICT
3991 && var1.v_type == VAR_FLOAT)
3992 {
3993 var1.vval.v_string = typval_tostring(&var1, TRUE);
3994 var1.v_type = VAR_STRING;
3995 }
3996#endif
3997 if (tv_get_string_chk(&var1) == NULL)
3998 {
3999 // not a number or string
4000 clear_tv(&var1);
4001 return FAIL;
4002 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004003 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004004
4005 /*
4006 * Get the second variable from inside the [:].
4007 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004008 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004009 if (**arg == ':')
4010 {
4011 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004012 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01004013 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004014 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004015 semsg(_(e_white_space_required_before_and_after_str_at_str),
4016 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004017 if (!empty1)
4018 clear_tv(&var1);
4019 return FAIL;
4020 }
4021 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004022 if (**arg == ']')
4023 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004024 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004025 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004026 if (!empty1)
4027 clear_tv(&var1);
4028 return FAIL;
4029 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004030 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004031 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004032 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004033 if (!empty1)
4034 clear_tv(&var1);
4035 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004036 return FAIL;
4037 }
4038 }
4039
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004040 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004041 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004042 if (**arg != ']')
4043 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004044 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004045 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004046 clear_tv(&var1);
4047 if (range)
4048 clear_tv(&var2);
4049 return FAIL;
4050 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004051 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004052 }
4053
4054 if (evaluate)
4055 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004056 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004057 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004058 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004059
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004060 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004061 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004062 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004063 clear_tv(&var2);
4064 return res;
4065 }
4066 return OK;
4067}
4068
4069/*
4070 * Check if "rettv" can have an [index] or [sli:ce]
4071 */
4072 int
4073check_can_index(typval_T *rettv, int evaluate, int verbose)
4074{
4075 switch (rettv->v_type)
4076 {
4077 case VAR_FUNC:
4078 case VAR_PARTIAL:
4079 if (verbose)
4080 emsg(_("E695: Cannot index a Funcref"));
4081 return FAIL;
4082 case VAR_FLOAT:
4083#ifdef FEAT_FLOAT
4084 if (verbose)
4085 emsg(_(e_float_as_string));
4086 return FAIL;
4087#endif
4088 case VAR_BOOL:
4089 case VAR_SPECIAL:
4090 case VAR_JOB:
4091 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004092 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004093 if (verbose)
4094 emsg(_(e_cannot_index_special_variable));
4095 return FAIL;
4096 case VAR_UNKNOWN:
4097 case VAR_ANY:
4098 case VAR_VOID:
4099 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004100 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004101 emsg(_(e_cannot_index_special_variable));
4102 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004103 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004104 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004105
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004106 case VAR_STRING:
4107 case VAR_LIST:
4108 case VAR_DICT:
4109 case VAR_BLOB:
4110 break;
4111 case VAR_NUMBER:
4112 if (in_vim9script())
4113 emsg(_(e_cannot_index_number));
4114 break;
4115 }
4116 return OK;
4117}
4118
4119/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004120 * slice() function
4121 */
4122 void
4123f_slice(typval_T *argvars, typval_T *rettv)
4124{
4125 if (check_can_index(argvars, TRUE, FALSE) == OK)
4126 {
4127 copy_tv(argvars, rettv);
4128 eval_index_inner(rettv, TRUE, argvars + 1,
4129 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4130 TRUE, NULL, 0, FALSE);
4131 }
4132}
4133
4134/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004135 * Apply index or range to "rettv".
4136 * "var1" is the first index, NULL for [:expr].
4137 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004138 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4139 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004140 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4141 */
4142 int
4143eval_index_inner(
4144 typval_T *rettv,
4145 int is_range,
4146 typval_T *var1,
4147 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004148 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004149 char_u *key,
4150 int keylen,
4151 int verbose)
4152{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004153 varnumber_T n1, n2 = 0;
4154 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004155
4156 n1 = 0;
4157 if (var1 != NULL && rettv->v_type != VAR_DICT)
4158 n1 = tv_get_number(var1);
4159
4160 if (is_range)
4161 {
4162 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004163 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004164 if (verbose)
4165 emsg(_(e_cannot_slice_dictionary));
4166 return FAIL;
4167 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004168 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004169 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004170 else
4171 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004172 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004173
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004174 switch (rettv->v_type)
4175 {
4176 case VAR_UNKNOWN:
4177 case VAR_ANY:
4178 case VAR_VOID:
4179 case VAR_FUNC:
4180 case VAR_PARTIAL:
4181 case VAR_FLOAT:
4182 case VAR_BOOL:
4183 case VAR_SPECIAL:
4184 case VAR_JOB:
4185 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004186 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004187 break; // not evaluating, skipping over subscript
4188
4189 case VAR_NUMBER:
4190 case VAR_STRING:
4191 {
4192 char_u *s = tv_get_string(rettv);
4193
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004194 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004195 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004196 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004197 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004198 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004199 else
4200 s = char_from_string(s, n1);
4201 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004202 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004203 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004204 // The resulting variable is a substring. If the indexes
4205 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004206 if (n1 < 0)
4207 {
4208 n1 = len + n1;
4209 if (n1 < 0)
4210 n1 = 0;
4211 }
4212 if (n2 < 0)
4213 n2 = len + n2;
4214 else if (n2 >= len)
4215 n2 = len;
4216 if (n1 >= len || n2 < 0 || n1 > n2)
4217 s = NULL;
4218 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004219 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004220 }
4221 else
4222 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004223 // The resulting variable is a string of a single
4224 // character. If the index is too big or negative the
4225 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004226 if (n1 >= len || n1 < 0)
4227 s = NULL;
4228 else
4229 s = vim_strnsave(s + n1, 1);
4230 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004231 clear_tv(rettv);
4232 rettv->v_type = VAR_STRING;
4233 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004234 }
4235 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004236
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004237 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004238 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4239 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004240 break;
4241
4242 case VAR_LIST:
4243 if (var1 == NULL)
4244 n1 = 0;
4245 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004246 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004247 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004248 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004249 return FAIL;
4250 break;
4251
4252 case VAR_DICT:
4253 {
4254 dictitem_T *item;
4255 typval_T tmp;
4256
4257 if (key == NULL)
4258 {
4259 key = tv_get_string_chk(var1);
4260 if (key == NULL)
4261 return FAIL;
4262 }
4263
4264 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
4265
4266 if (item == NULL && verbose)
4267 semsg(_(e_dictkey), key);
4268 if (item == NULL)
4269 return FAIL;
4270
4271 copy_tv(&item->di_tv, &tmp);
4272 clear_tv(rettv);
4273 *rettv = tmp;
4274 }
4275 break;
4276 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004277 return OK;
4278}
4279
4280/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004281 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004282 */
4283 char_u *
4284partial_name(partial_T *pt)
4285{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004286 if (pt != NULL)
4287 {
4288 if (pt->pt_name != NULL)
4289 return pt->pt_name;
4290 if (pt->pt_func != NULL)
4291 return pt->pt_func->uf_name;
4292 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004293 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004294}
4295
Bram Moolenaarddecc252016-04-06 22:59:37 +02004296 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004297partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004298{
4299 int i;
4300
4301 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004302 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004303 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004304 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004305 if (pt->pt_name != NULL)
4306 {
4307 func_unref(pt->pt_name);
4308 vim_free(pt->pt_name);
4309 }
4310 else
4311 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004312
Bram Moolenaar54656012021-06-09 20:50:46 +02004313 // "out_up" is no longer used, decrement refcount on partial that owns it.
4314 partial_unref(pt->pt_outer.out_up_partial);
4315
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004316 // Decrease the reference count for the context of a closure. If down
4317 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004318 if (pt->pt_funcstack != NULL)
4319 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004320 --pt->pt_funcstack->fs_refcount;
4321 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004322 }
4323
Bram Moolenaarddecc252016-04-06 22:59:37 +02004324 vim_free(pt);
4325}
4326
4327/*
4328 * Unreference a closure: decrement the reference count and free it when it
4329 * becomes zero.
4330 */
4331 void
4332partial_unref(partial_T *pt)
4333{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004334 if (pt != NULL)
4335 {
4336 if (--pt->pt_refcount <= 0)
4337 partial_free(pt);
4338
4339 // If the reference count goes down to one, the funcstack may be the
4340 // only reference and can be freed if no other partials reference it.
4341 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4342 funcstack_check_refcount(pt->pt_funcstack);
4343 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004344}
4345
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004346/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004347 * Return the next (unique) copy ID.
4348 * Used for serializing nested structures.
4349 */
4350 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004351get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004352{
4353 current_copyID += COPYID_INC;
4354 return current_copyID;
4355}
4356
4357/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004358 * Garbage collection for lists and dictionaries.
4359 *
4360 * We use reference counts to be able to free most items right away when they
4361 * are no longer used. But for composite items it's possible that it becomes
4362 * unused while the reference count is > 0: When there is a recursive
4363 * reference. Example:
4364 * :let l = [1, 2, 3]
4365 * :let d = {9: l}
4366 * :let l[1] = d
4367 *
4368 * Since this is quite unusual we handle this with garbage collection: every
4369 * once in a while find out which lists and dicts are not referenced from any
4370 * variable.
4371 *
4372 * Here is a good reference text about garbage collection (refers to Python
4373 * but it applies to all reference-counting mechanisms):
4374 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004375 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004376
4377/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004378 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004379 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004380 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004381 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004382 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004383garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004384{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004385 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004386 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004387 buf_T *buf;
4388 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004389 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004390 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004391
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004392 if (!testing)
4393 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004394 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004395 want_garbage_collect = FALSE;
4396 may_garbage_collect = FALSE;
4397 garbage_collect_at_exit = FALSE;
4398 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004399
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004400 // The execution stack can grow big, limit the size.
4401 if (exestack.ga_maxlen - exestack.ga_len > 500)
4402 {
4403 size_t new_len;
4404 char_u *pp;
4405 int n;
4406
4407 // Keep 150% of the current size, with a minimum of the growth size.
4408 n = exestack.ga_len / 2;
4409 if (n < exestack.ga_growsize)
4410 n = exestack.ga_growsize;
4411
4412 // Don't make it bigger though.
4413 if (exestack.ga_len + n < exestack.ga_maxlen)
4414 {
4415 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4416 pp = vim_realloc(exestack.ga_data, new_len);
4417 if (pp == NULL)
4418 return FAIL;
4419 exestack.ga_maxlen = exestack.ga_len + n;
4420 exestack.ga_data = pp;
4421 }
4422 }
4423
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004424 // We advance by two because we add one for items referenced through
4425 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004426 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004427
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004428 /*
4429 * 1. Go through all accessible variables and mark all lists and dicts
4430 * with copyID.
4431 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004432
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004433 // Don't free variables in the previous_funccal list unless they are only
4434 // referenced through previous_funccal. This must be first, because if
4435 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004436 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004437
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004438 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004439 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004440
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004441 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004442 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004443 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4444 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004445
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004446 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004447 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004448 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4449 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004450 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004451 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4452 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004453#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004454 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004455 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4456 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004457 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004458 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004459 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4460 NULL, NULL);
4461#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004462
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004463 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004464 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004465 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4466 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004467 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004468 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004469
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004470 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004471 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004472
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004473 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004474 abort = abort || set_ref_in_functions(copyID);
4475
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004476 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004477 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004478
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004479 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004480 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004481
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004482 // callbacks in buffers
4483 abort = abort || set_ref_in_buffers(copyID);
4484
Bram Moolenaar1dced572012-04-05 16:54:08 +02004485#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004486 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004487#endif
4488
Bram Moolenaardb913952012-06-29 12:54:53 +02004489#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004490 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004491#endif
4492
4493#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004494 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004495#endif
4496
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004497#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004498 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004499 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004500#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004501#ifdef FEAT_NETBEANS_INTG
4502 abort = abort || set_ref_in_nb_channel(copyID);
4503#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004504
Bram Moolenaare3188e22016-05-31 21:13:04 +02004505#ifdef FEAT_TIMERS
4506 abort = abort || set_ref_in_timer(copyID);
4507#endif
4508
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004509#ifdef FEAT_QUICKFIX
4510 abort = abort || set_ref_in_quickfix(copyID);
4511#endif
4512
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004513#ifdef FEAT_TERMINAL
4514 abort = abort || set_ref_in_term(copyID);
4515#endif
4516
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004517#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004518 abort = abort || set_ref_in_popups(copyID);
4519#endif
4520
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004521 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004522 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004523 /*
4524 * 2. Free lists and dictionaries that are not referenced.
4525 */
4526 did_free = free_unref_items(copyID);
4527
4528 /*
4529 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004530 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004531 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004532 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004533 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004534 else if (p_verbose > 0)
4535 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004536 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004537 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004538
4539 return did_free;
4540}
4541
4542/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004543 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004544 */
4545 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004546free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004547{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004548 int did_free = FALSE;
4549
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004550 // Let all "free" functions know that we are here. This means no
4551 // dictionaries, lists, channels or jobs are to be freed, because we will
4552 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004553 in_free_unref_items = TRUE;
4554
4555 /*
4556 * PASS 1: free the contents of the items. We don't free the items
4557 * themselves yet, so that it is possible to decrement refcount counters
4558 */
4559
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004560 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004561 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004562
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004563 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004564 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004565
4566#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004567 // Go through the list of jobs and free items without the copyID. This
4568 // must happen before doing channels, because jobs refer to channels, but
4569 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004570 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4571
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004572 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004573 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4574#endif
4575
4576 /*
4577 * PASS 2: free the items themselves.
4578 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004579 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004580 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004581
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004582#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004583 // Go through the list of jobs and free items without the copyID. This
4584 // must happen before doing channels, because jobs refer to channels, but
4585 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004586 free_unused_jobs(copyID, COPYID_MASK);
4587
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004588 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004589 free_unused_channels(copyID, COPYID_MASK);
4590#endif
4591
4592 in_free_unref_items = FALSE;
4593
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004594 return did_free;
4595}
4596
4597/*
4598 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004599 * "list_stack" is used to add lists to be marked. Can be NULL.
4600 *
4601 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004602 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004603 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004604set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004605{
4606 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004607 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004608 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004609 hashtab_T *cur_ht;
4610 ht_stack_T *ht_stack = NULL;
4611 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004612
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004613 cur_ht = ht;
4614 for (;;)
4615 {
4616 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004617 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004618 // Mark each item in the hashtab. If the item contains a hashtab
4619 // it is added to ht_stack, if it contains a list it is added to
4620 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004621 todo = (int)cur_ht->ht_used;
4622 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4623 if (!HASHITEM_EMPTY(hi))
4624 {
4625 --todo;
4626 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4627 &ht_stack, list_stack);
4628 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004629 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004630
4631 if (ht_stack == NULL)
4632 break;
4633
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004634 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004635 cur_ht = ht_stack->ht;
4636 tempitem = ht_stack;
4637 ht_stack = ht_stack->prev;
4638 free(tempitem);
4639 }
4640
4641 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004642}
4643
4644/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004645 * Mark a dict and its items with "copyID".
4646 * Returns TRUE if setting references failed somehow.
4647 */
4648 int
4649set_ref_in_dict(dict_T *d, int copyID)
4650{
4651 if (d != NULL && d->dv_copyID != copyID)
4652 {
4653 d->dv_copyID = copyID;
4654 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4655 }
4656 return FALSE;
4657}
4658
4659/*
4660 * Mark a list and its items with "copyID".
4661 * Returns TRUE if setting references failed somehow.
4662 */
4663 int
4664set_ref_in_list(list_T *ll, int copyID)
4665{
4666 if (ll != NULL && ll->lv_copyID != copyID)
4667 {
4668 ll->lv_copyID = copyID;
4669 return set_ref_in_list_items(ll, copyID, NULL);
4670 }
4671 return FALSE;
4672}
4673
4674/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004675 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004676 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4677 *
4678 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004679 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004680 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004681set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004682{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004683 listitem_T *li;
4684 int abort = FALSE;
4685 list_T *cur_l;
4686 list_stack_T *list_stack = NULL;
4687 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004688
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004689 cur_l = l;
4690 for (;;)
4691 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004692 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004693 // Mark each item in the list. If the item contains a hashtab
4694 // it is added to ht_stack, if it contains a list it is added to
4695 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004696 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4697 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4698 ht_stack, &list_stack);
4699 if (list_stack == NULL)
4700 break;
4701
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004702 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004703 cur_l = list_stack->list;
4704 tempitem = list_stack;
4705 list_stack = list_stack->prev;
4706 free(tempitem);
4707 }
4708
4709 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004710}
4711
4712/*
4713 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004714 * "list_stack" is used to add lists to be marked. Can be NULL.
4715 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4716 *
4717 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004718 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004719 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004720set_ref_in_item(
4721 typval_T *tv,
4722 int copyID,
4723 ht_stack_T **ht_stack,
4724 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004725{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004726 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004727
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004728 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004729 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004730 dict_T *dd = tv->vval.v_dict;
4731
Bram Moolenaara03f2332016-02-06 18:09:59 +01004732 if (dd != NULL && dd->dv_copyID != copyID)
4733 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004734 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004735 dd->dv_copyID = copyID;
4736 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004737 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004738 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4739 }
4740 else
4741 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004742 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4743
Bram Moolenaara03f2332016-02-06 18:09:59 +01004744 if (newitem == NULL)
4745 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004746 else
4747 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004748 newitem->ht = &dd->dv_hashtab;
4749 newitem->prev = *ht_stack;
4750 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004751 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004752 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004753 }
4754 }
4755 else if (tv->v_type == VAR_LIST)
4756 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004757 list_T *ll = tv->vval.v_list;
4758
Bram Moolenaara03f2332016-02-06 18:09:59 +01004759 if (ll != NULL && ll->lv_copyID != copyID)
4760 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004761 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004762 ll->lv_copyID = copyID;
4763 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004764 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004765 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004766 }
4767 else
4768 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004769 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4770
Bram Moolenaara03f2332016-02-06 18:09:59 +01004771 if (newitem == NULL)
4772 abort = TRUE;
4773 else
4774 {
4775 newitem->list = ll;
4776 newitem->prev = *list_stack;
4777 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004778 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004779 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004780 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004781 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004782 else if (tv->v_type == VAR_FUNC)
4783 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004784 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004785 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004786 else if (tv->v_type == VAR_PARTIAL)
4787 {
4788 partial_T *pt = tv->vval.v_partial;
4789 int i;
4790
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004791 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004792 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004793 // Didn't see this partial yet.
4794 pt->pt_copyID = copyID;
4795
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004796 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004797
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004798 if (pt->pt_dict != NULL)
4799 {
4800 typval_T dtv;
4801
4802 dtv.v_type = VAR_DICT;
4803 dtv.vval.v_dict = pt->pt_dict;
4804 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4805 }
4806
4807 for (i = 0; i < pt->pt_argc; ++i)
4808 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4809 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004810 if (pt->pt_funcstack != NULL)
4811 {
4812 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4813
4814 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4815 abort = abort || set_ref_in_item(stack + i, copyID,
4816 ht_stack, list_stack);
4817 }
4818
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004819 }
4820 }
4821#ifdef FEAT_JOB_CHANNEL
4822 else if (tv->v_type == VAR_JOB)
4823 {
4824 job_T *job = tv->vval.v_job;
4825 typval_T dtv;
4826
4827 if (job != NULL && job->jv_copyID != copyID)
4828 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004829 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004830 if (job->jv_channel != NULL)
4831 {
4832 dtv.v_type = VAR_CHANNEL;
4833 dtv.vval.v_channel = job->jv_channel;
4834 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4835 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004836 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004837 {
4838 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004839 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004840 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4841 }
4842 }
4843 }
4844 else if (tv->v_type == VAR_CHANNEL)
4845 {
4846 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004847 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004848 typval_T dtv;
4849 jsonq_T *jq;
4850 cbq_T *cq;
4851
4852 if (ch != NULL && ch->ch_copyID != copyID)
4853 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004854 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004855 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004856 {
4857 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4858 jq = jq->jq_next)
4859 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4860 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4861 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004862 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004863 {
4864 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004865 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004866 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4867 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004868 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004869 {
4870 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004871 dtv.vval.v_partial =
4872 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004873 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4874 }
4875 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004876 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004877 {
4878 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004879 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004880 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4881 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004882 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004883 {
4884 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004885 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004886 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4887 }
4888 }
4889 }
4890#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004891 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004892}
4893
Bram Moolenaar8c711452005-01-14 21:53:12 +00004894/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004895 * Return a string with the string representation of a variable.
4896 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004897 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004898 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004899 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004900 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02004901 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004902 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4903 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004904 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004905 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004906 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004907echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004908 typval_T *tv,
4909 char_u **tofree,
4910 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004911 int copyID,
4912 int echo_style,
4913 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004914 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004915{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004916 static int recurse = 0;
4917 char_u *r = NULL;
4918
Bram Moolenaar33570922005-01-25 22:26:29 +00004919 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004920 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004921 if (!did_echo_string_emsg)
4922 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004923 // Only give this message once for a recursive call to avoid
4924 // flooding the user with errors. And stop iterating over lists
4925 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004926 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004927 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004928 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004929 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004930 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004931 }
4932 ++recurse;
4933
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004934 switch (tv->v_type)
4935 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004936 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004937 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004938 {
4939 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004940 r = tv->vval.v_string;
4941 if (r == NULL)
4942 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004943 }
4944 else
4945 {
4946 *tofree = string_quote(tv->vval.v_string, FALSE);
4947 r = *tofree;
4948 }
4949 break;
4950
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004951 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004952 if (echo_style)
4953 {
4954 *tofree = NULL;
4955 r = tv->vval.v_string;
4956 }
4957 else
4958 {
4959 *tofree = string_quote(tv->vval.v_string, TRUE);
4960 r = *tofree;
4961 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004962 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004963
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004964 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004965 {
4966 partial_T *pt = tv->vval.v_partial;
4967 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004968 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004969 garray_T ga;
4970 int i;
4971 char_u *tf;
4972
4973 ga_init2(&ga, 1, 100);
4974 ga_concat(&ga, (char_u *)"function(");
4975 if (fname != NULL)
4976 {
4977 ga_concat(&ga, fname);
4978 vim_free(fname);
4979 }
4980 if (pt != NULL && pt->pt_argc > 0)
4981 {
4982 ga_concat(&ga, (char_u *)", [");
4983 for (i = 0; i < pt->pt_argc; ++i)
4984 {
4985 if (i > 0)
4986 ga_concat(&ga, (char_u *)", ");
4987 ga_concat(&ga,
4988 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4989 vim_free(tf);
4990 }
4991 ga_concat(&ga, (char_u *)"]");
4992 }
4993 if (pt != NULL && pt->pt_dict != NULL)
4994 {
4995 typval_T dtv;
4996
4997 ga_concat(&ga, (char_u *)", ");
4998 dtv.v_type = VAR_DICT;
4999 dtv.vval.v_dict = pt->pt_dict;
5000 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5001 vim_free(tf);
5002 }
5003 ga_concat(&ga, (char_u *)")");
5004
5005 *tofree = ga.ga_data;
5006 r = *tofree;
5007 break;
5008 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005009
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005010 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005011 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005012 break;
5013
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005014 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005015 if (tv->vval.v_list == NULL)
5016 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005017 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005018 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005019 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005020 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005021 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5022 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005023 {
5024 *tofree = NULL;
5025 r = (char_u *)"[...]";
5026 }
5027 else
5028 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005029 int old_copyID = tv->vval.v_list->lv_copyID;
5030
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005031 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005032 *tofree = list2string(tv, copyID, restore_copyID);
5033 if (restore_copyID)
5034 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005035 r = *tofree;
5036 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005037 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005038
Bram Moolenaar8c711452005-01-14 21:53:12 +00005039 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005040 if (tv->vval.v_dict == NULL)
5041 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005042 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005043 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005044 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005045 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005046 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5047 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005048 {
5049 *tofree = NULL;
5050 r = (char_u *)"{...}";
5051 }
5052 else
5053 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005054 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005055
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005056 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005057 *tofree = dict2string(tv, copyID, restore_copyID);
5058 if (restore_copyID)
5059 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005060 r = *tofree;
5061 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005062 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005063
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005064 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005065 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005066 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005067 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005068 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005069 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005070 break;
5071
Bram Moolenaar835dc632016-02-07 14:27:38 +01005072 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005073 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005074#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005075 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005076 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5077 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005078 if (composite_val)
5079 {
5080 *tofree = string_quote(r, FALSE);
5081 r = *tofree;
5082 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005083#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005084 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005085
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005086 case VAR_INSTR:
5087 *tofree = NULL;
5088 r = (char_u *)"instructions";
5089 break;
5090
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005091 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005092#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005093 *tofree = NULL;
5094 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5095 r = numbuf;
5096 break;
5097#endif
5098
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005099 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005100 case VAR_SPECIAL:
5101 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005102 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005103 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005104 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005105
Bram Moolenaar8502c702014-06-17 12:51:16 +02005106 if (--recurse == 0)
5107 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005108 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005109}
5110
5111/*
5112 * Return a string with the string representation of a variable.
5113 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5114 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005115 * Does not put quotes around strings, as ":echo" displays values.
5116 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5117 * May return NULL.
5118 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005119 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005120echo_string(
5121 typval_T *tv,
5122 char_u **tofree,
5123 char_u *numbuf,
5124 int copyID)
5125{
5126 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5127}
5128
5129/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005130 * Return string "str" in ' quotes, doubling ' characters.
5131 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005132 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005133 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005134 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005135string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005136{
Bram Moolenaar33570922005-01-25 22:26:29 +00005137 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005138 char_u *p, *r, *s;
5139
Bram Moolenaar33570922005-01-25 22:26:29 +00005140 len = (function ? 13 : 3);
5141 if (str != NULL)
5142 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005143 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005144 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00005145 if (*p == '\'')
5146 ++len;
5147 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005148 s = r = alloc(len);
5149 if (r != NULL)
5150 {
5151 if (function)
5152 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005153 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005154 r += 10;
5155 }
5156 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005157 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005158 if (str != NULL)
5159 for (p = str; *p != NUL; )
5160 {
5161 if (*p == '\'')
5162 *r++ = '\'';
5163 MB_COPY_CHAR(p, r);
5164 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005165 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005166 if (function)
5167 *r++ = ')';
5168 *r++ = NUL;
5169 }
5170 return s;
5171}
5172
5173/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005174 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5175 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005176 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005177 */
5178 int
5179buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5180{
5181 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005182 char_u *t;
5183 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005184
5185 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5186 return -1;
5187
5188 if (lnum > buf->b_ml.ml_line_count)
5189 lnum = buf->b_ml.ml_line_count;
5190
5191 str = ml_get_buf(buf, lnum, FALSE);
5192 if (str == NULL)
5193 return -1;
5194
5195 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005196 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005197
Bram Moolenaar91458462021-01-13 20:08:38 +01005198 // count the number of characters
5199 t = str;
5200 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5201 t += mb_ptr2len(t);
5202
5203 // In insert mode, when the cursor is at the end of a non-empty line,
5204 // byteidx points to the NUL character immediately past the end of the
5205 // string. In this case, add one to the character count.
5206 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5207 count++;
5208
5209 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005210}
5211
5212/*
5213 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005214 * byte index. Works only for loaded buffers. Returns -1 on failure.
5215 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005216 */
5217 int
5218buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5219{
5220 char_u *str;
5221 char_u *t;
5222
5223 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5224 return -1;
5225
5226 if (lnum > buf->b_ml.ml_line_count)
5227 lnum = buf->b_ml.ml_line_count;
5228
5229 str = ml_get_buf(buf, lnum, FALSE);
5230 if (str == NULL)
5231 return -1;
5232
5233 // Convert the character offset to a byte offset
5234 t = str;
5235 while (*t != NUL && --charidx > 0)
5236 t += mb_ptr2len(t);
5237
Bram Moolenaar91458462021-01-13 20:08:38 +01005238 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005239}
5240
5241/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005243 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005245 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005246var2fpos(
5247 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005248 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005249 int *fnum, // set to fnum for '0, 'A, etc.
5250 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005252 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005254 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005256 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005257 if (varp->v_type == VAR_LIST)
5258 {
5259 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005260 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005261 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005262 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005263
5264 l = varp->vval.v_list;
5265 if (l == NULL)
5266 return NULL;
5267
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005268 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005269 pos.lnum = list_find_nr(l, 0L, &error);
5270 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005271 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005272 if (charcol)
5273 len = (long)mb_charlen(ml_get(pos.lnum));
5274 else
5275 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005276
Bram Moolenaarec65d772020-08-20 22:29:12 +02005277 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005278 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005279 li = list_find(l, 1L);
5280 if (li != NULL && li->li_tv.v_type == VAR_STRING
5281 && li->li_tv.vval.v_string != NULL
5282 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005283 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005284 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005285 }
5286 else
5287 {
5288 pos.col = list_find_nr(l, 1L, &error);
5289 if (error)
5290 return NULL;
5291 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005292
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005293 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005294 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005295 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005296 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005297
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005298 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005299 pos.coladd = list_find_nr(l, 2L, &error);
5300 if (error)
5301 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005302
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005303 return &pos;
5304 }
5305
Bram Moolenaarc5809432021-03-27 21:23:30 +01005306 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5307 return NULL;
5308
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005309 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005310 if (name == NULL)
5311 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005312 if (name[0] == '.') // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005313 {
5314 pos = curwin->w_cursor;
5315 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005316 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005317 return &pos;
5318 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005319 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005320 {
5321 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005322 pos = VIsual;
5323 else
5324 pos = curwin->w_cursor;
5325 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005326 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005327 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005328 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005329 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005331 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5333 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005334 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005335 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 return pp;
5337 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005338
Bram Moolenaara5525202006-03-02 22:52:09 +00005339 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005340
Bram Moolenaar477933c2007-07-17 14:32:23 +00005341 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005342 {
5343 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005344 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005345 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005346 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005347 // In silent Ex mode topline is zero, but that's not a valid line
5348 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005349 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005350 return &pos;
5351 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005352 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005353 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005354 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005355 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005356 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005357 return &pos;
5358 }
5359 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005360 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005362 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 {
5364 pos.lnum = curbuf->b_ml.ml_line_count;
5365 pos.col = 0;
5366 }
5367 else
5368 {
5369 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005370 if (charcol)
5371 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5372 else
5373 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 }
5375 return &pos;
5376 }
5377 return NULL;
5378}
5379
5380/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005381 * Convert list in "arg" into a position and optional file number.
5382 * When "fnump" is NULL there is no file number, only 3 items.
5383 * Note that the column is passed on as-is, the caller may want to decrement
5384 * it to use 1 for the first column.
5385 * Return FAIL when conversion is not possible, doesn't check the position for
5386 * validity.
5387 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005388 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005389list2fpos(
5390 typval_T *arg,
5391 pos_T *posp,
5392 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005393 colnr_T *curswantp,
5394 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005395{
5396 list_T *l = arg->vval.v_list;
5397 long i = 0;
5398 long n;
5399
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005400 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5401 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005402 if (arg->v_type != VAR_LIST
5403 || l == NULL
5404 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005405 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005406 return FAIL;
5407
5408 if (fnump != NULL)
5409 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005410 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005411 if (n < 0)
5412 return FAIL;
5413 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005414 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005415 *fnump = n;
5416 }
5417
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005418 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005419 if (n < 0)
5420 return FAIL;
5421 posp->lnum = n;
5422
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005423 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005424 if (n < 0)
5425 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005426 // If character position is specified, then convert to byte position
5427 if (charcol)
5428 {
5429 buf_T *buf;
5430
5431 // Get the text for the specified line in a loaded buffer
5432 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5433 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5434 return FAIL;
5435
Bram Moolenaar91458462021-01-13 20:08:38 +01005436 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005437 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005438 posp->col = n;
5439
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005440 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005441 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005442 posp->coladd = 0;
5443 else
5444 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005445
Bram Moolenaar493c1782014-05-28 14:34:46 +02005446 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005447 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005448
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005449 return OK;
5450}
5451
5452/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 * Get the length of an environment variable name.
5454 * Advance "arg" to the first character after the name.
5455 * Return 0 for error.
5456 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005457 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005458get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459{
5460 char_u *p;
5461 int len;
5462
5463 for (p = *arg; vim_isIDc(*p); ++p)
5464 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005465 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 return 0;
5467
5468 len = (int)(p - *arg);
5469 *arg = p;
5470 return len;
5471}
5472
5473/*
5474 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005475 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 * Return 0 if something is wrong.
5477 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005478 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005479get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480{
5481 char_u *p;
5482 int len;
5483
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005484 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005486 {
5487 if (*p == ':')
5488 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005489 // "s:" is start of "s:var", but "n:" is not and can be used in
5490 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005491 len = (int)(p - *arg);
5492 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5493 || len > 1)
5494 break;
5495 }
5496 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005497 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 return 0;
5499
5500 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005501 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502
5503 return len;
5504}
5505
5506/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005507 * Get the length of the name of a variable or function.
5508 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005510 * Return -1 if curly braces expansion failed.
5511 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 * If the name contains 'magic' {}'s, expand them and return the
5513 * expanded name in an allocated string via 'alias' - caller must free.
5514 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005515 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005516get_name_len(
5517 char_u **arg,
5518 char_u **alias,
5519 int evaluate,
5520 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521{
5522 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 char_u *p;
5524 char_u *expr_start;
5525 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005527 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528
5529 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5530 && (*arg)[2] == (int)KE_SNR)
5531 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005532 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 *arg += 3;
5534 return get_id_len(arg) + 3;
5535 }
5536 len = eval_fname_script(*arg);
5537 if (len > 0)
5538 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005539 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 *arg += len;
5541 }
5542
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005544 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005546 p = find_name_end(*arg, &expr_start, &expr_end,
5547 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 if (expr_start != NULL)
5549 {
5550 char_u *temp_string;
5551
5552 if (!evaluate)
5553 {
5554 len += (int)(p - *arg);
5555 *arg = skipwhite(p);
5556 return len;
5557 }
5558
5559 /*
5560 * Include any <SID> etc in the expanded string:
5561 * Thus the -len here.
5562 */
5563 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5564 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005565 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 *alias = temp_string;
5567 *arg = skipwhite(p);
5568 return (int)STRLEN(temp_string);
5569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570
5571 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005572 // Only give an error when there is something, otherwise it will be
5573 // reported at a higher level.
5574 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005575 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576
5577 return len;
5578}
5579
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005580/*
5581 * Find the end of a variable or function name, taking care of magic braces.
5582 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5583 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005584 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005585 * Return a pointer to just after the name. Equal to "arg" if there is no
5586 * valid name.
5587 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005588 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005589find_name_end(
5590 char_u *arg,
5591 char_u **expr_start,
5592 char_u **expr_end,
5593 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005595 int mb_nest = 0;
5596 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005598 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005599 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005601 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005603 *expr_start = NULL;
5604 *expr_end = NULL;
5605 }
5606
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005607 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005608 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5609 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005610 return arg;
5611
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005612 for (p = arg; *p != NUL
5613 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005614 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005615 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005616 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005617 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005618 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005619 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005620 if (*p == '\'')
5621 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005622 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005623 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005624 ;
5625 if (*p == NUL)
5626 break;
5627 }
5628 else if (*p == '"')
5629 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005630 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005631 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005632 if (*p == '\\' && p[1] != NUL)
5633 ++p;
5634 if (*p == NUL)
5635 break;
5636 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005637 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5638 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005639 // "s:" is start of "s:var", but "n:" is not and can be used in
5640 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005641 len = (int)(p - arg);
5642 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005643 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005644 break;
5645 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005646
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005647 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005649 if (*p == '[')
5650 ++br_nest;
5651 else if (*p == ']')
5652 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005654
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005655 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005657 if (*p == '{')
5658 {
5659 mb_nest++;
5660 if (expr_start != NULL && *expr_start == NULL)
5661 *expr_start = p;
5662 }
5663 else if (*p == '}')
5664 {
5665 mb_nest--;
5666 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5667 *expr_end = p;
5668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 }
5671
5672 return p;
5673}
5674
5675/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005676 * Expands out the 'magic' {}'s in a variable/function name.
5677 * Note that this can call itself recursively, to deal with
5678 * constructs like foo{bar}{baz}{bam}
5679 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5680 * "in_start" ^
5681 * "expr_start" ^
5682 * "expr_end" ^
5683 * "in_end" ^
5684 *
5685 * Returns a new allocated string, which the caller must free.
5686 * Returns NULL for failure.
5687 */
5688 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005689make_expanded_name(
5690 char_u *in_start,
5691 char_u *expr_start,
5692 char_u *expr_end,
5693 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005694{
5695 char_u c1;
5696 char_u *retval = NULL;
5697 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005698
5699 if (expr_end == NULL || in_end == NULL)
5700 return NULL;
5701 *expr_start = NUL;
5702 *expr_end = NUL;
5703 c1 = *in_end;
5704 *in_end = NUL;
5705
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005706 temp_result = eval_to_string(expr_start + 1, FALSE);
5707 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005708 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005709 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5710 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005711 if (retval != NULL)
5712 {
5713 STRCPY(retval, in_start);
5714 STRCAT(retval, temp_result);
5715 STRCAT(retval, expr_end + 1);
5716 }
5717 }
5718 vim_free(temp_result);
5719
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005720 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005721 *expr_start = '{';
5722 *expr_end = '}';
5723
5724 if (retval != NULL)
5725 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005726 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005727 if (expr_start != NULL)
5728 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005729 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005730 temp_result = make_expanded_name(retval, expr_start,
5731 expr_end, temp_result);
5732 vim_free(retval);
5733 retval = temp_result;
5734 }
5735 }
5736
5737 return retval;
5738}
5739
5740/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005742 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005744 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005745eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005747 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005748}
5749
5750/*
5751 * Return TRUE if character "c" can be used as the first character in a
5752 * variable or function name (excluding '{' and '}').
5753 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005754 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005755eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005756{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005757 return ASCII_ISALPHA(c) || c == '_';
5758}
5759
5760/*
5761 * Return TRUE if character "c" can be used as the first character of a
5762 * dictionary key.
5763 */
5764 int
5765eval_isdictc(int c)
5766{
5767 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768}
5769
5770/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005771 * Handle:
5772 * - expr[expr], expr[expr:expr] subscript
5773 * - ".name" lookup
5774 * - function call with Funcref variable: func(expr)
5775 * - method call: var->method()
5776 *
5777 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005778 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005779 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005780handle_subscript(
5781 char_u **arg,
5782 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005783 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005784 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005785{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005786 int evaluate = evalarg != NULL
5787 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005788 int ret = OK;
5789 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005790 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005791 int getnext;
5792 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005793
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005794 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005795 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005796 // When at the end of the line and ".name" or "->{" or "->X" follows in
5797 // the next line then consume the line break.
5798 p = eval_next_non_blank(*arg, evalarg, &getnext);
5799 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005800 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02005801 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
5802 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
5803 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005804 {
5805 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005806 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005807 check_white = FALSE;
5808 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005809
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01005810 if (rettv->v_type == VAR_ANY)
5811 {
5812 char_u *exp_name;
5813 int cc;
5814 int idx;
5815 ufunc_T *ufunc;
5816 type_T *type;
5817
5818 // Found script from "import * as {name}", script item name must
5819 // follow.
5820 if (**arg != '.')
5821 {
5822 if (verbose)
5823 semsg(_(e_expected_str_but_got_str), "'.'", *arg);
5824 ret = FAIL;
5825 break;
5826 }
5827 ++*arg;
5828 if (IS_WHITE_OR_NUL(**arg))
5829 {
5830 if (verbose)
5831 emsg(_(e_no_white_space_allowed_after_dot));
5832 ret = FAIL;
5833 break;
5834 }
5835
5836 // isolate the name
5837 exp_name = *arg;
5838 while (eval_isnamec(**arg))
5839 ++*arg;
5840 cc = **arg;
5841 **arg = NUL;
5842
5843 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
5844 evalarg->eval_cctx, verbose);
5845 **arg = cc;
5846 *arg = skipwhite(*arg);
5847
5848 if (idx < 0 && ufunc == NULL)
5849 {
5850 ret = FAIL;
5851 break;
5852 }
5853 if (idx >= 0)
5854 {
5855 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
5856 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
5857
5858 copy_tv(sv->sv_tv, rettv);
5859 }
5860 else
5861 {
5862 rettv->v_type = VAR_FUNC;
5863 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
5864 }
5865 }
5866
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005867 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5868 || rettv->v_type == VAR_PARTIAL))
5869 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005870 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005871 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5872 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005873
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005874 // Stop the expression evaluation when immediately aborting on
5875 // error, or when an interrupt occurred or an exception was thrown
5876 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005877 if (aborting())
5878 {
5879 if (ret == OK)
5880 clear_tv(rettv);
5881 ret = FAIL;
5882 }
5883 dict_unref(selfdict);
5884 selfdict = NULL;
5885 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005886 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005887 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005888 if (in_vim9script())
5889 *arg = skipwhite(p + 2);
5890 else
5891 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005892 if (ret == OK)
5893 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005894 if (VIM_ISWHITE(**arg))
5895 {
5896 emsg(_(e_nowhitespace));
5897 ret = FAIL;
5898 }
5899 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005900 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005901 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005902 else
5903 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005904 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005905 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005906 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005907 // "." is ".name" lookup when we found a dict or when evaluating and
5908 // scriptversion is at least 2, where string concatenation is "..".
5909 else if (**arg == '['
5910 || (**arg == '.' && (rettv->v_type == VAR_DICT
5911 || (!evaluate
5912 && (*arg)[1] != '.'
5913 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005914 {
5915 dict_unref(selfdict);
5916 if (rettv->v_type == VAR_DICT)
5917 {
5918 selfdict = rettv->vval.v_dict;
5919 if (selfdict != NULL)
5920 ++selfdict->dv_refcount;
5921 }
5922 else
5923 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005924 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005925 {
5926 clear_tv(rettv);
5927 ret = FAIL;
5928 }
5929 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005930 else
5931 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005932 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005933
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005934 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5935 // Don't do this when "Func" is already a partial that was bound
5936 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005937 if (selfdict != NULL
5938 && (rettv->v_type == VAR_FUNC
5939 || (rettv->v_type == VAR_PARTIAL
5940 && (rettv->vval.v_partial->pt_auto
5941 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005942 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005943
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005944 dict_unref(selfdict);
5945 return ret;
5946}
5947
5948/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005949 * Make a copy of an item.
5950 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005951 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5952 * reference to an already copied list/dict can be used.
5953 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005954 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005955 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005956item_copy(
5957 typval_T *from,
5958 typval_T *to,
5959 int deep,
5960 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005961{
5962 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005963 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005964
Bram Moolenaar33570922005-01-25 22:26:29 +00005965 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005966 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005967 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005968 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005969 }
5970 ++recurse;
5971
5972 switch (from->v_type)
5973 {
5974 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005975 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005976 case VAR_STRING:
5977 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005978 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005979 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005980 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005981 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005982 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005983 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005984 copy_tv(from, to);
5985 break;
5986 case VAR_LIST:
5987 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005988 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005989 if (from->vval.v_list == NULL)
5990 to->vval.v_list = NULL;
5991 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5992 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005993 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005994 to->vval.v_list = from->vval.v_list->lv_copylist;
5995 ++to->vval.v_list->lv_refcount;
5996 }
5997 else
5998 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5999 if (to->vval.v_list == NULL)
6000 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006001 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006002 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006003 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006004 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006005 case VAR_DICT:
6006 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006007 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006008 if (from->vval.v_dict == NULL)
6009 to->vval.v_dict = NULL;
6010 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6011 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006012 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006013 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6014 ++to->vval.v_dict->dv_refcount;
6015 }
6016 else
6017 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
6018 if (to->vval.v_dict == NULL)
6019 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006020 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006021 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006022 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006023 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006024 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006025 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006026 }
6027 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006028 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006029}
6030
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006031 void
6032echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6033{
6034 char_u *tofree;
6035 char_u numbuf[NUMBUFLEN];
6036 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6037
6038 if (*atstart)
6039 {
6040 *atstart = FALSE;
6041 // Call msg_start() after eval1(), evaluating the expression
6042 // may cause a message to appear.
6043 if (with_space)
6044 {
6045 // Mark the saved text as finishing the line, so that what
6046 // follows is displayed on a new line when scrolling back
6047 // at the more prompt.
6048 msg_sb_eol();
6049 msg_start();
6050 }
6051 }
6052 else if (with_space)
6053 msg_puts_attr(" ", echo_attr);
6054
6055 if (p != NULL)
6056 for ( ; *p != NUL && !got_int; ++p)
6057 {
6058 if (*p == '\n' || *p == '\r' || *p == TAB)
6059 {
6060 if (*p != TAB && *needclr)
6061 {
6062 // remove any text still there from the command
6063 msg_clr_eos();
6064 *needclr = FALSE;
6065 }
6066 msg_putchar_attr(*p, echo_attr);
6067 }
6068 else
6069 {
6070 if (has_mbyte)
6071 {
6072 int i = (*mb_ptr2len)(p);
6073
6074 (void)msg_outtrans_len_attr(p, i, echo_attr);
6075 p += i - 1;
6076 }
6077 else
6078 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6079 }
6080 }
6081 vim_free(tofree);
6082}
6083
Bram Moolenaare9a41262005-01-15 22:18:47 +00006084/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 * ":echo expr1 ..." print each argument separated with a space, add a
6086 * newline at the end.
6087 * ":echon expr1 ..." print each argument plain.
6088 */
6089 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006090ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091{
6092 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006093 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006094 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 int needclr = TRUE;
6096 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006097 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006098 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006099 evalarg_T evalarg;
6100
Bram Moolenaare707c882020-07-01 13:07:37 +02006101 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102
6103 if (eap->skip)
6104 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006105 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006107 // If eval1() causes an error message the text from the command may
6108 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006109 need_clr_eos = needclr;
6110
Bram Moolenaar68db9962021-05-09 23:19:22 +02006111 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006112 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 {
6114 /*
6115 * Report the invalid expression unless the expression evaluation
6116 * has been cancelled due to an aborting error, an interrupt, or an
6117 * exception.
6118 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006119 if (!aborting() && did_emsg == did_emsg_before
6120 && called_emsg == called_emsg_before)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006121 semsg(_(e_invexpr2), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006122 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 break;
6124 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006125 need_clr_eos = FALSE;
6126
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006128 {
6129 if (rettv.v_type == VAR_VOID)
6130 {
6131 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6132 break;
6133 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006134 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006135 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006136
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006137 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 arg = skipwhite(arg);
6139 }
6140 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006141 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142
6143 if (eap->skip)
6144 --emsg_skip;
6145 else
6146 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006147 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 if (needclr)
6149 msg_clr_eos();
6150 if (eap->cmdidx == CMD_echo)
6151 msg_end();
6152 }
6153}
6154
6155/*
6156 * ":echohl {name}".
6157 */
6158 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006159ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006161 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162}
6163
6164/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006165 * Returns the :echo attribute
6166 */
6167 int
6168get_echo_attr(void)
6169{
6170 return echo_attr;
6171}
6172
6173/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 * ":execute expr1 ..." execute the result of an expression.
6175 * ":echomsg expr1 ..." Print a message
6176 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006177 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 * Each gets spaces around each argument and a newline at the end for
6179 * echo commands
6180 */
6181 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006182ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183{
6184 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006185 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 int ret = OK;
6187 char_u *p;
6188 garray_T ga;
6189 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190
6191 ga_init2(&ga, 1, 80);
6192
6193 if (eap->skip)
6194 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006195 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006197 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006198 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200
6201 if (!eap->skip)
6202 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006203 char_u buf[NUMBUFLEN];
6204
6205 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006206 {
6207 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6208 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006209 semsg(_(e_using_invalid_value_as_string_str),
6210 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006211 p = NULL;
6212 }
6213 else
6214 p = tv_get_string_buf(&rettv, buf);
6215 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006216 else
6217 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006218 if (p == NULL)
6219 {
6220 clear_tv(&rettv);
6221 ret = FAIL;
6222 break;
6223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 len = (int)STRLEN(p);
6225 if (ga_grow(&ga, len + 2) == FAIL)
6226 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006227 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 ret = FAIL;
6229 break;
6230 }
6231 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 ga.ga_len += len;
6235 }
6236
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006237 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 arg = skipwhite(arg);
6239 }
6240
6241 if (ret != FAIL && ga.ga_data != NULL)
6242 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006243 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6244 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006245 // Mark the already saved text as finishing the line, so that what
6246 // follows is displayed on a new line when scrolling back at the
6247 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006248 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006249 }
6250
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006252 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006253 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006254 out_flush();
6255 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006256 else if (eap->cmdidx == CMD_echoconsole)
6257 {
6258 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6259 ui_write((char_u *)"\r\n", 2, TRUE);
6260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 else if (eap->cmdidx == CMD_echoerr)
6262 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006263 int save_did_emsg = did_emsg;
6264
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006265 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006266 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 if (!force_abort)
6268 did_emsg = save_did_emsg;
6269 }
6270 else if (eap->cmdidx == CMD_execute)
6271 do_cmdline((char_u *)ga.ga_data,
6272 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6273 }
6274
6275 ga_clear(&ga);
6276
6277 if (eap->skip)
6278 --emsg_skip;
6279
6280 eap->nextcmd = check_nextcmd(arg);
6281}
6282
6283/*
6284 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6285 * "arg" points to the "&" or '+' when called, to "option" when returning.
6286 * Returns NULL when no option name found. Otherwise pointer to the char
6287 * after the option name.
6288 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006289 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006290find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291{
6292 char_u *p = *arg;
6293
6294 ++p;
6295 if (*p == 'g' && p[1] == ':')
6296 {
6297 *opt_flags = OPT_GLOBAL;
6298 p += 2;
6299 }
6300 else if (*p == 'l' && p[1] == ':')
6301 {
6302 *opt_flags = OPT_LOCAL;
6303 p += 2;
6304 }
6305 else
6306 *opt_flags = 0;
6307
6308 if (!ASCII_ISALPHA(*p))
6309 return NULL;
6310 *arg = p;
6311
6312 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006313 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 else
6315 while (ASCII_ISALPHA(*p))
6316 ++p;
6317 return p;
6318}
6319
6320/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006321 * Display script name where an item was last set.
6322 * Should only be invoked when 'verbose' is non-zero.
6323 */
6324 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006325last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006326{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006327 char_u *p;
6328
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006329 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006330 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006331 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006332 if (p != NULL)
6333 {
6334 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006335 msg_puts(_("\n\tLast set from "));
6336 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006337 if (script_ctx.sc_lnum > 0)
6338 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006339 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006340 msg_outnum((long)script_ctx.sc_lnum);
6341 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006342 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006343 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006344 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006345 }
6346}
6347
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006348#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349
6350/*
6351 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006352 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 * "flags" can be "g" to do a global substitute.
6354 * Returns an allocated string, NULL for error.
6355 */
6356 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006357do_string_sub(
6358 char_u *str,
6359 char_u *pat,
6360 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006361 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006362 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363{
6364 int sublen;
6365 regmatch_T regmatch;
6366 int i;
6367 int do_all;
6368 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006369 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 garray_T ga;
6371 char_u *ret;
6372 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006373 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006375 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006377 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378
6379 ga_init2(&ga, 1, 200);
6380
6381 do_all = (flags[0] == 'g');
6382
6383 regmatch.rm_ic = p_ic;
6384 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6385 if (regmatch.regprog != NULL)
6386 {
6387 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006388 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6390 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006391 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006392 if (regmatch.startp[0] == regmatch.endp[0])
6393 {
6394 if (zero_width == regmatch.startp[0])
6395 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006396 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006397 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006398 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6399 (size_t)i);
6400 ga.ga_len += i;
6401 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006402 continue;
6403 }
6404 zero_width = regmatch.startp[0];
6405 }
6406
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 /*
6408 * Get some space for a temporary buffer to do the substitution
6409 * into. It will contain:
6410 * - The text up to where the match is.
6411 * - The substituted text.
6412 * - The text after the match.
6413 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006414 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006415 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6417 {
6418 ga_clear(&ga);
6419 break;
6420 }
6421
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006422 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 i = (int)(regmatch.startp[0] - tail);
6424 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006425 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006426 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 + ga.ga_len + i, TRUE, TRUE, FALSE);
6428 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006429 tail = regmatch.endp[0];
6430 if (*tail == NUL)
6431 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 if (!do_all)
6433 break;
6434 }
6435
6436 if (ga.ga_data != NULL)
6437 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6438
Bram Moolenaar473de612013-06-08 18:19:48 +02006439 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 }
6441
6442 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6443 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006444 if (p_cpo == empty_option)
6445 p_cpo = save_cpo;
6446 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006447 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006448 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006449 // If it's still empty it was changed and restored, need to restore in
6450 // the complicated way.
6451 if (*p_cpo == NUL)
6452 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006453 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455
6456 return ret;
6457}