blob: bfe9f613611f45589470ce3d7cb9894d336a4493 [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);
Yegappan Lakshmanana2438132021-07-10 21:29:18 +020060static char_u *eval_next_line(evalarg_T *evalarg);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020061
Bram Moolenaare21c1582019-03-02 11:57:09 +010062/*
63 * Return "n1" divided by "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010064 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010065 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020066 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010067num_divide(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010068{
69 varnumber_T result;
70
Bram Moolenaar99880f92021-01-20 21:23:14 +010071 if (n2 == 0)
Bram Moolenaare21c1582019-03-02 11:57:09 +010072 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010073 if (in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010074 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010075 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010076 if (failed != NULL)
77 *failed = TRUE;
78 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010079 if (n1 == 0)
80 result = VARNUM_MIN; // similar to NaN
81 else if (n1 < 0)
82 result = -VARNUM_MAX;
83 else
84 result = VARNUM_MAX;
85 }
86 else
87 result = n1 / n2;
88
89 return result;
90}
91
92/*
93 * Return "n1" modulus "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010094 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010095 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020096 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010097num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010098{
Bram Moolenaar99880f92021-01-20 21:23:14 +010099 if (n2 == 0 && in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +0100100 {
Bram Moolenaar99880f92021-01-20 21:23:14 +0100101 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +0100102 if (failed != NULL)
103 *failed = TRUE;
104 }
Bram Moolenaare21c1582019-03-02 11:57:09 +0100105 return (n2 == 0) ? 0 : (n1 % n2);
106}
107
Bram Moolenaar33570922005-01-25 22:26:29 +0000108/*
109 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000110 */
111 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100112eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000113{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200114 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200115 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000116
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200117#ifdef EBCDIC
118 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100119 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200120 */
121 sortFunctions();
122#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000123}
124
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000125#if defined(EXITFREE) || defined(PROTO)
126 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100127eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000128{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200129 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100130 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200131 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000132
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200133 // autoloaded script names
134 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000135
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200136 // unreferenced lists and dicts
137 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200138
139 // functions not garbage collected
140 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000141}
142#endif
143
Bram Moolenaare6b53242020-07-01 17:28:33 +0200144 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200145fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
146{
147 CLEAR_FIELD(*evalarg);
148 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
149 if (eap != NULL && getline_equal(eap->getline, eap->cookie, getsourceline))
150 {
151 evalarg->eval_getline = eap->getline;
152 evalarg->eval_cookie = eap->cookie;
153 }
154}
155
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156/*
157 * Top level evaluation function, returning a boolean.
158 * Sets "error" to TRUE if there was an error.
159 * Return TRUE or FALSE.
160 */
161 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100162eval_to_bool(
163 char_u *arg,
164 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200165 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100166 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167{
Bram Moolenaar33570922005-01-25 22:26:29 +0000168 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200169 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200170 evalarg_T evalarg;
171
Bram Moolenaar37c83712020-06-30 21:18:36 +0200172 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173
174 if (skip)
175 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200176 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178 else
179 {
180 *error = FALSE;
181 if (!skip)
182 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200183 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200184 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200185 else
186 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000187 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 }
189 }
190 if (skip)
191 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200192 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200194 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195}
196
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100197/*
198 * Call eval1() and give an error message if not done at a lower level.
199 */
200 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200201eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100202{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100203 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100204 int ret;
205 int did_emsg_before = did_emsg;
206 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200207 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100208
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200209 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
210
211 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100212 if (ret == FAIL)
213 {
214 // Report the invalid expression unless the expression evaluation has
215 // been cancelled due to an aborting error, an interrupt, or an
216 // exception, or we already gave a more specific error.
217 // Also check called_emsg for when using assert_fails().
218 if (!aborting() && did_emsg == did_emsg_before
219 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200220 semsg(_(e_invalid_expression_str), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100221 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200222 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100223 return ret;
224}
225
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100226/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200227 * Return whether a typval is a valid expression to pass to eval_expr_typval()
228 * or eval_expr_to_bool(). An empty string returns FALSE;
229 */
230 int
231eval_expr_valid_arg(typval_T *tv)
232{
233 return tv->v_type != VAR_UNKNOWN
234 && (tv->v_type != VAR_STRING
235 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
236}
237
238/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100239 * Evaluate an expression, which can be a function, partial or string.
240 * Pass arguments "argv[argc]".
241 * Return the result in "rettv" and OK or FAIL.
242 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200243 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100244eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
245{
246 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100247 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200248 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100249
250 if (expr->v_type == VAR_FUNC)
251 {
252 s = expr->vval.v_string;
253 if (s == NULL || *s == NUL)
254 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200255 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200256 funcexe.evaluate = TRUE;
257 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100258 return FAIL;
259 }
260 else if (expr->v_type == VAR_PARTIAL)
261 {
262 partial_T *partial = expr->vval.v_partial;
263
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200264 if (partial == NULL)
265 return FAIL;
266
Bram Moolenaar822ba242020-05-24 23:00:18 +0200267 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200268 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100269 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200270 if (call_def_function(partial->pt_func, argc, argv,
271 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100272 return FAIL;
273 }
274 else
275 {
276 s = partial_name(partial);
277 if (s == NULL || *s == NUL)
278 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200279 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100280 funcexe.evaluate = TRUE;
281 funcexe.partial = partial;
282 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
283 return FAIL;
284 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100285 }
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200286 else if (expr->v_type == VAR_INSTR)
287 {
288 return exe_typval_instr(expr, rettv);
289 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100290 else
291 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100292 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100293 if (s == NULL)
294 return FAIL;
295 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200296 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100297 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200298 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100299 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200300 clear_tv(rettv);
Bram Moolenaar108010a2021-06-27 22:03:33 +0200301 semsg(_(e_invalid_expression_str), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100302 return FAIL;
303 }
304 }
305 return OK;
306}
307
308/*
309 * Like eval_to_bool() but using a typval_T instead of a string.
310 * Works for string, funcref and partial.
311 */
312 int
313eval_expr_to_bool(typval_T *expr, int *error)
314{
315 typval_T rettv;
316 int res;
317
318 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
319 {
320 *error = TRUE;
321 return FALSE;
322 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200323 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100324 clear_tv(&rettv);
325 return res;
326}
327
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328/*
329 * Top level evaluation function, returning a string. If "skip" is TRUE,
330 * only parsing to "nextcmd" is done, without reporting errors. Return
331 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
332 */
333 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100334eval_to_string_skip(
335 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200336 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100337 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338{
Bram Moolenaar33570922005-01-25 22:26:29 +0000339 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200341 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342
Bram Moolenaar37c83712020-06-30 21:18:36 +0200343 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 if (skip)
345 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200346 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 retval = NULL;
348 else
349 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100350 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000351 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 }
353 if (skip)
354 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200355 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356
357 return retval;
358}
359
360/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000361 * Skip over an expression at "*pp".
362 * Return FAIL for an error, OK otherwise.
363 */
364 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200365skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000366{
Bram Moolenaar33570922005-01-25 22:26:29 +0000367 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000368
369 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200370 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000371}
372
373/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200374 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200375 * If in Vim9 script and line breaks are encountered, the lines are
376 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200377 * "arg" is advanced to just after the expression.
378 * "start" is set to the start of the expression, "end" to just after the end.
379 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200380 * Return FAIL for an error, OK otherwise.
381 */
382 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200383skip_expr_concatenate(
384 char_u **arg,
385 char_u **start,
386 char_u **end,
387 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200388{
389 typval_T rettv;
390 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200391 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200392 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200393 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200394 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200395 int evaluate = evalarg == NULL
396 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200397
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200398 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200399 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200400 {
401 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200402 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200403 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200404 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200405 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200406 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200407 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200408
409 // Don't evaluate the expression.
410 if (evalarg != NULL)
411 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200412 *arg = skipwhite(*arg);
413 res = eval1(arg, &rettv, evalarg);
414 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200415 if (evalarg != NULL)
416 evalarg->eval_flags = save_flags;
417
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200418 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200419 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200420 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200421 if (evalarg->eval_ga.ga_len == 1)
422 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200423 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200424 ga_clear(gap);
425 gap->ga_itemsize = 0;
426 }
427 else
428 {
429 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200430 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200431
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200432 // Line breaks encountered, concatenate all the lines.
433 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200434 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200435
436 // free the lines only when using getsourceline()
437 if (evalarg->eval_cookie != NULL)
438 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200439 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200440 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200441 // Do not free the last line, "arg" points into it, free it
442 // later.
443 vim_free(evalarg->eval_tofree);
444 evalarg->eval_tofree =
445 ((char_u **)gap->ga_data)[gap->ga_len - 1];
446 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200447 ga_clear_strings(gap);
448 }
449 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200450 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200451 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200452
453 // free lines that were explicitly marked for freeing
454 ga_clear_strings(freegap);
455 }
456
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200457 gap->ga_itemsize = 0;
458 if (p == NULL)
459 return FAIL;
460 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200461 vim_free(evalarg->eval_tofree_lambda);
462 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200463 // Compute "end" relative to the end.
464 *end = *start + STRLEN(*start) - endoff;
465 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200466 }
467
468 return res;
469}
470
471/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100472 * Convert "tv" to a string.
473 * When "convert" is TRUE convert a List into a sequence of lines and convert
474 * a Float to a String.
475 * Returns an allocated string (NULL when out of memory).
476 */
477 char_u *
478typval2string(typval_T *tv, int convert)
479{
480 garray_T ga;
481 char_u *retval;
482#ifdef FEAT_FLOAT
483 char_u numbuf[NUMBUFLEN];
484#endif
485
486 if (convert && tv->v_type == VAR_LIST)
487 {
488 ga_init2(&ga, (int)sizeof(char), 80);
489 if (tv->vval.v_list != NULL)
490 {
491 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
492 if (tv->vval.v_list->lv_len > 0)
493 ga_append(&ga, NL);
494 }
495 ga_append(&ga, NUL);
496 retval = (char_u *)ga.ga_data;
497 }
498#ifdef FEAT_FLOAT
499 else if (convert && tv->v_type == VAR_FLOAT)
500 {
501 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
502 retval = vim_strsave(numbuf);
503 }
504#endif
505 else
506 retval = vim_strsave(tv_get_string(tv));
507 return retval;
508}
509
510/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200511 * Top level evaluation function, returning a string. Does not handle line
512 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000513 * When "convert" is TRUE convert a List into a sequence of lines and convert
514 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 * Return pointer to allocated memory, or NULL for failure.
516 */
517 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100518eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100519 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100520 int convert,
521 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522{
Bram Moolenaar33570922005-01-25 22:26:29 +0000523 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100525 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100527 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
528 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 retval = NULL;
530 else
531 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100532 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000533 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100535 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536
537 return retval;
538}
539
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100540 char_u *
541eval_to_string(
542 char_u *arg,
543 int convert)
544{
545 return eval_to_string_eap(arg, convert, NULL);
546}
547
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000549 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200550 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200551 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 */
553 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100554eval_to_string_safe(
555 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100556 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557{
558 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200559 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200560 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200562 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200563 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000564 if (use_sandbox)
565 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200566 ++textwinlock;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200567 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000568 if (use_sandbox)
569 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200570 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200571 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200572 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 return retval;
574}
575
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576/*
577 * Top level evaluation function, returning a number.
578 * Evaluates "expr" silently.
579 * Returns -1 for an error.
580 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200581 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100582eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583{
Bram Moolenaar33570922005-01-25 22:26:29 +0000584 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200585 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000586 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587
588 ++emsg_off;
589
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200590 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 retval = -1;
592 else
593 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100594 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000595 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 }
597 --emsg_off;
598
599 return retval;
600}
601
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000602/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000603 * Top level evaluation function.
604 * Returns an allocated typval_T with the result.
605 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000606 */
607 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200608eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000609{
610 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200611 evalarg_T evalarg;
612
613 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000614
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200615 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200616 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100617 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000618
Bram Moolenaar37c83712020-06-30 21:18:36 +0200619 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000620 return tv;
621}
622
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100624 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200625 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
626 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000627 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200629 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100630call_vim_function(
631 char_u *func,
632 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200633 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200634 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000636 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200637 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100639 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200640 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200641 funcexe.firstline = curwin->w_cursor.lnum;
642 funcexe.lastline = curwin->w_cursor.lnum;
643 funcexe.evaluate = TRUE;
644 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000645 if (ret == FAIL)
646 clear_tv(rettv);
647
648 return ret;
649}
650
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100651/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100652 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100653 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200654 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
655 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100656 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200657 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100658call_func_retnr(
659 char_u *func,
660 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200661 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100662{
663 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200664 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100665
Bram Moolenaarded27a12018-08-01 19:06:03 +0200666 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100667 return -1;
668
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100669 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100670 clear_tv(&rettv);
671 return retval;
672}
673
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000674/*
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100675 * Call Vim script function like call_func_retnr() and drop the result.
676 * Returns FAIL when calling the function fails.
677 */
678 int
679call_func_noret(
680 char_u *func,
681 int argc,
682 typval_T *argv)
683{
684 typval_T rettv;
685
686 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
687 return FAIL;
688 clear_tv(&rettv);
689 return OK;
690}
691
692/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100693 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100694 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000695 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000696 */
697 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100698call_func_retstr(
699 char_u *func,
700 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200701 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000702{
703 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000704 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000705
Bram Moolenaarded27a12018-08-01 19:06:03 +0200706 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000707 return NULL;
708
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100709 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000710 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 return retval;
712}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000713
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000714/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100715 * Call Vim script function "func" and return the result as a List.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100716 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000717 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000718 */
719 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100720call_func_retlist(
721 char_u *func,
722 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200723 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000724{
725 typval_T rettv;
726
Bram Moolenaarded27a12018-08-01 19:06:03 +0200727 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000728 return NULL;
729
730 if (rettv.v_type != VAR_LIST)
731 {
732 clear_tv(&rettv);
733 return NULL;
734 }
735
736 return rettv.vval.v_list;
737}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739#ifdef FEAT_FOLDING
740/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100741 * Evaluate "arg", which is 'foldexpr'.
742 * Note: caller must set "curwin" to match "arg".
743 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
744 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 */
746 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100747eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748{
Bram Moolenaar33570922005-01-25 22:26:29 +0000749 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200750 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000752 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
753 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754
755 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000756 if (use_sandbox)
757 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200758 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200760 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 retval = 0;
762 else
763 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100764 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000765 if (tv.v_type == VAR_NUMBER)
766 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000767 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 retval = 0;
769 else
770 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100771 // If the result is a string, check if there is a non-digit before
772 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000773 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 if (!VIM_ISDIGIT(*s) && *s != '-')
775 *cp = *s++;
776 retval = atol((char *)s);
777 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000778 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 }
780 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000781 if (use_sandbox)
782 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200783 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200784 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200786 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787}
788#endif
789
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000791 * Get an lval: variable, Dict item or List item that can be assigned a value
792 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
793 * "name.key", "name.key[expr]" etc.
794 * Indexing only works if "name" is an existing List or Dictionary.
795 * "name" points to the start of the name.
796 * If "rettv" is not NULL it points to the value to be assigned.
797 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
798 * wrong; must end in space or cmd separator.
799 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100800 * flags:
801 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100802 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100803 * GLV_NO_AUTOLOAD: do not use script autoloading
804 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000805 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000806 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000807 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000808 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200809 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100810get_lval(
811 char_u *name,
812 typval_T *rettv,
813 lval_T *lp,
814 int unlet,
815 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100816 int flags, // GLV_ values
817 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000818{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000819 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000820 char_u *expr_start, *expr_end;
821 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000822 dictitem_T *v;
823 typval_T var1;
824 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000825 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000826 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000827 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000828 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100829 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100830 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100831 int writing;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000832
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100833 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200834 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000835
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100836 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000837 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100838 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000839 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200840 lp->ll_name_end = find_name_end(name, NULL, NULL,
841 FNE_INCL_BR | fne_flags);
842 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000843 }
844
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100845 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000846 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100847 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000848 if (expr_start != NULL)
849 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100850 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100851 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000852 && *p != '[' && *p != '.')
853 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200854 semsg(_(e_trailing_arg), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000855 return NULL;
856 }
857
858 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
859 if (lp->ll_exp_name == NULL)
860 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100861 // Report an invalid expression in braces, unless the
862 // expression evaluation has been cancelled due to an
863 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000864 if (!aborting() && !quiet)
865 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000866 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100867 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000868 return NULL;
869 }
870 }
871 lp->ll_name = lp->ll_exp_name;
872 }
873 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100874 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000875 lp->ll_name = name;
876
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200877 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100878 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200879 // "a: type" is declaring variable "a" with a type, not "a:".
880 if (p == name + 2 && p[-1] == ':')
881 {
882 --p;
883 lp->ll_name_end = p;
884 }
885 if (*p == ':')
886 {
887 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
888 char_u *tp = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100889
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200890 // parse the type after the name
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100891 lp->ll_type = parse_type(&tp, &si->sn_type_list, !quiet);
892 if (lp->ll_type == NULL && !quiet)
893 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200894 lp->ll_name_end = tp;
895 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100896 }
897 }
898
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100899 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000900 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
901 return p;
902
903 cc = *p;
904 *p = NUL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100905 // When we would write to the variable pass &ht and prevent autoload.
906 writing = !(flags & GLV_READ_ONLY);
907 v = find_var(lp->ll_name, writing ? &ht : NULL,
908 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000909 if (v == NULL && !quiet)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200910 semsg(_(e_undefined_variable_str), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000911 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000912 if (v == NULL)
913 return NULL;
914
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +0100915 if (in_vim9script() && (flags & GLV_NO_DECL) == 0)
916 {
917 if (!quiet)
918 semsg(_(e_variable_already_declared), lp->ll_name);
919 return NULL;
920 }
921
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000922 /*
923 * Loop until no more [idx] or .key is following.
924 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000925 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100926 var1.v_type = VAR_UNKNOWN;
927 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +0200928 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000929 {
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +0200930 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
931 {
932 if (!quiet)
933 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
934 return NULL;
935 }
Bram Moolenaare65081d2021-06-27 15:04:05 +0200936 if (lp->ll_tv->v_type != VAR_LIST
937 && lp->ll_tv->v_type != VAR_DICT
938 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000939 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000940 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100941 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000942 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000943 }
Bram Moolenaare65081d2021-06-27 15:04:05 +0200944
945 // a NULL list/blob works like an empty list/blob, allocate one now.
946 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
947 rettv_list_alloc(lp->ll_tv);
948 else if (lp->ll_tv->v_type == VAR_BLOB
949 && lp->ll_tv->vval.v_blob == NULL)
950 rettv_blob_alloc(lp->ll_tv);
951
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000952 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000953 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000954 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100955 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000956 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000957 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000958
Bram Moolenaar10c65862020-10-08 21:16:42 +0200959 if (in_vim9script() && lp->ll_valtype == NULL
960 && lp->ll_tv == &v->di_tv
961 && ht != NULL && ht == get_script_local_ht())
962 {
Bram Moolenaarc967d572021-07-08 21:38:50 +0200963 svar_T *sv = find_typval_in_script(lp->ll_tv);
Bram Moolenaar10c65862020-10-08 21:16:42 +0200964
965 // Vim9 script local variable: get the type
966 if (sv != NULL)
967 lp->ll_valtype = sv->sv_type;
968 }
969
Bram Moolenaar8c711452005-01-14 21:53:12 +0000970 len = -1;
971 if (*p == '.')
972 {
973 key = p + 1;
974 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
975 ;
976 if (len == 0)
977 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000978 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100979 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000980 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000981 }
982 p = key + len;
983 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000984 else
985 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100986 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000987 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000988 if (*p == ':')
989 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000990 else
991 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000992 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200993 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000994 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100995 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000996 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100997 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000998 clear_tv(&var1);
999 return NULL;
1000 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001001 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001002 }
1003
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001004 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001005 if (*p == ':')
1006 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001007 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001008 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001009 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001010 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001011 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001012 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001013 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001014 if (rettv != NULL
1015 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001016 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001017 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001018 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001019 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001020 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001021 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001022 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001023 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001024 }
1025 p = skipwhite(p + 1);
1026 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001027 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001028 else
1029 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001030 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001031 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001032 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001033 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001034 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001035 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001036 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001037 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001038 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001039 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001040 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001041 clear_tv(&var2);
1042 return NULL;
1043 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001044 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001045 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001046 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001047 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001048 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001049
Bram Moolenaar8c711452005-01-14 21:53:12 +00001050 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001051 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001052 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001053 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001054 clear_tv(&var1);
1055 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001056 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001057 }
1058
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001059 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001060 ++p;
1061 }
1062
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001063 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001064 {
1065 if (len == -1)
1066 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001067 // "[key]": get key from "var1"
1068 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001069 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001070 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001071 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001072 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001073 }
1074 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001075 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001076
1077 // a NULL dict is equivalent with an empty dict
1078 if (lp->ll_tv->vval.v_dict == NULL)
1079 {
1080 lp->ll_tv->vval.v_dict = dict_alloc();
1081 if (lp->ll_tv->vval.v_dict == NULL)
1082 {
1083 clear_tv(&var1);
1084 return NULL;
1085 }
1086 ++lp->ll_tv->vval.v_dict->dv_refcount;
1087 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001088 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001089
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001090 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001091
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001092 // When assigning to a scope dictionary check that a function and
1093 // variable name is valid (only variable name unless it is l: or
1094 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001095 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001096 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001097 int prevval;
1098 int wrong;
1099
1100 if (len != -1)
1101 {
1102 prevval = key[len];
1103 key[len] = NUL;
1104 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001105 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001106 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001107 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1108 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001109 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar03290b82020-12-19 16:30:44 +01001110 || !valid_varname(key, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001111 if (len != -1)
1112 key[len] = prevval;
1113 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001114 {
1115 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001116 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001117 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001118 }
1119
Bram Moolenaar10c65862020-10-08 21:16:42 +02001120 if (lp->ll_valtype != NULL)
1121 // use the type of the member
1122 lp->ll_valtype = lp->ll_valtype->tt_member;
1123
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001124 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001125 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001126 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001127 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001128 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001129 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001130 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001131 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001132 return NULL;
1133 }
1134
Bram Moolenaar31b81602019-02-10 22:14:27 +01001135 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001136 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001137 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001138 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001139 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001140 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001141 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001142 }
1143 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001144 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001145 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001146 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001147 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001148 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001149 p = NULL;
1150 break;
1151 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001152 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001153 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001154 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1155 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001156 {
1157 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001158 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001159 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001160
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001161 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001162 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001163 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001164 else if (lp->ll_tv->v_type == VAR_BLOB)
1165 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001166 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1167
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001168 /*
1169 * Get the number and item for the only or first index of the List.
1170 */
1171 if (empty1)
1172 lp->ll_n1 = 0;
1173 else
1174 // is number or string
1175 lp->ll_n1 = (long)tv_get_number(&var1);
1176 clear_tv(&var1);
1177
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001178 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001179 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001180 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001181 return NULL;
1182 }
1183 if (lp->ll_range && !lp->ll_empty2)
1184 {
1185 lp->ll_n2 = (long)tv_get_number(&var2);
1186 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001187 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1188 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001189 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001190 }
1191 lp->ll_blob = lp->ll_tv->vval.v_blob;
1192 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001193 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001194 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001195 else
1196 {
1197 /*
1198 * Get the number and item for the only or first index of the List.
1199 */
1200 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001201 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001202 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001203 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001204 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001205 clear_tv(&var1);
1206
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001207 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001208 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001209 lp->ll_li = list_find_index(lp->ll_list, &lp->ll_n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001210 if (lp->ll_li == NULL)
1211 {
Bram Moolenaare65081d2021-06-27 15:04:05 +02001212 // Vim9: Allow for adding an item at the end.
1213 if (in_vim9script() && lp->ll_n1 == lp->ll_list->lv_len
1214 && lp->ll_list->lv_lock == 0)
1215 {
1216 list_append_number(lp->ll_list, 0);
1217 lp->ll_li = list_find_index(lp->ll_list, &lp->ll_n1);
1218 }
1219 if (lp->ll_li == NULL)
1220 {
1221 clear_tv(&var2);
1222 if (!quiet)
1223 semsg(_(e_listidx), lp->ll_n1);
1224 return NULL;
1225 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001226 }
1227
Bram Moolenaar10c65862020-10-08 21:16:42 +02001228 if (lp->ll_valtype != NULL)
1229 // use the type of the member
1230 lp->ll_valtype = lp->ll_valtype->tt_member;
1231
Bram Moolenaar8c711452005-01-14 21:53:12 +00001232 /*
1233 * May need to find the item or absolute index for the second
1234 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001235 * When no index given: "lp->ll_empty2" is TRUE.
1236 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001237 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001238 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001239 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001240 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001241 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001242 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001243 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001244 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001245 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001246 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001247 {
1248 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001249 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001250 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001251 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001252 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001253 }
1254
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001255 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001256 if (lp->ll_n1 < 0)
1257 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1258 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001259 {
1260 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001261 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001262 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001263 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001264 }
1265
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001266 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001267 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001268 }
1269
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001270 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001271 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001272 return p;
1273}
1274
1275/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001276 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001277 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001278 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001279clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001280{
1281 vim_free(lp->ll_exp_name);
1282 vim_free(lp->ll_newkey);
1283}
1284
1285/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001286 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001287 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001288 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1289 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001290 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001291 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001292set_var_lval(
1293 lval_T *lp,
1294 char_u *endp,
1295 typval_T *rettv,
1296 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001297 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1298 char_u *op,
1299 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001300{
1301 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001302 listitem_T *ri;
1303 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001304
1305 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001306 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001307 cc = *endp;
1308 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001309 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1310 return;
1311
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001312 if (lp->ll_blob != NULL)
1313 {
1314 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001315
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001316 if (op != NULL && *op != '=')
1317 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001318 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001319 return;
1320 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001321 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001322 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001323
1324 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1325 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001326 if (lp->ll_empty2)
1327 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1328
Bram Moolenaar68452172021-04-12 21:21:02 +02001329 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1330 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001331 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001332 }
1333 else
1334 {
1335 val = (int)tv_get_number_chk(rettv, &error);
1336 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001337 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001338 }
1339 }
1340 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001341 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001342 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001343
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001344 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1345 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001346 {
1347 emsg(_(e_cannot_mod));
1348 *endp = cc;
1349 return;
1350 }
1351
Bram Moolenaarff697e62019-02-12 22:28:33 +01001352 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001353 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001354 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001355 &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001356 {
1357 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001358 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1359 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001360 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar24e93162021-07-18 20:40:33 +02001361 set_var_const(lp->ll_name, NULL, &tv, FALSE,
1362 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001363 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001364 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001365 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001366 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001367 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001368 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1369 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001370 return;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001371 set_var_const(lp->ll_name, lp->ll_type, rettv, copy,
1372 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001373 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001374 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001375 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001376 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001377 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001378 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001379 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001380 else if (lp->ll_range)
1381 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001382 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001383 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001384
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001385 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1386 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001387 {
1388 emsg(_("E996: Cannot lock a range"));
1389 return;
1390 }
1391
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001392 /*
1393 * Check whether any of the list items is locked
1394 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001395 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001396 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001397 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001398 return;
1399 ri = ri->li_next;
1400 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1401 break;
1402 ll_li = ll_li->li_next;
1403 ++ll_n1;
1404 }
1405
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001406 /*
1407 * Assign the List values to the list items.
1408 */
1409 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001410 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001411 if (op != NULL && *op != '=')
1412 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1413 else
1414 {
1415 clear_tv(&lp->ll_li->li_tv);
1416 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1417 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001418 ri = ri->li_next;
1419 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1420 break;
1421 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001422 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001423 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001424 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001425 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001426 ri = NULL;
1427 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001428 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001429 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001430 lp->ll_li = lp->ll_li->li_next;
1431 ++lp->ll_n1;
1432 }
1433 if (ri != NULL)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001434 emsg(_(e_list_value_has_more_items_than_targets));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001435 else if (lp->ll_empty2
1436 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001437 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001438 emsg(_(e_list_value_does_not_have_enough_items));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001439 }
1440 else
1441 {
1442 /*
1443 * Assign to a List or Dictionary item.
1444 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001445 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1446 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001447 {
1448 emsg(_("E996: Cannot lock a list or dict"));
1449 return;
1450 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001451
1452 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001453 && check_typval_arg_type(lp->ll_valtype, rettv,
1454 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001455 return;
1456
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001457 if (lp->ll_newkey != NULL)
1458 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001459 if (op != NULL && *op != '=')
1460 {
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02001461 semsg(_(e_dictkey), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001462 return;
1463 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001464 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1465 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001466 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001467
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001468 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001469 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001470 if (di == NULL)
1471 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001472 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1473 {
1474 vim_free(di);
1475 return;
1476 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001477 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001478 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001479 else if (op != NULL && *op != '=')
1480 {
1481 tv_op(lp->ll_tv, rettv, op);
1482 return;
1483 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001484 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001485 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001486
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001487 /*
1488 * Assign the value to the variable or list item.
1489 */
1490 if (copy)
1491 copy_tv(rettv, lp->ll_tv);
1492 else
1493 {
1494 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001495 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001496 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001497 }
1498 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001499}
1500
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001501/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001502 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1503 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001504 * Returns OK or FAIL.
1505 */
1506 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001507tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001508{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001509 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001510 char_u numbuf[NUMBUFLEN];
1511 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001512 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001513
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001514 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001515 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001516 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001517 {
1518 switch (tv1->v_type)
1519 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001520 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001521 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001522 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001523 case VAR_DICT:
1524 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001525 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001526 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001527 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001528 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001529 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001530 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001531 break;
1532
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001533 case VAR_BLOB:
1534 if (*op != '+' || tv2->v_type != VAR_BLOB)
1535 break;
1536 // BLOB += BLOB
1537 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1538 {
1539 blob_T *b1 = tv1->vval.v_blob;
1540 blob_T *b2 = tv2->vval.v_blob;
1541 int i, len = blob_len(b2);
1542 for (i = 0; i < len; i++)
1543 ga_append(&b1->bv_ga, blob_get(b2, i));
1544 }
1545 return OK;
1546
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001547 case VAR_LIST:
1548 if (*op != '+' || tv2->v_type != VAR_LIST)
1549 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001550 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001551 if (tv2->vval.v_list != NULL)
1552 {
1553 if (tv1->vval.v_list == NULL)
1554 {
1555 tv1->vval.v_list = tv2->vval.v_list;
1556 ++tv1->vval.v_list->lv_refcount;
1557 }
1558 else
1559 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1560 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001561 return OK;
1562
1563 case VAR_NUMBER:
1564 case VAR_STRING:
1565 if (tv2->v_type == VAR_LIST)
1566 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001567 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001568 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001569 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001570 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001571#ifdef FEAT_FLOAT
1572 if (tv2->v_type == VAR_FLOAT)
1573 {
1574 float_T f = n;
1575
Bram Moolenaarff697e62019-02-12 22:28:33 +01001576 if (*op == '%')
1577 break;
1578 switch (*op)
1579 {
1580 case '+': f += tv2->vval.v_float; break;
1581 case '-': f -= tv2->vval.v_float; break;
1582 case '*': f *= tv2->vval.v_float; break;
1583 case '/': f /= tv2->vval.v_float; break;
1584 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001585 clear_tv(tv1);
1586 tv1->v_type = VAR_FLOAT;
1587 tv1->vval.v_float = f;
1588 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001589 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001590#endif
1591 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001592 switch (*op)
1593 {
1594 case '+': n += tv_get_number(tv2); break;
1595 case '-': n -= tv_get_number(tv2); break;
1596 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001597 case '/': n = num_divide(n, tv_get_number(tv2),
1598 &failed); break;
1599 case '%': n = num_modulus(n, tv_get_number(tv2),
1600 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001601 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001602 clear_tv(tv1);
1603 tv1->v_type = VAR_NUMBER;
1604 tv1->vval.v_number = n;
1605 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001606 }
1607 else
1608 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001609 if (tv2->v_type == VAR_FLOAT)
1610 break;
1611
Bram Moolenaarff697e62019-02-12 22:28:33 +01001612 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001613 s = tv_get_string(tv1);
1614 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001615 clear_tv(tv1);
1616 tv1->v_type = VAR_STRING;
1617 tv1->vval.v_string = s;
1618 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001619 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001620
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001621 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001622#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001623 {
1624 float_T f;
1625
Bram Moolenaarff697e62019-02-12 22:28:33 +01001626 if (*op == '%' || *op == '.'
1627 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001628 && tv2->v_type != VAR_NUMBER
1629 && tv2->v_type != VAR_STRING))
1630 break;
1631 if (tv2->v_type == VAR_FLOAT)
1632 f = tv2->vval.v_float;
1633 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001634 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001635 switch (*op)
1636 {
1637 case '+': tv1->vval.v_float += f; break;
1638 case '-': tv1->vval.v_float -= f; break;
1639 case '*': tv1->vval.v_float *= f; break;
1640 case '/': tv1->vval.v_float /= f; break;
1641 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001642 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001643#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001644 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001645 }
1646 }
1647
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001648 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001649 return FAIL;
1650}
1651
1652/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001653 * Evaluate the expression used in a ":for var in expr" command.
1654 * "arg" points to "var".
1655 * Set "*errp" to TRUE for an error, FALSE otherwise;
1656 * Return a pointer that holds the info. Null when there is an error.
1657 */
1658 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001659eval_for_line(
1660 char_u *arg,
1661 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001662 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001663 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001664{
Bram Moolenaar33570922005-01-25 22:26:29 +00001665 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001666 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001667 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001668 typval_T tv;
1669 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001670 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001671
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001672 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001673
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001674 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001675 if (fi == NULL)
1676 return NULL;
1677
Bram Moolenaar404557e2021-07-05 21:41:48 +02001678 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1679 &fi->fi_semicolon, FALSE);
1680 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001681 return fi;
1682
Bram Moolenaar404557e2021-07-05 21:41:48 +02001683 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001684 if (expr[0] != 'i' || expr[1] != 'n'
1685 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001686 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001687 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1688 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1689 else
1690 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001691 return fi;
1692 }
1693
1694 if (skip)
1695 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001696 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1697 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001698 {
1699 *errp = FALSE;
1700 if (!skip)
1701 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001702 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001703 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001704 l = tv.vval.v_list;
1705 if (l == NULL)
1706 {
1707 // a null list is like an empty list: do nothing
1708 clear_tv(&tv);
1709 }
1710 else
1711 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001712 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001713 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001714
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001715 // No need to increment the refcount, it's already set for
1716 // the list being used in "tv".
1717 fi->fi_list = l;
1718 list_add_watch(l, &fi->fi_lw);
1719 fi->fi_lw.lw_item = l->lv_first;
1720 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001721 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001722 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001723 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001724 fi->fi_bi = 0;
1725 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001726 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001727 typval_T btv;
1728
1729 // Make a copy, so that the iteration still works when the
1730 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001731 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001732 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001733 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001734 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001735 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001736 else if (tv.v_type == VAR_STRING)
1737 {
1738 fi->fi_byte_idx = 0;
1739 fi->fi_string = tv.vval.v_string;
1740 tv.vval.v_string = NULL;
1741 if (fi->fi_string == NULL)
1742 fi->fi_string = vim_strsave((char_u *)"");
1743 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001744 else
1745 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001746 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001747 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001748 }
1749 }
1750 }
1751 if (skip)
1752 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001753 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001754
1755 return fi;
1756}
1757
1758/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001759 * Used when looping over a :for line, skip the "in expr" part.
1760 */
1761 void
1762skip_for_lines(void *fi_void, evalarg_T *evalarg)
1763{
1764 forinfo_T *fi = (forinfo_T *)fi_void;
1765 int i;
1766
1767 for (i = 0; i < fi->fi_break_count; ++i)
1768 eval_next_line(evalarg);
1769}
1770
1771/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001772 * Use the first item in a ":for" list. Advance to the next.
1773 * Assign the values to the variable (list). "arg" points to the first one.
1774 * Return TRUE when a valid item was found, FALSE when at end of list or
1775 * something wrong.
1776 */
1777 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001778next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001779{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001780 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001781 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001782 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001783 ? (ASSIGN_FINAL
1784 // first round: error if variable exists
1785 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
1786 | ASSIGN_NO_MEMBER_TYPE)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001787 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001788 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001789 int skip_assign = in_vim9script() && arg[0] == '_'
1790 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001791
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001792 if (fi->fi_blob != NULL)
1793 {
1794 typval_T tv;
1795
1796 if (fi->fi_bi >= blob_len(fi->fi_blob))
1797 return FALSE;
1798 tv.v_type = VAR_NUMBER;
1799 tv.v_lock = VAR_FIXED;
1800 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1801 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001802 if (skip_assign)
1803 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001804 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001805 fi->fi_varcount, flag, NULL) == OK;
1806 }
1807
1808 if (fi->fi_string != NULL)
1809 {
1810 typval_T tv;
1811 int len;
1812
1813 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1814 if (len == 0)
1815 return FALSE;
1816 tv.v_type = VAR_STRING;
1817 tv.v_lock = VAR_FIXED;
1818 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1819 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001820 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001821 if (skip_assign)
1822 result = TRUE;
1823 else
1824 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001825 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001826 vim_free(tv.vval.v_string);
1827 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001828 }
1829
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001830 item = fi->fi_lw.lw_item;
1831 if (item == NULL)
1832 result = FALSE;
1833 else
1834 {
1835 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001836 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001837 if (skip_assign)
1838 result = TRUE;
1839 else
1840 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001841 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001842 }
1843 return result;
1844}
1845
1846/*
1847 * Free the structure used to store info used by ":for".
1848 */
1849 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001850free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001851{
Bram Moolenaar33570922005-01-25 22:26:29 +00001852 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001853
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001854 if (fi == NULL)
1855 return;
1856 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001857 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001858 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001859 list_unref(fi->fi_list);
1860 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001861 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001862 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001863 else
1864 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001865 vim_free(fi);
1866}
1867
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001869set_context_for_expression(
1870 expand_T *xp,
1871 char_u *arg,
1872 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001874 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001876 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001878 if (cmdidx == CMD_let || cmdidx == CMD_var
1879 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001880 {
1881 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001882 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001883 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001884 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001885 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001886 {
1887 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001888 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001889 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001890 break;
1891 }
1892 return;
1893 }
1894 }
1895 else
1896 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1897 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 while ((xp->xp_pattern = vim_strpbrk(arg,
1899 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1900 {
1901 c = *xp->xp_pattern;
1902 if (c == '&')
1903 {
1904 c = xp->xp_pattern[1];
1905 if (c == '&')
1906 {
1907 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001908 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 }
1910 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001911 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001913 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1914 xp->xp_pattern += 2;
1915
1916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 }
1918 else if (c == '$')
1919 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001920 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 xp->xp_context = EXPAND_ENV_VARS;
1922 }
1923 else if (c == '=')
1924 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001925 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 xp->xp_context = EXPAND_EXPRESSION;
1927 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001928 else if (c == '#'
1929 && xp->xp_context == EXPAND_EXPRESSION)
1930 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001931 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001932 break;
1933 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001934 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 && xp->xp_context == EXPAND_FUNCTIONS
1936 && vim_strchr(xp->xp_pattern, '(') == NULL)
1937 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001938 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 break;
1940 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001941 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001943 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 {
1945 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1946 if (c == '\\' && xp->xp_pattern[1] != NUL)
1947 ++xp->xp_pattern;
1948 xp->xp_context = EXPAND_NOTHING;
1949 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001950 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001952 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1954 /* skip */ ;
1955 xp->xp_context = EXPAND_NOTHING;
1956 }
1957 else if (c == '|')
1958 {
1959 if (xp->xp_pattern[1] == '|')
1960 {
1961 ++xp->xp_pattern;
1962 xp->xp_context = EXPAND_EXPRESSION;
1963 }
1964 else
1965 xp->xp_context = EXPAND_COMMANDS;
1966 }
1967 else
1968 xp->xp_context = EXPAND_EXPRESSION;
1969 }
1970 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001971 // Doesn't look like something valid, expand as an expression
1972 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001973 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 arg = xp->xp_pattern;
1975 if (*arg != NUL)
1976 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1977 /* skip */ ;
1978 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01001979
1980 // ":exe one two" completes "two"
1981 if ((cmdidx == CMD_execute
1982 || cmdidx == CMD_echo
1983 || cmdidx == CMD_echon
1984 || cmdidx == CMD_echomsg)
1985 && xp->xp_context == EXPAND_EXPRESSION)
1986 {
1987 for (;;)
1988 {
1989 char_u *n = skiptowhite(arg);
1990
1991 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
1992 break;
1993 arg = skipwhite(n);
1994 }
1995 }
1996
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 xp->xp_pattern = arg;
1998}
1999
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002001 * Return TRUE if "pat" matches "text".
2002 * Does not use 'cpo' and always uses 'magic'.
2003 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002004 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002005pattern_match(char_u *pat, char_u *text, int ic)
2006{
2007 int matches = FALSE;
2008 char_u *save_cpo;
2009 regmatch_T regmatch;
2010
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002011 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002012 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002013 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002014 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2015 if (regmatch.regprog != NULL)
2016 {
2017 regmatch.rm_ic = ic;
2018 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2019 vim_regfree(regmatch.regprog);
2020 }
2021 p_cpo = save_cpo;
2022 return matches;
2023}
2024
2025/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002026 * Handle a name followed by "(". Both for just "name(arg)" and for
2027 * "expr->name(arg)".
2028 * Returns OK or FAIL.
2029 */
2030 static int
2031eval_func(
2032 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002033 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002034 char_u *name,
2035 int name_len,
2036 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002037 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002038 typval_T *basetv) // "expr" for "expr->name(arg)"
2039{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002040 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002041 char_u *s = name;
2042 int len = name_len;
2043 partial_T *partial;
2044 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002045 type_T *type = NULL;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002046
2047 if (!evaluate)
2048 check_vars(s, len);
2049
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002050 // If "s" is the name of a variable of type VAR_FUNC
2051 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002052 s = deref_func_name(s, &len, &partial,
2053 in_vim9script() ? &type : NULL, !evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002054
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002055 // Need to make a copy, in case evaluating the arguments makes
2056 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002057 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002058 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002059 ret = FAIL;
2060 else
2061 {
2062 funcexe_T funcexe;
2063
2064 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002065 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002066 funcexe.firstline = curwin->w_cursor.lnum;
2067 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002068 funcexe.evaluate = evaluate;
2069 funcexe.partial = partial;
2070 funcexe.basetv = basetv;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002071 funcexe.check_type = type;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002072 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002073 }
2074 vim_free(s);
2075
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002076 // If evaluate is FALSE rettv->v_type was not set in
2077 // get_func_tv, but it's needed in handle_subscript() to parse
2078 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002079 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2080 {
2081 rettv->vval.v_string = NULL;
2082 rettv->v_type = VAR_FUNC;
2083 }
2084
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002085 // Stop the expression evaluation when immediately
2086 // aborting on error, or when an interrupt occurred or
2087 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002088 if (evaluate && aborting())
2089 {
2090 if (ret == OK)
2091 clear_tv(rettv);
2092 ret = FAIL;
2093 }
2094 return ret;
2095}
2096
2097/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002098 * Get the next line source line without advancing. But do skip over comment
2099 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002100 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002101 */
2102 static char_u *
2103getline_peek_skip_comments(evalarg_T *evalarg)
2104{
2105 for (;;)
2106 {
2107 char_u *next = getline_peek(evalarg->eval_getline,
2108 evalarg->eval_cookie);
2109 char_u *p;
2110
2111 if (next == NULL)
2112 break;
2113 p = skipwhite(next);
2114 if (*p != NUL && !vim9_comment_start(p))
2115 return next;
2116 (void)eval_next_line(evalarg);
2117 }
2118 return NULL;
2119}
2120
2121/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002122 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2123 * comment) and there is a next line, return the next line (skipping blanks)
2124 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002125 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2126 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002127 * "arg" must point somewhere inside a line, not at the start.
2128 */
Yegappan Lakshmanana2438132021-07-10 21:29:18 +02002129 static char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002130eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2131{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002132 char_u *p = skipwhite(arg);
2133
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002134 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002135 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002136 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002137 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002138 && (*p == NUL || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002139 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002140 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002141
2142 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002143 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002144 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002145 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002146
Bram Moolenaar9d489562020-07-30 20:08:50 +02002147 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002148 {
2149 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002150 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002151 }
2152 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002153 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002154}
2155
2156/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002157 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002158 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002159 */
Yegappan Lakshmanana2438132021-07-10 21:29:18 +02002160 static char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002161eval_next_line(evalarg_T *evalarg)
2162{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002163 garray_T *gap = &evalarg->eval_ga;
2164 char_u *line;
2165
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002166 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002167 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2168 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002169 else
2170 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002171 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002172 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2173 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002174 char_u *p = skipwhite(line);
2175
2176 // Going to concatenate the lines after parsing. For an empty or
2177 // comment line use an empty string.
2178 if (*p == NUL || vim9_comment_start(p))
2179 {
2180 vim_free(line);
2181 line = vim_strsave((char_u *)"");
2182 }
2183
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002184 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2185 ++gap->ga_len;
2186 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002187 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002188 {
2189 vim_free(evalarg->eval_tofree);
2190 evalarg->eval_tofree = line;
2191 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002192
2193 // Advanced to the next line, "arg" no longer points into the previous
2194 // line.
2195 VIM_CLEAR(evalarg->eval_tofree_cmdline);
2196
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002197 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002198}
2199
Bram Moolenaare6b53242020-07-01 17:28:33 +02002200/*
2201 * Call eval_next_non_blank() and get the next line if needed.
2202 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002203 char_u *
2204skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2205{
2206 int getnext;
2207 char_u *p = skipwhite(arg);
2208
Bram Moolenaar962d7212020-07-04 14:15:00 +02002209 if (evalarg == NULL)
2210 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002211 eval_next_non_blank(p, evalarg, &getnext);
2212 if (getnext)
2213 return eval_next_line(evalarg);
2214 return p;
2215}
2216
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002217/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002218 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002219 */
2220 void
2221clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2222{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002223 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002224 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002225 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002226 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002227 if (eap != NULL)
2228 {
2229 // We may need to keep the original command line, e.g. for
2230 // ":let" it has the variable names. But we may also need the
2231 // new one, "nextcmd" points into it. Keep both.
2232 vim_free(eap->cmdline_tofree);
2233 eap->cmdline_tofree = *eap->cmdlinep;
2234 *eap->cmdlinep = evalarg->eval_tofree;
2235 }
2236 else
2237 vim_free(evalarg->eval_tofree);
2238 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002239 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002240
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002241 VIM_CLEAR(evalarg->eval_tofree_cmdline);
2242 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002243 }
2244}
2245
2246/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2250 */
2251
2252/*
2253 * Handle zero level expression.
2254 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002255 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002256 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002257 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 * Return OK or FAIL.
2259 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002260 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002261eval0(
2262 char_u *arg,
2263 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002264 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002265 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266{
2267 int ret;
2268 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002269 int did_emsg_before = did_emsg;
2270 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002271 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002272 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273
2274 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002275 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002276 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002277
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002278 if (ret != FAIL)
2279 end_error = !ends_excmd2(arg, p);
2280 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 {
2282 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002283 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 /*
2285 * Report the invalid expression unless the expression evaluation has
2286 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002287 * exception, or we already gave a more specific error.
2288 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002290 if (!aborting()
2291 && did_emsg == did_emsg_before
2292 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002293 && (flags & EVAL_CONSTANT) == 0
2294 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002295 {
2296 if (end_error)
2297 semsg(_(e_trailing_arg), p);
2298 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002299 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002300 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002301
2302 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002303 // a next command to avoid more errors, unless "|" is following, which
2304 // could only be a command separator.
2305 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2306 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002307 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002309
2310 if (eap != NULL)
2311 eap->nextcmd = check_nextcmd(p);
2312
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 return ret;
2314}
2315
2316/*
2317 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002318 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002319 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 *
2321 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002322 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002324 * Note: "rettv.v_lock" is not set.
2325 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 * Return OK or FAIL.
2327 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002328 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002329eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002331 char_u *p;
2332 int getnext;
2333
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002334 CLEAR_POINTER(rettv);
2335
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 /*
2337 * Get the first variable.
2338 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002339 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 return FAIL;
2341
Bram Moolenaar793648f2020-06-26 21:28:25 +02002342 p = eval_next_non_blank(*arg, evalarg, &getnext);
2343 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002345 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002346 int result;
2347 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002348 evalarg_T *evalarg_used = evalarg;
2349 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002350 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002351 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002352 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002353
2354 if (evalarg == NULL)
2355 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002356 CLEAR_FIELD(local_evalarg);
2357 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002358 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002359 orig_flags = evalarg_used->eval_flags;
2360 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002361
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002362 if (getnext)
2363 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002364 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002365 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002366 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002367 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002368 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002369 clear_tv(rettv);
2370 return FAIL;
2371 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002372 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002373 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002374
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002376 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002378 int error = FALSE;
2379
Bram Moolenaar13106602020-10-04 16:06:05 +02002380 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002381 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002382 else if (vim9script)
2383 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002384 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002386 if (error || !op_falsy || !result)
2387 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002388 if (error)
2389 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 }
2391
2392 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002393 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002395 if (op_falsy)
2396 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002397 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002398 {
mityu4ac198c2021-05-28 17:52:40 +02002399 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002400 clear_tv(rettv);
2401 return FAIL;
2402 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002403 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002404 evalarg_used->eval_flags = (op_falsy ? !result : result)
2405 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2406 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002407 {
2408 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002410 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002411 if (!op_falsy || !result)
2412 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002414 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002416 /*
2417 * Check for the ":".
2418 */
2419 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2420 if (*p != ':')
2421 {
2422 emsg(_(e_missing_colon));
2423 if (evaluate && result)
2424 clear_tv(rettv);
2425 evalarg_used->eval_flags = orig_flags;
2426 return FAIL;
2427 }
2428 if (getnext)
2429 *arg = eval_next_line(evalarg_used);
2430 else
2431 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002432 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002433 {
2434 error_white_both(p, 1);
2435 clear_tv(rettv);
2436 evalarg_used->eval_flags = orig_flags;
2437 return FAIL;
2438 }
2439 *arg = p;
2440 }
2441
2442 /*
2443 * Get the third variable. Recursive!
2444 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002445 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002446 {
mityu4ac198c2021-05-28 17:52:40 +02002447 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002448 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002449 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002450 return FAIL;
2451 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002452 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2453 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002454 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002455 if (eval1(arg, &var2, evalarg_used) == FAIL)
2456 {
2457 if (evaluate && result)
2458 clear_tv(rettv);
2459 evalarg_used->eval_flags = orig_flags;
2460 return FAIL;
2461 }
2462 if (evaluate && !result)
2463 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002465
2466 if (evalarg == NULL)
2467 clear_evalarg(&local_evalarg, NULL);
2468 else
2469 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 }
2471
2472 return OK;
2473}
2474
2475/*
2476 * Handle first level expression:
2477 * expr2 || expr2 || expr2 logical OR
2478 *
2479 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002480 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 *
2482 * Return OK or FAIL.
2483 */
2484 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002485eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002487 char_u *p;
2488 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489
2490 /*
2491 * Get the first variable.
2492 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002493 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 return FAIL;
2495
2496 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002497 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002499 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002500 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002502 evalarg_T *evalarg_used = evalarg;
2503 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002504 int evaluate;
2505 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002506 long result = FALSE;
2507 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002508 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002509 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002510
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002511 if (evalarg == NULL)
2512 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002513 CLEAR_FIELD(local_evalarg);
2514 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002515 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002516 orig_flags = evalarg_used->eval_flags;
2517 evaluate = orig_flags & EVAL_EVALUATE;
2518 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002519 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002520 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002521 result = tv_get_bool_chk(rettv, &error);
2522 else if (tv_get_number_chk(rettv, &error) != 0)
2523 result = TRUE;
2524 clear_tv(rettv);
2525 if (error)
2526 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 }
2528
2529 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002530 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002532 while (p[0] == '|' && p[1] == '|')
2533 {
2534 if (getnext)
2535 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002536 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002537 {
2538 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2539 {
2540 error_white_both(p, 2);
2541 clear_tv(rettv);
2542 return FAIL;
2543 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002544 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002545 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002546
2547 /*
2548 * Get the second variable.
2549 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002550 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2551 {
mityu4ac198c2021-05-28 17:52:40 +02002552 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002553 clear_tv(rettv);
2554 return FAIL;
2555 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002556 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2557 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002558 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002559 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002560 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002561
2562 /*
2563 * Compute the result.
2564 */
2565 if (evaluate && !result)
2566 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002567 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002568 result = tv_get_bool_chk(&var2, &error);
2569 else if (tv_get_number_chk(&var2, &error) != 0)
2570 result = TRUE;
2571 clear_tv(&var2);
2572 if (error)
2573 return FAIL;
2574 }
2575 if (evaluate)
2576 {
2577 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002578 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002579 rettv->v_type = VAR_BOOL;
2580 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002581 }
2582 else
2583 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002584 rettv->v_type = VAR_NUMBER;
2585 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002586 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002587 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002588
2589 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002591
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002592 if (evalarg == NULL)
2593 clear_evalarg(&local_evalarg, NULL);
2594 else
2595 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 }
2597
2598 return OK;
2599}
2600
2601/*
2602 * Handle second level expression:
2603 * expr3 && expr3 && expr3 logical AND
2604 *
2605 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002606 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 *
2608 * Return OK or FAIL.
2609 */
2610 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002611eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002613 char_u *p;
2614 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615
2616 /*
2617 * Get the first variable.
2618 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002619 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 return FAIL;
2621
2622 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002623 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002625 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002626 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002628 evalarg_T *evalarg_used = evalarg;
2629 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002630 int orig_flags;
2631 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002632 long result = TRUE;
2633 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002634 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002635 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002636
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002637 if (evalarg == NULL)
2638 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002639 CLEAR_FIELD(local_evalarg);
2640 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002641 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002642 orig_flags = evalarg_used->eval_flags;
2643 evaluate = orig_flags & EVAL_EVALUATE;
2644 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002645 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002646 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002647 result = tv_get_bool_chk(rettv, &error);
2648 else if (tv_get_number_chk(rettv, &error) == 0)
2649 result = FALSE;
2650 clear_tv(rettv);
2651 if (error)
2652 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 }
2654
2655 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002656 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002658 while (p[0] == '&' && p[1] == '&')
2659 {
2660 if (getnext)
2661 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002662 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002663 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002664 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002665 {
2666 error_white_both(p, 2);
2667 clear_tv(rettv);
2668 return FAIL;
2669 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002670 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002671 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002672
2673 /*
2674 * Get the second variable.
2675 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002676 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2677 {
mityu4ac198c2021-05-28 17:52:40 +02002678 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002679 clear_tv(rettv);
2680 return FAIL;
2681 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002682 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2683 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002684 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002685 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002686 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002687 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002688
2689 /*
2690 * Compute the result.
2691 */
2692 if (evaluate && result)
2693 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002694 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002695 result = tv_get_bool_chk(&var2, &error);
2696 else if (tv_get_number_chk(&var2, &error) == 0)
2697 result = FALSE;
2698 clear_tv(&var2);
2699 if (error)
2700 return FAIL;
2701 }
2702 if (evaluate)
2703 {
2704 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002705 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002706 rettv->v_type = VAR_BOOL;
2707 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002708 }
2709 else
2710 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002711 rettv->v_type = VAR_NUMBER;
2712 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002713 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002714 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002715
2716 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002718
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002719 if (evalarg == NULL)
2720 clear_evalarg(&local_evalarg, NULL);
2721 else
2722 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 }
2724
2725 return OK;
2726}
2727
2728/*
2729 * Handle third level expression:
2730 * var1 == var2
2731 * var1 =~ var2
2732 * var1 != var2
2733 * var1 !~ var2
2734 * var1 > var2
2735 * var1 >= var2
2736 * var1 < var2
2737 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002738 * var1 is var2
2739 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 *
2741 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002742 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 *
2744 * Return OK or FAIL.
2745 */
2746 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002747eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002750 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002751 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002753 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754
2755 /*
2756 * Get the first variable.
2757 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002758 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 return FAIL;
2760
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002761 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002762 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763
2764 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002765 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002767 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002769 typval_T var2;
2770 int ic;
2771 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002772 int evaluate = evalarg == NULL
2773 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002774
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002775 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002776 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002777 *arg = eval_next_line(evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002778 p = *arg;
2779 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002780 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2781 {
mityu4ac198c2021-05-28 17:52:40 +02002782 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002783 clear_tv(rettv);
2784 return FAIL;
2785 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002786
Bram Moolenaar696ba232020-07-29 21:20:41 +02002787 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2788 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002789 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002790 clear_tv(rettv);
2791 return FAIL;
2792 }
2793
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002794 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 if (p[len] == '?')
2796 {
2797 ic = TRUE;
2798 ++len;
2799 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002800 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 else if (p[len] == '#')
2802 {
2803 ic = FALSE;
2804 ++len;
2805 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002806 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002808 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809
2810 /*
2811 * Get the second variable.
2812 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002813 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2814 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002815 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002816 clear_tv(rettv);
2817 return FAIL;
2818 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002819 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002820 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002822 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 return FAIL;
2824 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002825 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002826 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002827 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002828
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002829 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002830 {
2831 ret = FAIL;
2832 clear_tv(rettv);
2833 }
2834 else
2835 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002836 clear_tv(&var2);
2837 return ret;
2838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 }
2840
2841 return OK;
2842}
2843
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002844/*
2845 * Make a copy of blob "tv1" and append blob "tv2".
2846 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002847 void
2848eval_addblob(typval_T *tv1, typval_T *tv2)
2849{
2850 blob_T *b1 = tv1->vval.v_blob;
2851 blob_T *b2 = tv2->vval.v_blob;
2852 blob_T *b = blob_alloc();
2853 int i;
2854
2855 if (b != NULL)
2856 {
2857 for (i = 0; i < blob_len(b1); i++)
2858 ga_append(&b->bv_ga, blob_get(b1, i));
2859 for (i = 0; i < blob_len(b2); i++)
2860 ga_append(&b->bv_ga, blob_get(b2, i));
2861
2862 clear_tv(tv1);
2863 rettv_blob_set(tv1, b);
2864 }
2865}
2866
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002867/*
2868 * Make a copy of list "tv1" and append list "tv2".
2869 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002870 int
2871eval_addlist(typval_T *tv1, typval_T *tv2)
2872{
2873 typval_T var3;
2874
2875 // concatenate Lists
2876 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2877 {
2878 clear_tv(tv1);
2879 clear_tv(tv2);
2880 return FAIL;
2881 }
2882 clear_tv(tv1);
2883 *tv1 = var3;
2884 return OK;
2885}
2886
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887/*
2888 * Handle fourth level expression:
2889 * + number addition
2890 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002891 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002892 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 *
2894 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002895 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 *
2897 * Return OK or FAIL.
2898 */
2899 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002900eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 /*
2903 * Get the first variable.
2904 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002905 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 return FAIL;
2907
2908 /*
2909 * Repeat computing, until no '+', '-' or '.' is following.
2910 */
2911 for (;;)
2912 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002913 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002914 int getnext;
2915 char_u *p;
2916 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002917 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002918 int concat;
2919 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002920 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002921
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002922 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002923 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002924 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002925 p = eval_next_non_blank(*arg, evalarg, &getnext);
2926 op = *p;
2927 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002928 if ((op != '+' && op != '-' && !concat) || p[1] == '='
2929 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002931 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
2932 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002933
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002934 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002935 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002936 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002937 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002938 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002939 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002940 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002941 {
mityu4ac198c2021-05-28 17:52:40 +02002942 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002943 clear_tv(rettv);
2944 return FAIL;
2945 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002946 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002947 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002948 if ((op != '+' || (rettv->v_type != VAR_LIST
2949 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002950#ifdef FEAT_FLOAT
2951 && (op == '.' || rettv->v_type != VAR_FLOAT)
2952#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002953 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002954 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002955 int error = FALSE;
2956
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002957 // For "list + ...", an illegal use of the first operand as
2958 // a number cannot be determined before evaluating the 2nd
2959 // operand: if this is also a list, all is ok.
2960 // For "something . ...", "something - ..." or "non-list + ...",
2961 // we know that the first operand needs to be a string or number
2962 // without evaluating the 2nd operand. So check before to avoid
2963 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002964 if (op != '.')
2965 tv_get_number_chk(rettv, &error);
2966 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002967 {
2968 clear_tv(rettv);
2969 return FAIL;
2970 }
2971 }
2972
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 /*
2974 * Get the second variable.
2975 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002976 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002977 {
mityu89dcb4d2021-05-28 13:50:17 +02002978 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002979 clear_tv(rettv);
2980 return FAIL;
2981 }
2982 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002983 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002985 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 return FAIL;
2987 }
2988
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002989 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 {
2991 /*
2992 * Compute the result.
2993 */
2994 if (op == '.')
2995 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002996 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2997 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002998 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002999
Bram Moolenaar2e086612020-08-25 22:37:48 +02003000 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003001 || var2.v_type == VAR_CHANNEL
3002 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003003 semsg(_(e_using_invalid_value_as_string_str),
3004 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003005#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02003006 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003007 {
3008 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3009 var2.vval.v_float);
3010 s2 = buf2;
3011 }
3012#endif
3013 else
3014 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003015 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003016 {
3017 clear_tv(rettv);
3018 clear_tv(&var2);
3019 return FAIL;
3020 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003021 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003022 clear_tv(rettv);
3023 rettv->v_type = VAR_STRING;
3024 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003026 else if (op == '+' && rettv->v_type == VAR_BLOB
3027 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003028 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003029 else if (op == '+' && rettv->v_type == VAR_LIST
3030 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003031 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003033 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 else
3036 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003037 int error = FALSE;
3038 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003039#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003040 float_T f1 = 0, f2 = 0;
3041
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003042 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003043 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003044 f1 = rettv->vval.v_float;
3045 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003046 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003047 else
3048#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003049 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003050 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003051 if (error)
3052 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003053 // This can only happen for "list + non-list" or
3054 // "blob + non-blob". For "non-list + ..." or
3055 // "something - ...", we returned before evaluating the
3056 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003057 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003058 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003059 return FAIL;
3060 }
3061#ifdef FEAT_FLOAT
3062 if (var2.v_type == VAR_FLOAT)
3063 f1 = n1;
3064#endif
3065 }
3066#ifdef FEAT_FLOAT
3067 if (var2.v_type == VAR_FLOAT)
3068 {
3069 f2 = var2.vval.v_float;
3070 n2 = 0;
3071 }
3072 else
3073#endif
3074 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003075 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003076 if (error)
3077 {
3078 clear_tv(rettv);
3079 clear_tv(&var2);
3080 return FAIL;
3081 }
3082#ifdef FEAT_FLOAT
3083 if (rettv->v_type == VAR_FLOAT)
3084 f2 = n2;
3085#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003086 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003087 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003088
3089#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003090 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003091 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3092 {
3093 if (op == '+')
3094 f1 = f1 + f2;
3095 else
3096 f1 = f1 - f2;
3097 rettv->v_type = VAR_FLOAT;
3098 rettv->vval.v_float = f1;
3099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003101#endif
3102 {
3103 if (op == '+')
3104 n1 = n1 + n2;
3105 else
3106 n1 = n1 - n2;
3107 rettv->v_type = VAR_NUMBER;
3108 rettv->vval.v_number = n1;
3109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003111 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 }
3113 }
3114 return OK;
3115}
3116
3117/*
3118 * Handle fifth level expression:
3119 * * number multiplication
3120 * / number division
3121 * % number modulo
3122 *
3123 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003124 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 *
3126 * Return OK or FAIL.
3127 */
3128 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003129eval6(
3130 char_u **arg,
3131 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003132 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003133 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003135#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003136 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138
3139 /*
3140 * Get the first variable.
3141 */
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003142 if (eval7t(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 return FAIL;
3144
3145 /*
3146 * Repeat computing, until no '*', '/' or '%' is following.
3147 */
3148 for (;;)
3149 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003150 int evaluate;
3151 int getnext;
3152 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003153 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003154 int op;
3155 varnumber_T n1, n2;
3156#ifdef FEAT_FLOAT
3157 float_T f1, f2;
3158#endif
3159 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003160
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003161 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003162 p = eval_next_non_blank(*arg, evalarg, &getnext);
3163 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003164 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003166
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003167 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003168 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003169 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003170 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003171 {
3172 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3173 {
mityu4ac198c2021-05-28 17:52:40 +02003174 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003175 clear_tv(rettv);
3176 return FAIL;
3177 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003178 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003181#ifdef FEAT_FLOAT
3182 f1 = 0;
3183 f2 = 0;
3184#endif
3185 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003186 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003188#ifdef FEAT_FLOAT
3189 if (rettv->v_type == VAR_FLOAT)
3190 {
3191 f1 = rettv->vval.v_float;
3192 use_float = TRUE;
3193 n1 = 0;
3194 }
3195 else
3196#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003197 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003198 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003199 if (error)
3200 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 }
3202 else
3203 n1 = 0;
3204
3205 /*
3206 * Get the second variable.
3207 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003208 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3209 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003210 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003211 clear_tv(rettv);
3212 return FAIL;
3213 }
3214 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003215 if (eval7t(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 return FAIL;
3217
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003218 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003220#ifdef FEAT_FLOAT
3221 if (var2.v_type == VAR_FLOAT)
3222 {
3223 if (!use_float)
3224 {
3225 f1 = n1;
3226 use_float = TRUE;
3227 }
3228 f2 = var2.vval.v_float;
3229 n2 = 0;
3230 }
3231 else
3232#endif
3233 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003234 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003235 clear_tv(&var2);
3236 if (error)
3237 return FAIL;
3238#ifdef FEAT_FLOAT
3239 if (use_float)
3240 f2 = n2;
3241#endif
3242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243
3244 /*
3245 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003246 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003248#ifdef FEAT_FLOAT
3249 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003251 if (op == '*')
3252 f1 = f1 * f2;
3253 else if (op == '/')
3254 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003255# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003256 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003257 if (f2 == 0.0)
3258 {
3259 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003260 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003261 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003262 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003263 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003264 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003265 }
3266 else
3267 f1 = f1 / f2;
3268# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003269 // We rely on the floating point library to handle divide
3270 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003271 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003272# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003275 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003276 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003277 return FAIL;
3278 }
3279 rettv->v_type = VAR_FLOAT;
3280 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 }
3282 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003283#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003285 int failed = FALSE;
3286
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003287 if (op == '*')
3288 n1 = n1 * n2;
3289 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003290 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003292 n1 = num_modulus(n1, n2, &failed);
3293 if (failed)
3294 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003295
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003296 rettv->v_type = VAR_NUMBER;
3297 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 }
3300 }
3301
3302 return OK;
3303}
3304
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003305/*
3306 * Handle a type cast before a base level expression.
3307 * "arg" must point to the first non-white of the expression.
3308 * "arg" is advanced to just after the recognized expression.
3309 * Return OK or FAIL.
3310 */
3311 static int
3312eval7t(
3313 char_u **arg,
3314 typval_T *rettv,
3315 evalarg_T *evalarg,
3316 int want_string) // after "." operator
3317{
3318 type_T *want_type = NULL;
3319 garray_T type_list; // list of pointers to allocated types
3320 int res;
3321 int evaluate = evalarg == NULL ? 0
3322 : (evalarg->eval_flags & EVAL_EVALUATE);
3323
3324 // Recognize <type> in Vim9 script only.
3325 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1]))
3326 {
3327 ++*arg;
3328 ga_init2(&type_list, sizeof(type_T *), 10);
3329 want_type = parse_type(arg, &type_list, TRUE);
3330 if (want_type == NULL && (evaluate || **arg != '>'))
3331 {
3332 clear_type_list(&type_list);
3333 return FAIL;
3334 }
3335
3336 if (**arg != '>')
3337 {
3338 if (*skipwhite(*arg) == '>')
3339 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3340 else
3341 emsg(_(e_missing_gt));
3342 clear_type_list(&type_list);
3343 return FAIL;
3344 }
3345 ++*arg;
3346 *arg = skipwhite_and_linebreak(*arg, evalarg);
3347 }
3348
3349 res = eval7(arg, rettv, evalarg, want_string);
3350
3351 if (want_type != NULL && evaluate)
3352 {
3353 if (res == OK)
3354 {
3355 type_T *actual = typval2type(rettv, get_copyID(), &type_list, TRUE);
3356
3357 if (!equal_type(want_type, actual))
3358 {
3359 if (want_type == &t_bool && actual != &t_bool
3360 && (actual->tt_flags & TTFLAG_BOOL_OK))
3361 {
3362 int n = tv2bool(rettv);
3363
3364 // can use "0" and "1" for boolean in some places
3365 clear_tv(rettv);
3366 rettv->v_type = VAR_BOOL;
3367 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3368 }
3369 else
3370 {
3371 where_T where;
3372
3373 where.wt_index = 0;
3374 where.wt_variable = TRUE;
3375 res = check_type(want_type, actual, TRUE, where);
3376 }
3377 }
3378 }
3379 clear_type_list(&type_list);
3380 }
3381
3382 return res;
3383}
3384
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003385 int
3386eval_leader(char_u **arg, int vim9)
3387{
3388 char_u *s = *arg;
3389 char_u *p = *arg;
3390
3391 while (*p == '!' || *p == '-' || *p == '+')
3392 {
3393 char_u *n = skipwhite(p + 1);
3394
3395 // ++, --, -+ and +- are not accepted in Vim9 script
3396 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3397 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003398 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003399 return FAIL;
3400 }
3401 p = n;
3402 }
3403 *arg = p;
3404 return OK;
3405}
3406
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407/*
3408 * Handle sixth level expression:
3409 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003410 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003411 * "string" string constant
3412 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 * &option-name option value
3414 * @r register contents
3415 * identifier variable value
3416 * function() function call
3417 * $VAR environment variable
3418 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003419 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003420 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003421 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003422 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 *
3424 * Also handle:
3425 * ! in front logical NOT
3426 * - in front unary minus
3427 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003428 * trailing [] subscript in String or List
3429 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003430 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 *
3432 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003433 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 *
3435 * Return OK or FAIL.
3436 */
3437 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003438eval7(
3439 char_u **arg,
3440 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003441 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003442 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003444 int evaluate = evalarg != NULL
3445 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 int len;
3447 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 char_u *start_leader, *end_leader;
3449 int ret = OK;
3450 char_u *alias;
3451
3452 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003453 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003454 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003456 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457
3458 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003459 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 */
3461 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003462 if (eval_leader(arg, in_vim9script()) == FAIL)
3463 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 end_leader = *arg;
3465
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003466 if (**arg == '.' && (!isdigit(*(*arg + 1))
3467#ifdef FEAT_FLOAT
3468 || current_sctx.sc_version < 2
3469#endif
3470 ))
3471 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003472 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003473 ++*arg;
3474 return FAIL;
3475 }
3476
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 switch (**arg)
3478 {
3479 /*
3480 * Number constant.
3481 */
3482 case '0':
3483 case '1':
3484 case '2':
3485 case '3':
3486 case '4':
3487 case '5':
3488 case '6':
3489 case '7':
3490 case '8':
3491 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003492 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003493
3494 // Apply prefixed "-" and "+" now. Matters especially when
3495 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003496 if (ret == OK && evaluate && end_leader > start_leader
3497 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003498 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 break;
3500
3501 /*
3502 * String constant: "string".
3503 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003504 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 break;
3506
3507 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003508 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003510 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003511 break;
3512
3513 /*
3514 * List: [expr, expr]
3515 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003516 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 break;
3518
3519 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003520 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003521 */
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003522 case '#': if (in_vim9script())
3523 {
3524 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3525 }
3526 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003527 {
3528 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003529 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003530 }
3531 else
3532 ret = NOTDONE;
3533 break;
3534
3535 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003536 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003537 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003538 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003539 case '{': if (in_vim9script())
3540 ret = NOTDONE;
3541 else
3542 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003543 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003544 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003545 break;
3546
3547 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003548 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003550 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 break;
3552
3553 /*
3554 * Environment variable: $VAR.
3555 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003556 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 break;
3558
3559 /*
3560 * Register contents: @r.
3561 */
3562 case '@': ++*arg;
3563 if (evaluate)
3564 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003565 if (in_vim9script() && IS_WHITE_OR_NUL(**arg))
3566 semsg(_(e_syntax_error_at_str), *arg);
3567 else if (in_vim9script() && !valid_yank_reg(**arg, FALSE))
3568 emsg_invreg(**arg);
3569 else
3570 {
3571 rettv->v_type = VAR_STRING;
3572 rettv->vval.v_string = get_reg_contents(**arg,
3573 GREG_EXPR_SRC);
3574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 }
3576 if (**arg != NUL)
3577 ++*arg;
3578 break;
3579
3580 /*
3581 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003582 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003584 case '(': ret = NOTDONE;
3585 if (in_vim9script())
Bram Moolenaar06409502021-02-17 17:00:27 +01003586 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003587 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003588 if (ret == OK && evaluate)
3589 {
3590 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3591
Bram Moolenaara9931532021-06-12 15:58:16 +02003592 // Compile it here to get the return type. The return
3593 // type is optional, when it's missing use t_unknown.
3594 // This is recognized in compile_return().
3595 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3596 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003597 if (compile_def_function(ufunc,
Bram Moolenaare99d4222021-06-13 14:01:26 +02003598 FALSE, COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003599 {
3600 clear_tv(rettv);
3601 ret = FAIL;
3602 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003603 }
3604 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003605 if (ret == NOTDONE)
3606 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003607 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003608 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003609
Bram Moolenaar9215f012020-06-27 21:18:00 +02003610 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003611 if (**arg == ')')
3612 ++*arg;
3613 else if (ret == OK)
3614 {
3615 emsg(_(e_missing_close));
3616 clear_tv(rettv);
3617 ret = FAIL;
3618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 }
3620 break;
3621
Bram Moolenaar8c711452005-01-14 21:53:12 +00003622 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 break;
3624 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003625
3626 if (ret == NOTDONE)
3627 {
3628 /*
3629 * Must be a variable or function name.
3630 * Can also be a curly-braces kind of name: {expr}.
3631 */
3632 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003633 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003634 if (alias != NULL)
3635 s = alias;
3636
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003637 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003638 ret = FAIL;
3639 else
3640 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003641 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3642
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003643 if (evaluate && in_vim9script() && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003644 {
3645 emsg(_(e_cannot_use_underscore_here));
3646 ret = FAIL;
3647 }
3648 else if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003649 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003650 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003651 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003652 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003653 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003654 else if (flags & EVAL_CONSTANT)
3655 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003656 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003657 {
3658 // get the value of "true", "false" or a variable
3659 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3660 {
3661 rettv->v_type = VAR_BOOL;
3662 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003663 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003664 }
3665 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003666 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003667 {
3668 rettv->v_type = VAR_BOOL;
3669 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003670 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003671 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003672 else if (len == 4 && in_vim9script()
3673 && STRNCMP(s, "null", 4) == 0)
3674 {
3675 rettv->v_type = VAR_SPECIAL;
3676 rettv->vval.v_number = VVAL_NULL;
3677 ret = OK;
3678 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003679 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003680 ret = eval_variable(s, len, rettv, NULL,
3681 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003682 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003683 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003684 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003685 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003686 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003687 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003688 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003689 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003690 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003691 }
3692
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003693 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3694 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003695 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003696 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697
3698 /*
3699 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3700 */
3701 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003702 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003703 return ret;
3704}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003705
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003706/*
3707 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003708 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003709 * Adjusts "end_leaderp" until it is at "start_leader".
3710 */
3711 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003712eval7_leader(
3713 typval_T *rettv,
3714 int numeric_only,
3715 char_u *start_leader,
3716 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003717{
3718 char_u *end_leader = *end_leaderp;
3719 int ret = OK;
3720 int error = FALSE;
3721 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003722 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003723#ifdef FEAT_FLOAT
3724 float_T f = 0.0;
3725
3726 if (rettv->v_type == VAR_FLOAT)
3727 f = rettv->vval.v_float;
3728 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003729#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003730 {
3731 while (VIM_ISWHITE(end_leader[-1]))
3732 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003733 if (in_vim9script() && end_leader[-1] == '!')
3734 val = tv2bool(rettv);
3735 else
3736 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003737 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003738 if (error)
3739 {
3740 clear_tv(rettv);
3741 ret = FAIL;
3742 }
3743 else
3744 {
3745 while (end_leader > start_leader)
3746 {
3747 --end_leader;
3748 if (*end_leader == '!')
3749 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003750 if (numeric_only)
3751 {
3752 ++end_leader;
3753 break;
3754 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003755#ifdef FEAT_FLOAT
3756 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003757 {
3758 if (in_vim9script())
3759 {
3760 rettv->v_type = VAR_BOOL;
3761 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3762 }
3763 else
3764 f = !f;
3765 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003766 else
3767#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003768 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003769 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003770 type = VAR_BOOL;
3771 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003772 }
3773 else if (*end_leader == '-')
3774 {
3775#ifdef FEAT_FLOAT
3776 if (rettv->v_type == VAR_FLOAT)
3777 f = -f;
3778 else
3779#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003780 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003781 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003782 type = VAR_NUMBER;
3783 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003784 }
3785 }
3786#ifdef FEAT_FLOAT
3787 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003789 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003790 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003792 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003793#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003794 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003795 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003796 if (in_vim9script())
3797 rettv->v_type = type;
3798 else
3799 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003800 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003803 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 return ret;
3805}
3806
3807/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003808 * Call the function referred to in "rettv".
3809 */
3810 static int
3811call_func_rettv(
3812 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003813 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003814 typval_T *rettv,
3815 int evaluate,
3816 dict_T *selfdict,
3817 typval_T *basetv)
3818{
3819 partial_T *pt = NULL;
3820 funcexe_T funcexe;
3821 typval_T functv;
3822 char_u *s;
3823 int ret;
3824
3825 // need to copy the funcref so that we can clear rettv
3826 if (evaluate)
3827 {
3828 functv = *rettv;
3829 rettv->v_type = VAR_UNKNOWN;
3830
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003831 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003832 if (functv.v_type == VAR_PARTIAL)
3833 {
3834 pt = functv.vval.v_partial;
3835 s = partial_name(pt);
3836 }
3837 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003838 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003839 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003840 if (s == NULL || *s == NUL)
3841 {
3842 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02003843 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003844 goto theend;
3845 }
3846 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003847 }
3848 else
3849 s = (char_u *)"";
3850
Bram Moolenaara80faa82020-04-12 19:37:17 +02003851 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003852 funcexe.firstline = curwin->w_cursor.lnum;
3853 funcexe.lastline = curwin->w_cursor.lnum;
3854 funcexe.evaluate = evaluate;
3855 funcexe.partial = pt;
3856 funcexe.selfdict = selfdict;
3857 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003858 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003859
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003860theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003861 // Clear the funcref afterwards, so that deleting it while
3862 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003863 if (evaluate)
3864 clear_tv(&functv);
3865
3866 return ret;
3867}
3868
3869/*
3870 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003871 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003872 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3873 */
3874 static int
3875eval_lambda(
3876 char_u **arg,
3877 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003878 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003879 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003880{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003881 int evaluate = evalarg != NULL
3882 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003883 typval_T base = *rettv;
3884 int ret;
3885
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003886 rettv->v_type = VAR_UNKNOWN;
3887
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003888 if (**arg == '{')
3889 {
3890 // ->{lambda}()
3891 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3892 }
3893 else
3894 {
3895 // ->(lambda)()
3896 ++*arg;
3897 ret = eval1(arg, rettv, evalarg);
3898 *arg = skipwhite_and_linebreak(*arg, evalarg);
3899 if (**arg != ')')
3900 {
3901 emsg(_(e_missing_close));
3902 ret = FAIL;
3903 }
3904 ++*arg;
3905 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003906 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003907 return FAIL;
3908 else if (**arg != '(')
3909 {
3910 if (verbose)
3911 {
3912 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003913 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003914 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003915 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003916 }
3917 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003918 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003919 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003920 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003921 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003922
3923 // Clear the funcref afterwards, so that deleting it while
3924 // evaluating the arguments is possible (see test55).
3925 if (evaluate)
3926 clear_tv(&base);
3927
3928 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003929}
3930
3931/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003932 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003933 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02003934 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3935 */
3936 static int
3937eval_method(
3938 char_u **arg,
3939 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003940 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003941 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003942{
3943 char_u *name;
3944 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003945 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003946 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003947 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003948 int evaluate = evalarg != NULL
3949 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003950
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003951 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003952
Bram Moolenaarac92e252019-08-03 21:58:38 +02003953 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003954 len = get_name_len(arg, &alias, evaluate, TRUE);
3955 if (alias != NULL)
3956 name = alias;
3957
3958 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003959 {
3960 if (verbose)
3961 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003962 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003963 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003964 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003965 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003966 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003967 if (**arg != '(')
3968 {
3969 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003970 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003971 ret = FAIL;
3972 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003973 else if (VIM_ISWHITE((*arg)[-1]))
3974 {
3975 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003976 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003977 ret = FAIL;
3978 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003979 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003980 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003981 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003982 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003983
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003984 // Clear the funcref afterwards, so that deleting it while
3985 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003986 if (evaluate)
3987 clear_tv(&base);
3988
Bram Moolenaarac92e252019-08-03 21:58:38 +02003989 return ret;
3990}
3991
3992/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003993 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3994 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003995 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3996 */
3997 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003998eval_index(
3999 char_u **arg,
4000 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004001 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004002 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004003{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004004 int evaluate = evalarg != NULL
4005 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004006 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004007 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004008 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004009 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004010 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004011 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004012
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004013 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4014 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004015
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004016 init_tv(&var1);
4017 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004018 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004019 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004020 /*
4021 * dict.name
4022 */
4023 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004024 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004025 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004026 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004027 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004028 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004029 }
4030 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004031 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004032 /*
4033 * something[idx]
4034 *
4035 * Get the (first) variable from inside the [].
4036 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004037 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004038 if (**arg == ':')
4039 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004040 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004041 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004042 else if (vim9 && **arg == ':')
4043 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004044 semsg(_(e_white_space_required_before_and_after_str_at_str),
4045 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004046 clear_tv(&var1);
4047 return FAIL;
4048 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004049 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004050 {
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004051#ifdef FEAT_FLOAT
4052 // allow for indexing with float
4053 if (vim9 && rettv->v_type == VAR_DICT
4054 && var1.v_type == VAR_FLOAT)
4055 {
4056 var1.vval.v_string = typval_tostring(&var1, TRUE);
4057 var1.v_type = VAR_STRING;
4058 }
4059#endif
4060 if (tv_get_string_chk(&var1) == NULL)
4061 {
4062 // not a number or string
4063 clear_tv(&var1);
4064 return FAIL;
4065 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004066 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004067
4068 /*
4069 * Get the second variable from inside the [:].
4070 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004071 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004072 if (**arg == ':')
4073 {
4074 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004075 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01004076 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004077 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004078 semsg(_(e_white_space_required_before_and_after_str_at_str),
4079 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004080 if (!empty1)
4081 clear_tv(&var1);
4082 return FAIL;
4083 }
4084 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004085 if (**arg == ']')
4086 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004087 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004088 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004089 if (!empty1)
4090 clear_tv(&var1);
4091 return FAIL;
4092 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004093 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004094 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004095 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004096 if (!empty1)
4097 clear_tv(&var1);
4098 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004099 return FAIL;
4100 }
4101 }
4102
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004103 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004104 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004105 if (**arg != ']')
4106 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004107 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004108 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004109 clear_tv(&var1);
4110 if (range)
4111 clear_tv(&var2);
4112 return FAIL;
4113 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004114 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004115 }
4116
4117 if (evaluate)
4118 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004119 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004120 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004121 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004122
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004123 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004124 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004125 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004126 clear_tv(&var2);
4127 return res;
4128 }
4129 return OK;
4130}
4131
4132/*
4133 * Check if "rettv" can have an [index] or [sli:ce]
4134 */
4135 int
4136check_can_index(typval_T *rettv, int evaluate, int verbose)
4137{
4138 switch (rettv->v_type)
4139 {
4140 case VAR_FUNC:
4141 case VAR_PARTIAL:
4142 if (verbose)
4143 emsg(_("E695: Cannot index a Funcref"));
4144 return FAIL;
4145 case VAR_FLOAT:
4146#ifdef FEAT_FLOAT
4147 if (verbose)
4148 emsg(_(e_float_as_string));
4149 return FAIL;
4150#endif
4151 case VAR_BOOL:
4152 case VAR_SPECIAL:
4153 case VAR_JOB:
4154 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004155 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004156 if (verbose)
4157 emsg(_(e_cannot_index_special_variable));
4158 return FAIL;
4159 case VAR_UNKNOWN:
4160 case VAR_ANY:
4161 case VAR_VOID:
4162 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004163 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004164 emsg(_(e_cannot_index_special_variable));
4165 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004166 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004167 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004168
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004169 case VAR_STRING:
4170 case VAR_LIST:
4171 case VAR_DICT:
4172 case VAR_BLOB:
4173 break;
4174 case VAR_NUMBER:
4175 if (in_vim9script())
4176 emsg(_(e_cannot_index_number));
4177 break;
4178 }
4179 return OK;
4180}
4181
4182/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004183 * slice() function
4184 */
4185 void
4186f_slice(typval_T *argvars, typval_T *rettv)
4187{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004188 if (in_vim9script()
4189 && ((argvars[0].v_type != VAR_LIST
4190 && argvars[0].v_type != VAR_BLOB
4191 && argvars[0].v_type != VAR_STRING
4192 && check_for_list_arg(argvars, 0) == FAIL)
4193 || check_for_number_arg(argvars, 1) == FAIL
4194 || check_for_opt_number_arg(argvars, 2) == FAIL))
4195 return;
4196
Bram Moolenaar6601b622021-01-13 21:47:15 +01004197 if (check_can_index(argvars, TRUE, FALSE) == OK)
4198 {
4199 copy_tv(argvars, rettv);
4200 eval_index_inner(rettv, TRUE, argvars + 1,
4201 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4202 TRUE, NULL, 0, FALSE);
4203 }
4204}
4205
4206/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004207 * Apply index or range to "rettv".
4208 * "var1" is the first index, NULL for [:expr].
4209 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004210 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4211 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004212 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4213 */
4214 int
4215eval_index_inner(
4216 typval_T *rettv,
4217 int is_range,
4218 typval_T *var1,
4219 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004220 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004221 char_u *key,
4222 int keylen,
4223 int verbose)
4224{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004225 varnumber_T n1, n2 = 0;
4226 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004227
4228 n1 = 0;
4229 if (var1 != NULL && rettv->v_type != VAR_DICT)
4230 n1 = tv_get_number(var1);
4231
4232 if (is_range)
4233 {
4234 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004235 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004236 if (verbose)
4237 emsg(_(e_cannot_slice_dictionary));
4238 return FAIL;
4239 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004240 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004241 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004242 else
4243 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004244 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004245
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004246 switch (rettv->v_type)
4247 {
4248 case VAR_UNKNOWN:
4249 case VAR_ANY:
4250 case VAR_VOID:
4251 case VAR_FUNC:
4252 case VAR_PARTIAL:
4253 case VAR_FLOAT:
4254 case VAR_BOOL:
4255 case VAR_SPECIAL:
4256 case VAR_JOB:
4257 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004258 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004259 break; // not evaluating, skipping over subscript
4260
4261 case VAR_NUMBER:
4262 case VAR_STRING:
4263 {
4264 char_u *s = tv_get_string(rettv);
4265
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004266 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004267 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004268 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004269 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004270 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004271 else
4272 s = char_from_string(s, n1);
4273 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004274 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004275 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004276 // The resulting variable is a substring. If the indexes
4277 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004278 if (n1 < 0)
4279 {
4280 n1 = len + n1;
4281 if (n1 < 0)
4282 n1 = 0;
4283 }
4284 if (n2 < 0)
4285 n2 = len + n2;
4286 else if (n2 >= len)
4287 n2 = len;
4288 if (n1 >= len || n2 < 0 || n1 > n2)
4289 s = NULL;
4290 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004291 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004292 }
4293 else
4294 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004295 // The resulting variable is a string of a single
4296 // character. If the index is too big or negative the
4297 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004298 if (n1 >= len || n1 < 0)
4299 s = NULL;
4300 else
4301 s = vim_strnsave(s + n1, 1);
4302 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004303 clear_tv(rettv);
4304 rettv->v_type = VAR_STRING;
4305 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004306 }
4307 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004308
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004309 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004310 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4311 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004312 break;
4313
4314 case VAR_LIST:
4315 if (var1 == NULL)
4316 n1 = 0;
4317 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004318 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004319 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004320 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004321 return FAIL;
4322 break;
4323
4324 case VAR_DICT:
4325 {
4326 dictitem_T *item;
4327 typval_T tmp;
4328
4329 if (key == NULL)
4330 {
4331 key = tv_get_string_chk(var1);
4332 if (key == NULL)
4333 return FAIL;
4334 }
4335
4336 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
4337
4338 if (item == NULL && verbose)
4339 semsg(_(e_dictkey), key);
4340 if (item == NULL)
4341 return FAIL;
4342
4343 copy_tv(&item->di_tv, &tmp);
4344 clear_tv(rettv);
4345 *rettv = tmp;
4346 }
4347 break;
4348 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004349 return OK;
4350}
4351
4352/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004353 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004354 */
4355 char_u *
4356partial_name(partial_T *pt)
4357{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004358 if (pt != NULL)
4359 {
4360 if (pt->pt_name != NULL)
4361 return pt->pt_name;
4362 if (pt->pt_func != NULL)
4363 return pt->pt_func->uf_name;
4364 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004365 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004366}
4367
Bram Moolenaarddecc252016-04-06 22:59:37 +02004368 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004369partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004370{
4371 int i;
4372
4373 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004374 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004375 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004376 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004377 if (pt->pt_name != NULL)
4378 {
4379 func_unref(pt->pt_name);
4380 vim_free(pt->pt_name);
4381 }
4382 else
4383 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004384
Bram Moolenaar54656012021-06-09 20:50:46 +02004385 // "out_up" is no longer used, decrement refcount on partial that owns it.
4386 partial_unref(pt->pt_outer.out_up_partial);
4387
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004388 // Decrease the reference count for the context of a closure. If down
4389 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004390 if (pt->pt_funcstack != NULL)
4391 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004392 --pt->pt_funcstack->fs_refcount;
4393 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004394 }
4395
Bram Moolenaarddecc252016-04-06 22:59:37 +02004396 vim_free(pt);
4397}
4398
4399/*
4400 * Unreference a closure: decrement the reference count and free it when it
4401 * becomes zero.
4402 */
4403 void
4404partial_unref(partial_T *pt)
4405{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004406 if (pt != NULL)
4407 {
4408 if (--pt->pt_refcount <= 0)
4409 partial_free(pt);
4410
4411 // If the reference count goes down to one, the funcstack may be the
4412 // only reference and can be freed if no other partials reference it.
4413 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4414 funcstack_check_refcount(pt->pt_funcstack);
4415 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004416}
4417
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004418/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004419 * Return the next (unique) copy ID.
4420 * Used for serializing nested structures.
4421 */
4422 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004423get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004424{
4425 current_copyID += COPYID_INC;
4426 return current_copyID;
4427}
4428
4429/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004430 * Garbage collection for lists and dictionaries.
4431 *
4432 * We use reference counts to be able to free most items right away when they
4433 * are no longer used. But for composite items it's possible that it becomes
4434 * unused while the reference count is > 0: When there is a recursive
4435 * reference. Example:
4436 * :let l = [1, 2, 3]
4437 * :let d = {9: l}
4438 * :let l[1] = d
4439 *
4440 * Since this is quite unusual we handle this with garbage collection: every
4441 * once in a while find out which lists and dicts are not referenced from any
4442 * variable.
4443 *
4444 * Here is a good reference text about garbage collection (refers to Python
4445 * but it applies to all reference-counting mechanisms):
4446 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004447 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004448
4449/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004450 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004451 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004452 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004453 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004454 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004455garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004456{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004457 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004458 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004459 buf_T *buf;
4460 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004461 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004462 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004463
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004464 if (!testing)
4465 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004466 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004467 want_garbage_collect = FALSE;
4468 may_garbage_collect = FALSE;
4469 garbage_collect_at_exit = FALSE;
4470 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004471
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004472 // The execution stack can grow big, limit the size.
4473 if (exestack.ga_maxlen - exestack.ga_len > 500)
4474 {
4475 size_t new_len;
4476 char_u *pp;
4477 int n;
4478
4479 // Keep 150% of the current size, with a minimum of the growth size.
4480 n = exestack.ga_len / 2;
4481 if (n < exestack.ga_growsize)
4482 n = exestack.ga_growsize;
4483
4484 // Don't make it bigger though.
4485 if (exestack.ga_len + n < exestack.ga_maxlen)
4486 {
4487 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4488 pp = vim_realloc(exestack.ga_data, new_len);
4489 if (pp == NULL)
4490 return FAIL;
4491 exestack.ga_maxlen = exestack.ga_len + n;
4492 exestack.ga_data = pp;
4493 }
4494 }
4495
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004496 // We advance by two because we add one for items referenced through
4497 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004498 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004499
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004500 /*
4501 * 1. Go through all accessible variables and mark all lists and dicts
4502 * with copyID.
4503 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004504
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004505 // Don't free variables in the previous_funccal list unless they are only
4506 // referenced through previous_funccal. This must be first, because if
4507 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004508 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004509
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004510 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004511 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004512
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004513 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004514 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004515 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4516 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004517
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004518 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004519 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004520 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4521 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004522 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004523 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4524 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004525#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004526 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004527 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4528 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004529 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004530 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004531 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4532 NULL, NULL);
4533#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004534
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004535 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004536 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004537 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4538 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004539 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004540 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004541
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004542 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004543 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004544
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004545 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004546 abort = abort || set_ref_in_functions(copyID);
4547
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004548 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004549 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004550
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004551 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004552 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004553
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004554 // callbacks in buffers
4555 abort = abort || set_ref_in_buffers(copyID);
4556
Bram Moolenaar1dced572012-04-05 16:54:08 +02004557#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004558 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004559#endif
4560
Bram Moolenaardb913952012-06-29 12:54:53 +02004561#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004562 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004563#endif
4564
4565#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004566 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004567#endif
4568
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004569#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004570 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004571 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004572#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004573#ifdef FEAT_NETBEANS_INTG
4574 abort = abort || set_ref_in_nb_channel(copyID);
4575#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004576
Bram Moolenaare3188e22016-05-31 21:13:04 +02004577#ifdef FEAT_TIMERS
4578 abort = abort || set_ref_in_timer(copyID);
4579#endif
4580
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004581#ifdef FEAT_QUICKFIX
4582 abort = abort || set_ref_in_quickfix(copyID);
4583#endif
4584
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004585#ifdef FEAT_TERMINAL
4586 abort = abort || set_ref_in_term(copyID);
4587#endif
4588
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004589#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004590 abort = abort || set_ref_in_popups(copyID);
4591#endif
4592
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004593 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004594 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004595 /*
4596 * 2. Free lists and dictionaries that are not referenced.
4597 */
4598 did_free = free_unref_items(copyID);
4599
4600 /*
4601 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004602 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004603 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004604 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004605 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004606 else if (p_verbose > 0)
4607 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004608 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004609 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004610
4611 return did_free;
4612}
4613
4614/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004615 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004616 */
4617 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004618free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004619{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004620 int did_free = FALSE;
4621
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004622 // Let all "free" functions know that we are here. This means no
4623 // dictionaries, lists, channels or jobs are to be freed, because we will
4624 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004625 in_free_unref_items = TRUE;
4626
4627 /*
4628 * PASS 1: free the contents of the items. We don't free the items
4629 * themselves yet, so that it is possible to decrement refcount counters
4630 */
4631
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004632 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004633 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004634
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004635 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004636 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004637
4638#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004639 // Go through the list of jobs and free items without the copyID. This
4640 // must happen before doing channels, because jobs refer to channels, but
4641 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004642 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4643
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004644 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004645 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4646#endif
4647
4648 /*
4649 * PASS 2: free the items themselves.
4650 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004651 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004652 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004653
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004654#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004655 // Go through the list of jobs and free items without the copyID. This
4656 // must happen before doing channels, because jobs refer to channels, but
4657 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004658 free_unused_jobs(copyID, COPYID_MASK);
4659
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004660 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004661 free_unused_channels(copyID, COPYID_MASK);
4662#endif
4663
4664 in_free_unref_items = FALSE;
4665
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004666 return did_free;
4667}
4668
4669/*
4670 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004671 * "list_stack" is used to add lists to be marked. Can be NULL.
4672 *
4673 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004674 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004675 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004676set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004677{
4678 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004679 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004680 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004681 hashtab_T *cur_ht;
4682 ht_stack_T *ht_stack = NULL;
4683 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004684
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004685 cur_ht = ht;
4686 for (;;)
4687 {
4688 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004689 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004690 // Mark each item in the hashtab. If the item contains a hashtab
4691 // it is added to ht_stack, if it contains a list it is added to
4692 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004693 todo = (int)cur_ht->ht_used;
4694 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4695 if (!HASHITEM_EMPTY(hi))
4696 {
4697 --todo;
4698 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4699 &ht_stack, list_stack);
4700 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004701 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004702
4703 if (ht_stack == NULL)
4704 break;
4705
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004706 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004707 cur_ht = ht_stack->ht;
4708 tempitem = ht_stack;
4709 ht_stack = ht_stack->prev;
4710 free(tempitem);
4711 }
4712
4713 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004714}
4715
4716/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004717 * Mark a dict and its items with "copyID".
4718 * Returns TRUE if setting references failed somehow.
4719 */
4720 int
4721set_ref_in_dict(dict_T *d, int copyID)
4722{
4723 if (d != NULL && d->dv_copyID != copyID)
4724 {
4725 d->dv_copyID = copyID;
4726 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4727 }
4728 return FALSE;
4729}
4730
4731/*
4732 * Mark a list and its items with "copyID".
4733 * Returns TRUE if setting references failed somehow.
4734 */
4735 int
4736set_ref_in_list(list_T *ll, int copyID)
4737{
4738 if (ll != NULL && ll->lv_copyID != copyID)
4739 {
4740 ll->lv_copyID = copyID;
4741 return set_ref_in_list_items(ll, copyID, NULL);
4742 }
4743 return FALSE;
4744}
4745
4746/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004747 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004748 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4749 *
4750 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004751 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004752 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004753set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004754{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004755 listitem_T *li;
4756 int abort = FALSE;
4757 list_T *cur_l;
4758 list_stack_T *list_stack = NULL;
4759 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004760
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004761 cur_l = l;
4762 for (;;)
4763 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004764 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004765 // Mark each item in the list. If the item contains a hashtab
4766 // it is added to ht_stack, if it contains a list it is added to
4767 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004768 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4769 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4770 ht_stack, &list_stack);
4771 if (list_stack == NULL)
4772 break;
4773
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004774 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004775 cur_l = list_stack->list;
4776 tempitem = list_stack;
4777 list_stack = list_stack->prev;
4778 free(tempitem);
4779 }
4780
4781 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004782}
4783
4784/*
4785 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004786 * "list_stack" is used to add lists to be marked. Can be NULL.
4787 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4788 *
4789 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004790 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004791 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004792set_ref_in_item(
4793 typval_T *tv,
4794 int copyID,
4795 ht_stack_T **ht_stack,
4796 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004797{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004798 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004799
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004800 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004801 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004802 dict_T *dd = tv->vval.v_dict;
4803
Bram Moolenaara03f2332016-02-06 18:09:59 +01004804 if (dd != NULL && dd->dv_copyID != copyID)
4805 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004806 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004807 dd->dv_copyID = copyID;
4808 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004809 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004810 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4811 }
4812 else
4813 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004814 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4815
Bram Moolenaara03f2332016-02-06 18:09:59 +01004816 if (newitem == NULL)
4817 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004818 else
4819 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004820 newitem->ht = &dd->dv_hashtab;
4821 newitem->prev = *ht_stack;
4822 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004823 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004824 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004825 }
4826 }
4827 else if (tv->v_type == VAR_LIST)
4828 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004829 list_T *ll = tv->vval.v_list;
4830
Bram Moolenaara03f2332016-02-06 18:09:59 +01004831 if (ll != NULL && ll->lv_copyID != copyID)
4832 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004833 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004834 ll->lv_copyID = copyID;
4835 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004836 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004837 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004838 }
4839 else
4840 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004841 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4842
Bram Moolenaara03f2332016-02-06 18:09:59 +01004843 if (newitem == NULL)
4844 abort = TRUE;
4845 else
4846 {
4847 newitem->list = ll;
4848 newitem->prev = *list_stack;
4849 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004850 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004851 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004852 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004853 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004854 else if (tv->v_type == VAR_FUNC)
4855 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004856 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004857 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004858 else if (tv->v_type == VAR_PARTIAL)
4859 {
4860 partial_T *pt = tv->vval.v_partial;
4861 int i;
4862
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004863 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004864 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004865 // Didn't see this partial yet.
4866 pt->pt_copyID = copyID;
4867
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004868 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004869
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004870 if (pt->pt_dict != NULL)
4871 {
4872 typval_T dtv;
4873
4874 dtv.v_type = VAR_DICT;
4875 dtv.vval.v_dict = pt->pt_dict;
4876 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4877 }
4878
4879 for (i = 0; i < pt->pt_argc; ++i)
4880 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4881 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004882 if (pt->pt_funcstack != NULL)
4883 {
4884 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4885
4886 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4887 abort = abort || set_ref_in_item(stack + i, copyID,
4888 ht_stack, list_stack);
4889 }
4890
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004891 }
4892 }
4893#ifdef FEAT_JOB_CHANNEL
4894 else if (tv->v_type == VAR_JOB)
4895 {
4896 job_T *job = tv->vval.v_job;
4897 typval_T dtv;
4898
4899 if (job != NULL && job->jv_copyID != copyID)
4900 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004901 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004902 if (job->jv_channel != NULL)
4903 {
4904 dtv.v_type = VAR_CHANNEL;
4905 dtv.vval.v_channel = job->jv_channel;
4906 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4907 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004908 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004909 {
4910 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004911 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004912 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4913 }
4914 }
4915 }
4916 else if (tv->v_type == VAR_CHANNEL)
4917 {
4918 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004919 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004920 typval_T dtv;
4921 jsonq_T *jq;
4922 cbq_T *cq;
4923
4924 if (ch != NULL && ch->ch_copyID != copyID)
4925 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004926 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004927 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004928 {
4929 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4930 jq = jq->jq_next)
4931 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4932 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4933 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004934 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004935 {
4936 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004937 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004938 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4939 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004940 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004941 {
4942 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004943 dtv.vval.v_partial =
4944 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004945 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4946 }
4947 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004948 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004949 {
4950 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004951 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004952 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4953 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004954 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004955 {
4956 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004957 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004958 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4959 }
4960 }
4961 }
4962#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004963 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004964}
4965
Bram Moolenaar8c711452005-01-14 21:53:12 +00004966/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004967 * Return a string with the string representation of a variable.
4968 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004969 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004970 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004971 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004972 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02004973 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004974 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4975 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004976 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004977 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004978 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004979echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004980 typval_T *tv,
4981 char_u **tofree,
4982 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004983 int copyID,
4984 int echo_style,
4985 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004986 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004987{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004988 static int recurse = 0;
4989 char_u *r = NULL;
4990
Bram Moolenaar33570922005-01-25 22:26:29 +00004991 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004992 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004993 if (!did_echo_string_emsg)
4994 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004995 // Only give this message once for a recursive call to avoid
4996 // flooding the user with errors. And stop iterating over lists
4997 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004998 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004999 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005000 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005001 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005002 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005003 }
5004 ++recurse;
5005
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005006 switch (tv->v_type)
5007 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005008 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005009 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005010 {
5011 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005012 r = tv->vval.v_string;
5013 if (r == NULL)
5014 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005015 }
5016 else
5017 {
5018 *tofree = string_quote(tv->vval.v_string, FALSE);
5019 r = *tofree;
5020 }
5021 break;
5022
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005023 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005024 if (echo_style)
5025 {
5026 *tofree = NULL;
5027 r = tv->vval.v_string;
5028 }
5029 else
5030 {
5031 *tofree = string_quote(tv->vval.v_string, TRUE);
5032 r = *tofree;
5033 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005034 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005035
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005036 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005037 {
5038 partial_T *pt = tv->vval.v_partial;
5039 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005040 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005041 garray_T ga;
5042 int i;
5043 char_u *tf;
5044
5045 ga_init2(&ga, 1, 100);
5046 ga_concat(&ga, (char_u *)"function(");
5047 if (fname != NULL)
5048 {
5049 ga_concat(&ga, fname);
5050 vim_free(fname);
5051 }
5052 if (pt != NULL && pt->pt_argc > 0)
5053 {
5054 ga_concat(&ga, (char_u *)", [");
5055 for (i = 0; i < pt->pt_argc; ++i)
5056 {
5057 if (i > 0)
5058 ga_concat(&ga, (char_u *)", ");
5059 ga_concat(&ga,
5060 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5061 vim_free(tf);
5062 }
5063 ga_concat(&ga, (char_u *)"]");
5064 }
5065 if (pt != NULL && pt->pt_dict != NULL)
5066 {
5067 typval_T dtv;
5068
5069 ga_concat(&ga, (char_u *)", ");
5070 dtv.v_type = VAR_DICT;
5071 dtv.vval.v_dict = pt->pt_dict;
5072 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5073 vim_free(tf);
5074 }
5075 ga_concat(&ga, (char_u *)")");
5076
5077 *tofree = ga.ga_data;
5078 r = *tofree;
5079 break;
5080 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005081
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005082 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005083 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005084 break;
5085
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005086 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005087 if (tv->vval.v_list == NULL)
5088 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005089 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005090 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005091 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005092 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005093 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5094 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005095 {
5096 *tofree = NULL;
5097 r = (char_u *)"[...]";
5098 }
5099 else
5100 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005101 int old_copyID = tv->vval.v_list->lv_copyID;
5102
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005103 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005104 *tofree = list2string(tv, copyID, restore_copyID);
5105 if (restore_copyID)
5106 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005107 r = *tofree;
5108 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005109 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005110
Bram Moolenaar8c711452005-01-14 21:53:12 +00005111 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005112 if (tv->vval.v_dict == NULL)
5113 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005114 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005115 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005116 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005117 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005118 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5119 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005120 {
5121 *tofree = NULL;
5122 r = (char_u *)"{...}";
5123 }
5124 else
5125 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005126 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005127
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005128 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005129 *tofree = dict2string(tv, copyID, restore_copyID);
5130 if (restore_copyID)
5131 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005132 r = *tofree;
5133 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005134 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005135
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005136 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005137 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005138 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005139 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005140 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005141 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005142 break;
5143
Bram Moolenaar835dc632016-02-07 14:27:38 +01005144 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005145 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005146#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005147 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005148 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5149 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005150 if (composite_val)
5151 {
5152 *tofree = string_quote(r, FALSE);
5153 r = *tofree;
5154 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005155#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005156 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005157
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005158 case VAR_INSTR:
5159 *tofree = NULL;
5160 r = (char_u *)"instructions";
5161 break;
5162
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005163 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005164#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005165 *tofree = NULL;
5166 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5167 r = numbuf;
5168 break;
5169#endif
5170
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005171 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005172 case VAR_SPECIAL:
5173 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005174 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005175 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005176 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005177
Bram Moolenaar8502c702014-06-17 12:51:16 +02005178 if (--recurse == 0)
5179 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005180 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005181}
5182
5183/*
5184 * Return a string with the string representation of a variable.
5185 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5186 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005187 * Does not put quotes around strings, as ":echo" displays values.
5188 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5189 * May return NULL.
5190 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005191 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005192echo_string(
5193 typval_T *tv,
5194 char_u **tofree,
5195 char_u *numbuf,
5196 int copyID)
5197{
5198 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5199}
5200
5201/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005202 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5203 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005204 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005205 */
5206 int
5207buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5208{
5209 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005210 char_u *t;
5211 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005212
5213 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5214 return -1;
5215
5216 if (lnum > buf->b_ml.ml_line_count)
5217 lnum = buf->b_ml.ml_line_count;
5218
5219 str = ml_get_buf(buf, lnum, FALSE);
5220 if (str == NULL)
5221 return -1;
5222
5223 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005224 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005225
Bram Moolenaar91458462021-01-13 20:08:38 +01005226 // count the number of characters
5227 t = str;
5228 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5229 t += mb_ptr2len(t);
5230
5231 // In insert mode, when the cursor is at the end of a non-empty line,
5232 // byteidx points to the NUL character immediately past the end of the
5233 // string. In this case, add one to the character count.
5234 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5235 count++;
5236
5237 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005238}
5239
5240/*
5241 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005242 * byte index. Works only for loaded buffers. Returns -1 on failure.
5243 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005244 */
5245 int
5246buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5247{
5248 char_u *str;
5249 char_u *t;
5250
5251 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5252 return -1;
5253
5254 if (lnum > buf->b_ml.ml_line_count)
5255 lnum = buf->b_ml.ml_line_count;
5256
5257 str = ml_get_buf(buf, lnum, FALSE);
5258 if (str == NULL)
5259 return -1;
5260
5261 // Convert the character offset to a byte offset
5262 t = str;
5263 while (*t != NUL && --charidx > 0)
5264 t += mb_ptr2len(t);
5265
Bram Moolenaar91458462021-01-13 20:08:38 +01005266 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005267}
5268
5269/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005271 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005273 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005274var2fpos(
5275 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005276 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005277 int *fnum, // set to fnum for '0, 'A, etc.
5278 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005280 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005282 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005284 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005285 if (varp->v_type == VAR_LIST)
5286 {
5287 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005288 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005289 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005290 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005291
5292 l = varp->vval.v_list;
5293 if (l == NULL)
5294 return NULL;
5295
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005296 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005297 pos.lnum = list_find_nr(l, 0L, &error);
5298 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005299 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005300 if (charcol)
5301 len = (long)mb_charlen(ml_get(pos.lnum));
5302 else
5303 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005304
Bram Moolenaarec65d772020-08-20 22:29:12 +02005305 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005306 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005307 li = list_find(l, 1L);
5308 if (li != NULL && li->li_tv.v_type == VAR_STRING
5309 && li->li_tv.vval.v_string != NULL
5310 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005311 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005312 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005313 }
5314 else
5315 {
5316 pos.col = list_find_nr(l, 1L, &error);
5317 if (error)
5318 return NULL;
5319 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005320
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005321 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005322 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005323 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005324 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005325
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005326 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005327 pos.coladd = list_find_nr(l, 2L, &error);
5328 if (error)
5329 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005330
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005331 return &pos;
5332 }
5333
Bram Moolenaarc5809432021-03-27 21:23:30 +01005334 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5335 return NULL;
5336
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005337 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005338 if (name == NULL)
5339 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005340 if (name[0] == '.') // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005341 {
5342 pos = curwin->w_cursor;
5343 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005344 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005345 return &pos;
5346 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005347 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005348 {
5349 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005350 pos = VIsual;
5351 else
5352 pos = curwin->w_cursor;
5353 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005354 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005355 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005356 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005357 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005359 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5361 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005362 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005363 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 return pp;
5365 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005366
Bram Moolenaara5525202006-03-02 22:52:09 +00005367 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005368
Bram Moolenaar477933c2007-07-17 14:32:23 +00005369 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005370 {
5371 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005372 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005373 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005374 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005375 // In silent Ex mode topline is zero, but that's not a valid line
5376 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005377 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005378 return &pos;
5379 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005380 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005381 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005382 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005383 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005384 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005385 return &pos;
5386 }
5387 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005388 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005390 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 {
5392 pos.lnum = curbuf->b_ml.ml_line_count;
5393 pos.col = 0;
5394 }
5395 else
5396 {
5397 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005398 if (charcol)
5399 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5400 else
5401 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 }
5403 return &pos;
5404 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005405 if (in_vim9script())
5406 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 return NULL;
5408}
5409
5410/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005411 * Convert list in "arg" into a position and optional file number.
5412 * When "fnump" is NULL there is no file number, only 3 items.
5413 * Note that the column is passed on as-is, the caller may want to decrement
5414 * it to use 1 for the first column.
5415 * Return FAIL when conversion is not possible, doesn't check the position for
5416 * validity.
5417 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005418 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005419list2fpos(
5420 typval_T *arg,
5421 pos_T *posp,
5422 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005423 colnr_T *curswantp,
5424 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005425{
5426 list_T *l = arg->vval.v_list;
5427 long i = 0;
5428 long n;
5429
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005430 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5431 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005432 if (arg->v_type != VAR_LIST
5433 || l == NULL
5434 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005435 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005436 return FAIL;
5437
5438 if (fnump != NULL)
5439 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005440 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005441 if (n < 0)
5442 return FAIL;
5443 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005444 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005445 *fnump = n;
5446 }
5447
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005448 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005449 if (n < 0)
5450 return FAIL;
5451 posp->lnum = n;
5452
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005453 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005454 if (n < 0)
5455 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005456 // If character position is specified, then convert to byte position
5457 if (charcol)
5458 {
5459 buf_T *buf;
5460
5461 // Get the text for the specified line in a loaded buffer
5462 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5463 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5464 return FAIL;
5465
Bram Moolenaar91458462021-01-13 20:08:38 +01005466 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005467 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005468 posp->col = n;
5469
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005470 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005471 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005472 posp->coladd = 0;
5473 else
5474 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005475
Bram Moolenaar493c1782014-05-28 14:34:46 +02005476 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005477 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005478
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005479 return OK;
5480}
5481
5482/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 * Get the length of an environment variable name.
5484 * Advance "arg" to the first character after the name.
5485 * Return 0 for error.
5486 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005487 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005488get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489{
5490 char_u *p;
5491 int len;
5492
5493 for (p = *arg; vim_isIDc(*p); ++p)
5494 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005495 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 return 0;
5497
5498 len = (int)(p - *arg);
5499 *arg = p;
5500 return len;
5501}
5502
5503/*
5504 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005505 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 * Return 0 if something is wrong.
5507 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005508 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005509get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510{
5511 char_u *p;
5512 int len;
5513
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005514 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005516 {
5517 if (*p == ':')
5518 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005519 // "s:" is start of "s:var", but "n:" is not and can be used in
5520 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005521 len = (int)(p - *arg);
5522 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5523 || len > 1)
5524 break;
5525 }
5526 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005527 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 return 0;
5529
5530 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005531 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532
5533 return len;
5534}
5535
5536/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005537 * Get the length of the name of a variable or function.
5538 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005540 * Return -1 if curly braces expansion failed.
5541 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 * If the name contains 'magic' {}'s, expand them and return the
5543 * expanded name in an allocated string via 'alias' - caller must free.
5544 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005545 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005546get_name_len(
5547 char_u **arg,
5548 char_u **alias,
5549 int evaluate,
5550 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551{
5552 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 char_u *p;
5554 char_u *expr_start;
5555 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005557 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558
5559 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5560 && (*arg)[2] == (int)KE_SNR)
5561 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005562 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 *arg += 3;
5564 return get_id_len(arg) + 3;
5565 }
5566 len = eval_fname_script(*arg);
5567 if (len > 0)
5568 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005569 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 *arg += len;
5571 }
5572
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005574 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005576 p = find_name_end(*arg, &expr_start, &expr_end,
5577 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 if (expr_start != NULL)
5579 {
5580 char_u *temp_string;
5581
5582 if (!evaluate)
5583 {
5584 len += (int)(p - *arg);
5585 *arg = skipwhite(p);
5586 return len;
5587 }
5588
5589 /*
5590 * Include any <SID> etc in the expanded string:
5591 * Thus the -len here.
5592 */
5593 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5594 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005595 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 *alias = temp_string;
5597 *arg = skipwhite(p);
5598 return (int)STRLEN(temp_string);
5599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600
5601 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005602 // Only give an error when there is something, otherwise it will be
5603 // reported at a higher level.
5604 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005605 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606
5607 return len;
5608}
5609
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005610/*
5611 * Find the end of a variable or function name, taking care of magic braces.
5612 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5613 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005614 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005615 * Return a pointer to just after the name. Equal to "arg" if there is no
5616 * valid name.
5617 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005618 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005619find_name_end(
5620 char_u *arg,
5621 char_u **expr_start,
5622 char_u **expr_end,
5623 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005625 int mb_nest = 0;
5626 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005628 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005629 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005631 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005633 *expr_start = NULL;
5634 *expr_end = NULL;
5635 }
5636
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005637 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005638 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5639 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005640 return arg;
5641
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005642 for (p = arg; *p != NUL
5643 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005644 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005645 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005646 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005647 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005648 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005649 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005650 if (*p == '\'')
5651 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005652 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005653 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005654 ;
5655 if (*p == NUL)
5656 break;
5657 }
5658 else if (*p == '"')
5659 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005660 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005661 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005662 if (*p == '\\' && p[1] != NUL)
5663 ++p;
5664 if (*p == NUL)
5665 break;
5666 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005667 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5668 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005669 // "s:" is start of "s:var", but "n:" is not and can be used in
5670 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005671 len = (int)(p - arg);
5672 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005673 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005674 break;
5675 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005676
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005677 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005679 if (*p == '[')
5680 ++br_nest;
5681 else if (*p == ']')
5682 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005684
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005685 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005687 if (*p == '{')
5688 {
5689 mb_nest++;
5690 if (expr_start != NULL && *expr_start == NULL)
5691 *expr_start = p;
5692 }
5693 else if (*p == '}')
5694 {
5695 mb_nest--;
5696 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5697 *expr_end = p;
5698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 }
5701
5702 return p;
5703}
5704
5705/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005706 * Expands out the 'magic' {}'s in a variable/function name.
5707 * Note that this can call itself recursively, to deal with
5708 * constructs like foo{bar}{baz}{bam}
5709 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5710 * "in_start" ^
5711 * "expr_start" ^
5712 * "expr_end" ^
5713 * "in_end" ^
5714 *
5715 * Returns a new allocated string, which the caller must free.
5716 * Returns NULL for failure.
5717 */
5718 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005719make_expanded_name(
5720 char_u *in_start,
5721 char_u *expr_start,
5722 char_u *expr_end,
5723 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005724{
5725 char_u c1;
5726 char_u *retval = NULL;
5727 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005728
5729 if (expr_end == NULL || in_end == NULL)
5730 return NULL;
5731 *expr_start = NUL;
5732 *expr_end = NUL;
5733 c1 = *in_end;
5734 *in_end = NUL;
5735
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005736 temp_result = eval_to_string(expr_start + 1, FALSE);
5737 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005738 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005739 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5740 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005741 if (retval != NULL)
5742 {
5743 STRCPY(retval, in_start);
5744 STRCAT(retval, temp_result);
5745 STRCAT(retval, expr_end + 1);
5746 }
5747 }
5748 vim_free(temp_result);
5749
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005750 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005751 *expr_start = '{';
5752 *expr_end = '}';
5753
5754 if (retval != NULL)
5755 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005756 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005757 if (expr_start != NULL)
5758 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005759 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005760 temp_result = make_expanded_name(retval, expr_start,
5761 expr_end, temp_result);
5762 vim_free(retval);
5763 retval = temp_result;
5764 }
5765 }
5766
5767 return retval;
5768}
5769
5770/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005772 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005774 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005775eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005777 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005778}
5779
5780/*
5781 * Return TRUE if character "c" can be used as the first character in a
5782 * variable or function name (excluding '{' and '}').
5783 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005784 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005785eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005786{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005787 return ASCII_ISALPHA(c) || c == '_';
5788}
5789
5790/*
5791 * Return TRUE if character "c" can be used as the first character of a
5792 * dictionary key.
5793 */
5794 int
5795eval_isdictc(int c)
5796{
5797 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798}
5799
5800/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005801 * Handle:
5802 * - expr[expr], expr[expr:expr] subscript
5803 * - ".name" lookup
5804 * - function call with Funcref variable: func(expr)
5805 * - method call: var->method()
5806 *
5807 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005808 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005809 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005810handle_subscript(
5811 char_u **arg,
5812 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005813 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005814 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005815{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005816 int evaluate = evalarg != NULL
5817 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005818 int ret = OK;
5819 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005820 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005821 int getnext;
5822 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005823
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005824 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005825 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005826 // When at the end of the line and ".name" or "->{" or "->X" follows in
5827 // the next line then consume the line break.
5828 p = eval_next_non_blank(*arg, evalarg, &getnext);
5829 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005830 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02005831 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
5832 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
5833 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005834 {
5835 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005836 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005837 check_white = FALSE;
5838 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005839
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01005840 if (rettv->v_type == VAR_ANY)
5841 {
5842 char_u *exp_name;
5843 int cc;
5844 int idx;
5845 ufunc_T *ufunc;
5846 type_T *type;
5847
5848 // Found script from "import * as {name}", script item name must
5849 // follow.
5850 if (**arg != '.')
5851 {
5852 if (verbose)
5853 semsg(_(e_expected_str_but_got_str), "'.'", *arg);
5854 ret = FAIL;
5855 break;
5856 }
5857 ++*arg;
5858 if (IS_WHITE_OR_NUL(**arg))
5859 {
5860 if (verbose)
5861 emsg(_(e_no_white_space_allowed_after_dot));
5862 ret = FAIL;
5863 break;
5864 }
5865
5866 // isolate the name
5867 exp_name = *arg;
5868 while (eval_isnamec(**arg))
5869 ++*arg;
5870 cc = **arg;
5871 **arg = NUL;
5872
5873 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
5874 evalarg->eval_cctx, verbose);
5875 **arg = cc;
5876 *arg = skipwhite(*arg);
5877
5878 if (idx < 0 && ufunc == NULL)
5879 {
5880 ret = FAIL;
5881 break;
5882 }
5883 if (idx >= 0)
5884 {
5885 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
5886 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
5887
5888 copy_tv(sv->sv_tv, rettv);
5889 }
5890 else
5891 {
5892 rettv->v_type = VAR_FUNC;
5893 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
5894 }
5895 }
5896
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005897 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5898 || rettv->v_type == VAR_PARTIAL))
5899 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005900 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005901 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5902 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005903
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005904 // Stop the expression evaluation when immediately aborting on
5905 // error, or when an interrupt occurred or an exception was thrown
5906 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005907 if (aborting())
5908 {
5909 if (ret == OK)
5910 clear_tv(rettv);
5911 ret = FAIL;
5912 }
5913 dict_unref(selfdict);
5914 selfdict = NULL;
5915 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005916 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005917 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005918 if (in_vim9script())
5919 *arg = skipwhite(p + 2);
5920 else
5921 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005922 if (ret == OK)
5923 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005924 if (VIM_ISWHITE(**arg))
5925 {
5926 emsg(_(e_nowhitespace));
5927 ret = FAIL;
5928 }
5929 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005930 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005931 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005932 else
5933 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005934 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005935 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005936 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005937 // "." is ".name" lookup when we found a dict or when evaluating and
5938 // scriptversion is at least 2, where string concatenation is "..".
5939 else if (**arg == '['
5940 || (**arg == '.' && (rettv->v_type == VAR_DICT
5941 || (!evaluate
5942 && (*arg)[1] != '.'
5943 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005944 {
5945 dict_unref(selfdict);
5946 if (rettv->v_type == VAR_DICT)
5947 {
5948 selfdict = rettv->vval.v_dict;
5949 if (selfdict != NULL)
5950 ++selfdict->dv_refcount;
5951 }
5952 else
5953 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005954 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005955 {
5956 clear_tv(rettv);
5957 ret = FAIL;
5958 }
5959 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005960 else
5961 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005962 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005963
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005964 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5965 // Don't do this when "Func" is already a partial that was bound
5966 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005967 if (selfdict != NULL
5968 && (rettv->v_type == VAR_FUNC
5969 || (rettv->v_type == VAR_PARTIAL
5970 && (rettv->vval.v_partial->pt_auto
5971 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005972 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005973
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005974 dict_unref(selfdict);
5975 return ret;
5976}
5977
5978/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005979 * Make a copy of an item.
5980 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005981 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5982 * reference to an already copied list/dict can be used.
5983 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005984 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005985 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005986item_copy(
5987 typval_T *from,
5988 typval_T *to,
5989 int deep,
5990 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005991{
5992 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005993 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005994
Bram Moolenaar33570922005-01-25 22:26:29 +00005995 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005996 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005997 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005998 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005999 }
6000 ++recurse;
6001
6002 switch (from->v_type)
6003 {
6004 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006005 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006006 case VAR_STRING:
6007 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006008 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006009 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006010 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006011 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006012 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006013 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006014 copy_tv(from, to);
6015 break;
6016 case VAR_LIST:
6017 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006018 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006019 if (from->vval.v_list == NULL)
6020 to->vval.v_list = NULL;
6021 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6022 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006023 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006024 to->vval.v_list = from->vval.v_list->lv_copylist;
6025 ++to->vval.v_list->lv_refcount;
6026 }
6027 else
6028 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
6029 if (to->vval.v_list == NULL)
6030 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006031 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006032 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006033 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006034 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006035 case VAR_DICT:
6036 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006037 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006038 if (from->vval.v_dict == NULL)
6039 to->vval.v_dict = NULL;
6040 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6041 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006042 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006043 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6044 ++to->vval.v_dict->dv_refcount;
6045 }
6046 else
6047 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
6048 if (to->vval.v_dict == NULL)
6049 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006050 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006051 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006052 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006053 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006054 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006055 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006056 }
6057 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006058 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006059}
6060
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006061 void
6062echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6063{
6064 char_u *tofree;
6065 char_u numbuf[NUMBUFLEN];
6066 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6067
6068 if (*atstart)
6069 {
6070 *atstart = FALSE;
6071 // Call msg_start() after eval1(), evaluating the expression
6072 // may cause a message to appear.
6073 if (with_space)
6074 {
6075 // Mark the saved text as finishing the line, so that what
6076 // follows is displayed on a new line when scrolling back
6077 // at the more prompt.
6078 msg_sb_eol();
6079 msg_start();
6080 }
6081 }
6082 else if (with_space)
6083 msg_puts_attr(" ", echo_attr);
6084
6085 if (p != NULL)
6086 for ( ; *p != NUL && !got_int; ++p)
6087 {
6088 if (*p == '\n' || *p == '\r' || *p == TAB)
6089 {
6090 if (*p != TAB && *needclr)
6091 {
6092 // remove any text still there from the command
6093 msg_clr_eos();
6094 *needclr = FALSE;
6095 }
6096 msg_putchar_attr(*p, echo_attr);
6097 }
6098 else
6099 {
6100 if (has_mbyte)
6101 {
6102 int i = (*mb_ptr2len)(p);
6103
6104 (void)msg_outtrans_len_attr(p, i, echo_attr);
6105 p += i - 1;
6106 }
6107 else
6108 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6109 }
6110 }
6111 vim_free(tofree);
6112}
6113
Bram Moolenaare9a41262005-01-15 22:18:47 +00006114/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 * ":echo expr1 ..." print each argument separated with a space, add a
6116 * newline at the end.
6117 * ":echon expr1 ..." print each argument plain.
6118 */
6119 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006120ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121{
6122 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006123 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006124 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 int needclr = TRUE;
6126 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006127 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006128 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006129 evalarg_T evalarg;
6130
Bram Moolenaare707c882020-07-01 13:07:37 +02006131 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132
6133 if (eap->skip)
6134 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006135 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006137 // If eval1() causes an error message the text from the command may
6138 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006139 need_clr_eos = needclr;
6140
Bram Moolenaar68db9962021-05-09 23:19:22 +02006141 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006142 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 {
6144 /*
6145 * Report the invalid expression unless the expression evaluation
6146 * has been cancelled due to an aborting error, an interrupt, or an
6147 * exception.
6148 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006149 if (!aborting() && did_emsg == did_emsg_before
6150 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006151 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006152 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 break;
6154 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006155 need_clr_eos = FALSE;
6156
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006158 {
6159 if (rettv.v_type == VAR_VOID)
6160 {
6161 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6162 break;
6163 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006164 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006165 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006166
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006167 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 arg = skipwhite(arg);
6169 }
6170 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006171 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172
6173 if (eap->skip)
6174 --emsg_skip;
6175 else
6176 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006177 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 if (needclr)
6179 msg_clr_eos();
6180 if (eap->cmdidx == CMD_echo)
6181 msg_end();
6182 }
6183}
6184
6185/*
6186 * ":echohl {name}".
6187 */
6188 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006189ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006191 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192}
6193
6194/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006195 * Returns the :echo attribute
6196 */
6197 int
6198get_echo_attr(void)
6199{
6200 return echo_attr;
6201}
6202
6203/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 * ":execute expr1 ..." execute the result of an expression.
6205 * ":echomsg expr1 ..." Print a message
6206 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006207 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 * Each gets spaces around each argument and a newline at the end for
6209 * echo commands
6210 */
6211 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006212ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213{
6214 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006215 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 int ret = OK;
6217 char_u *p;
6218 garray_T ga;
6219 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006220 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221
6222 ga_init2(&ga, 1, 80);
6223
6224 if (eap->skip)
6225 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006226 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006228 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006229 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231
6232 if (!eap->skip)
6233 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006234 char_u buf[NUMBUFLEN];
6235
6236 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006237 {
6238 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6239 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006240 semsg(_(e_using_invalid_value_as_string_str),
6241 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006242 p = NULL;
6243 }
6244 else
6245 p = tv_get_string_buf(&rettv, buf);
6246 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006247 else
6248 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006249 if (p == NULL)
6250 {
6251 clear_tv(&rettv);
6252 ret = FAIL;
6253 break;
6254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 len = (int)STRLEN(p);
6256 if (ga_grow(&ga, len + 2) == FAIL)
6257 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006258 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 ret = FAIL;
6260 break;
6261 }
6262 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 ga.ga_len += len;
6266 }
6267
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006268 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 arg = skipwhite(arg);
6270 }
6271
6272 if (ret != FAIL && ga.ga_data != NULL)
6273 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006274 // use the first line of continuation lines for messages
6275 SOURCING_LNUM = start_lnum;
6276
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006277 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6278 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006279 // Mark the already saved text as finishing the line, so that what
6280 // follows is displayed on a new line when scrolling back at the
6281 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006282 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006283 }
6284
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006286 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006287 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006288 out_flush();
6289 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006290 else if (eap->cmdidx == CMD_echoconsole)
6291 {
6292 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6293 ui_write((char_u *)"\r\n", 2, TRUE);
6294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 else if (eap->cmdidx == CMD_echoerr)
6296 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006297 int save_did_emsg = did_emsg;
6298
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006299 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006300 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 if (!force_abort)
6302 did_emsg = save_did_emsg;
6303 }
6304 else if (eap->cmdidx == CMD_execute)
6305 do_cmdline((char_u *)ga.ga_data,
6306 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6307 }
6308
6309 ga_clear(&ga);
6310
6311 if (eap->skip)
6312 --emsg_skip;
6313
6314 eap->nextcmd = check_nextcmd(arg);
6315}
6316
6317/*
6318 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6319 * "arg" points to the "&" or '+' when called, to "option" when returning.
6320 * Returns NULL when no option name found. Otherwise pointer to the char
6321 * after the option name.
6322 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006323 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006324find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325{
6326 char_u *p = *arg;
6327
6328 ++p;
6329 if (*p == 'g' && p[1] == ':')
6330 {
6331 *opt_flags = OPT_GLOBAL;
6332 p += 2;
6333 }
6334 else if (*p == 'l' && p[1] == ':')
6335 {
6336 *opt_flags = OPT_LOCAL;
6337 p += 2;
6338 }
6339 else
6340 *opt_flags = 0;
6341
6342 if (!ASCII_ISALPHA(*p))
6343 return NULL;
6344 *arg = p;
6345
6346 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006347 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 else
6349 while (ASCII_ISALPHA(*p))
6350 ++p;
6351 return p;
6352}
6353
6354/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006355 * Display script name where an item was last set.
6356 * Should only be invoked when 'verbose' is non-zero.
6357 */
6358 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006359last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006360{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006361 char_u *p;
6362
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006363 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006364 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006365 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006366 if (p != NULL)
6367 {
6368 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006369 msg_puts(_("\n\tLast set from "));
6370 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006371 if (script_ctx.sc_lnum > 0)
6372 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006373 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006374 msg_outnum((long)script_ctx.sc_lnum);
6375 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006376 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006377 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006378 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006379 }
6380}
6381
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006382#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383
6384/*
6385 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006386 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 * "flags" can be "g" to do a global substitute.
6388 * Returns an allocated string, NULL for error.
6389 */
6390 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006391do_string_sub(
6392 char_u *str,
6393 char_u *pat,
6394 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006395 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006396 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397{
6398 int sublen;
6399 regmatch_T regmatch;
6400 int i;
6401 int do_all;
6402 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006403 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 garray_T ga;
6405 char_u *ret;
6406 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006407 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006409 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006411 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412
6413 ga_init2(&ga, 1, 200);
6414
6415 do_all = (flags[0] == 'g');
6416
6417 regmatch.rm_ic = p_ic;
6418 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6419 if (regmatch.regprog != NULL)
6420 {
6421 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006422 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6424 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006425 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006426 if (regmatch.startp[0] == regmatch.endp[0])
6427 {
6428 if (zero_width == regmatch.startp[0])
6429 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006430 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006431 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006432 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6433 (size_t)i);
6434 ga.ga_len += i;
6435 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006436 continue;
6437 }
6438 zero_width = regmatch.startp[0];
6439 }
6440
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 /*
6442 * Get some space for a temporary buffer to do the substitution
6443 * into. It will contain:
6444 * - The text up to where the match is.
6445 * - The substituted text.
6446 * - The text after the match.
6447 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006448 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006449 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6451 {
6452 ga_clear(&ga);
6453 break;
6454 }
6455
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006456 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 i = (int)(regmatch.startp[0] - tail);
6458 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006459 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006460 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 + ga.ga_len + i, TRUE, TRUE, FALSE);
6462 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006463 tail = regmatch.endp[0];
6464 if (*tail == NUL)
6465 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 if (!do_all)
6467 break;
6468 }
6469
6470 if (ga.ga_data != NULL)
6471 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6472
Bram Moolenaar473de612013-06-08 18:19:48 +02006473 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 }
6475
6476 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6477 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006478 if (p_cpo == empty_option)
6479 p_cpo = save_cpo;
6480 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006481 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006482 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006483 // If it's still empty it was changed and restored, need to restore in
6484 // the complicated way.
6485 if (*p_cpo == NUL)
6486 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006487 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489
6490 return ret;
6491}