blob: 058b2d3bed8e0f2e3cd70eab0a169bae986be423 [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)
1361 set_var(lp->ll_name, &tv, FALSE);
1362 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001363 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001364 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001365 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001366 {
1367 if (lp->ll_type != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001368 && check_typval_arg_type(lp->ll_type, rettv, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001369 return;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001370 set_var_const(lp->ll_name, lp->ll_type, rettv, copy,
1371 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001372 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001373 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001374 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001375 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001376 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001377 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001378 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001379 else if (lp->ll_range)
1380 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001381 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001382 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001383
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001384 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1385 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001386 {
1387 emsg(_("E996: Cannot lock a range"));
1388 return;
1389 }
1390
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001391 /*
1392 * Check whether any of the list items is locked
1393 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001394 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001395 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001396 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001397 return;
1398 ri = ri->li_next;
1399 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1400 break;
1401 ll_li = ll_li->li_next;
1402 ++ll_n1;
1403 }
1404
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001405 /*
1406 * Assign the List values to the list items.
1407 */
1408 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001409 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001410 if (op != NULL && *op != '=')
1411 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1412 else
1413 {
1414 clear_tv(&lp->ll_li->li_tv);
1415 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1416 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001417 ri = ri->li_next;
1418 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1419 break;
1420 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001421 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001422 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001423 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001424 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001425 ri = NULL;
1426 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001427 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001428 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001429 lp->ll_li = lp->ll_li->li_next;
1430 ++lp->ll_n1;
1431 }
1432 if (ri != NULL)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001433 emsg(_(e_list_value_has_more_items_than_targets));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001434 else if (lp->ll_empty2
1435 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001436 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001437 emsg(_(e_list_value_does_not_have_enough_items));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001438 }
1439 else
1440 {
1441 /*
1442 * Assign to a List or Dictionary item.
1443 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001444 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1445 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001446 {
1447 emsg(_("E996: Cannot lock a list or dict"));
1448 return;
1449 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001450
1451 if (lp->ll_valtype != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001452 && check_typval_arg_type(lp->ll_valtype, rettv, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001453 return;
1454
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001455 if (lp->ll_newkey != NULL)
1456 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001457 if (op != NULL && *op != '=')
1458 {
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02001459 semsg(_(e_dictkey), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001460 return;
1461 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001462 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1463 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001464 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001465
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001466 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001467 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001468 if (di == NULL)
1469 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001470 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1471 {
1472 vim_free(di);
1473 return;
1474 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001475 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001476 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001477 else if (op != NULL && *op != '=')
1478 {
1479 tv_op(lp->ll_tv, rettv, op);
1480 return;
1481 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001482 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001483 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001484
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001485 /*
1486 * Assign the value to the variable or list item.
1487 */
1488 if (copy)
1489 copy_tv(rettv, lp->ll_tv);
1490 else
1491 {
1492 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001493 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001494 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001495 }
1496 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001497}
1498
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001499/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001500 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1501 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001502 * Returns OK or FAIL.
1503 */
1504 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001505tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001506{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001507 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001508 char_u numbuf[NUMBUFLEN];
1509 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001510 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001511
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001512 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001513 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001514 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001515 {
1516 switch (tv1->v_type)
1517 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001518 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001519 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001520 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001521 case VAR_DICT:
1522 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001523 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001524 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001525 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001526 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001527 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001528 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001529 break;
1530
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001531 case VAR_BLOB:
1532 if (*op != '+' || tv2->v_type != VAR_BLOB)
1533 break;
1534 // BLOB += BLOB
1535 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1536 {
1537 blob_T *b1 = tv1->vval.v_blob;
1538 blob_T *b2 = tv2->vval.v_blob;
1539 int i, len = blob_len(b2);
1540 for (i = 0; i < len; i++)
1541 ga_append(&b1->bv_ga, blob_get(b2, i));
1542 }
1543 return OK;
1544
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001545 case VAR_LIST:
1546 if (*op != '+' || tv2->v_type != VAR_LIST)
1547 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001548 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001549 if (tv2->vval.v_list != NULL)
1550 {
1551 if (tv1->vval.v_list == NULL)
1552 {
1553 tv1->vval.v_list = tv2->vval.v_list;
1554 ++tv1->vval.v_list->lv_refcount;
1555 }
1556 else
1557 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1558 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001559 return OK;
1560
1561 case VAR_NUMBER:
1562 case VAR_STRING:
1563 if (tv2->v_type == VAR_LIST)
1564 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001565 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001566 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001567 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001568 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001569#ifdef FEAT_FLOAT
1570 if (tv2->v_type == VAR_FLOAT)
1571 {
1572 float_T f = n;
1573
Bram Moolenaarff697e62019-02-12 22:28:33 +01001574 if (*op == '%')
1575 break;
1576 switch (*op)
1577 {
1578 case '+': f += tv2->vval.v_float; break;
1579 case '-': f -= tv2->vval.v_float; break;
1580 case '*': f *= tv2->vval.v_float; break;
1581 case '/': f /= tv2->vval.v_float; break;
1582 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001583 clear_tv(tv1);
1584 tv1->v_type = VAR_FLOAT;
1585 tv1->vval.v_float = f;
1586 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001587 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001588#endif
1589 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001590 switch (*op)
1591 {
1592 case '+': n += tv_get_number(tv2); break;
1593 case '-': n -= tv_get_number(tv2); break;
1594 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001595 case '/': n = num_divide(n, tv_get_number(tv2),
1596 &failed); break;
1597 case '%': n = num_modulus(n, tv_get_number(tv2),
1598 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001599 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001600 clear_tv(tv1);
1601 tv1->v_type = VAR_NUMBER;
1602 tv1->vval.v_number = n;
1603 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001604 }
1605 else
1606 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001607 if (tv2->v_type == VAR_FLOAT)
1608 break;
1609
Bram Moolenaarff697e62019-02-12 22:28:33 +01001610 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001611 s = tv_get_string(tv1);
1612 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001613 clear_tv(tv1);
1614 tv1->v_type = VAR_STRING;
1615 tv1->vval.v_string = s;
1616 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001617 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001618
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001619 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001620#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001621 {
1622 float_T f;
1623
Bram Moolenaarff697e62019-02-12 22:28:33 +01001624 if (*op == '%' || *op == '.'
1625 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001626 && tv2->v_type != VAR_NUMBER
1627 && tv2->v_type != VAR_STRING))
1628 break;
1629 if (tv2->v_type == VAR_FLOAT)
1630 f = tv2->vval.v_float;
1631 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001632 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001633 switch (*op)
1634 {
1635 case '+': tv1->vval.v_float += f; break;
1636 case '-': tv1->vval.v_float -= f; break;
1637 case '*': tv1->vval.v_float *= f; break;
1638 case '/': tv1->vval.v_float /= f; break;
1639 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001640 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001641#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001642 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001643 }
1644 }
1645
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001646 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001647 return FAIL;
1648}
1649
1650/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001651 * Evaluate the expression used in a ":for var in expr" command.
1652 * "arg" points to "var".
1653 * Set "*errp" to TRUE for an error, FALSE otherwise;
1654 * Return a pointer that holds the info. Null when there is an error.
1655 */
1656 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001657eval_for_line(
1658 char_u *arg,
1659 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001660 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001661 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001662{
Bram Moolenaar33570922005-01-25 22:26:29 +00001663 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001664 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001665 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001666 typval_T tv;
1667 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001668 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001669
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001670 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001671
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001672 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001673 if (fi == NULL)
1674 return NULL;
1675
Bram Moolenaar404557e2021-07-05 21:41:48 +02001676 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1677 &fi->fi_semicolon, FALSE);
1678 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001679 return fi;
1680
Bram Moolenaar404557e2021-07-05 21:41:48 +02001681 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001682 if (expr[0] != 'i' || expr[1] != 'n'
1683 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001684 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001685 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1686 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1687 else
1688 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001689 return fi;
1690 }
1691
1692 if (skip)
1693 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001694 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1695 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001696 {
1697 *errp = FALSE;
1698 if (!skip)
1699 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001700 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001701 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001702 l = tv.vval.v_list;
1703 if (l == NULL)
1704 {
1705 // a null list is like an empty list: do nothing
1706 clear_tv(&tv);
1707 }
1708 else
1709 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001710 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001711 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001712
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001713 // No need to increment the refcount, it's already set for
1714 // the list being used in "tv".
1715 fi->fi_list = l;
1716 list_add_watch(l, &fi->fi_lw);
1717 fi->fi_lw.lw_item = l->lv_first;
1718 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001719 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001720 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001721 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001722 fi->fi_bi = 0;
1723 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001724 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001725 typval_T btv;
1726
1727 // Make a copy, so that the iteration still works when the
1728 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001729 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001730 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001731 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001732 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001733 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001734 else if (tv.v_type == VAR_STRING)
1735 {
1736 fi->fi_byte_idx = 0;
1737 fi->fi_string = tv.vval.v_string;
1738 tv.vval.v_string = NULL;
1739 if (fi->fi_string == NULL)
1740 fi->fi_string = vim_strsave((char_u *)"");
1741 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001742 else
1743 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001744 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001745 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001746 }
1747 }
1748 }
1749 if (skip)
1750 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001751 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001752
1753 return fi;
1754}
1755
1756/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001757 * Used when looping over a :for line, skip the "in expr" part.
1758 */
1759 void
1760skip_for_lines(void *fi_void, evalarg_T *evalarg)
1761{
1762 forinfo_T *fi = (forinfo_T *)fi_void;
1763 int i;
1764
1765 for (i = 0; i < fi->fi_break_count; ++i)
1766 eval_next_line(evalarg);
1767}
1768
1769/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001770 * Use the first item in a ":for" list. Advance to the next.
1771 * Assign the values to the variable (list). "arg" points to the first one.
1772 * Return TRUE when a valid item was found, FALSE when at end of list or
1773 * something wrong.
1774 */
1775 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001776next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001777{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001778 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001779 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001780 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001781 ? (ASSIGN_FINAL
1782 // first round: error if variable exists
1783 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
1784 | ASSIGN_NO_MEMBER_TYPE)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001785 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001786 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001787 int skip_assign = in_vim9script() && arg[0] == '_'
1788 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001789
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001790 if (fi->fi_blob != NULL)
1791 {
1792 typval_T tv;
1793
1794 if (fi->fi_bi >= blob_len(fi->fi_blob))
1795 return FALSE;
1796 tv.v_type = VAR_NUMBER;
1797 tv.v_lock = VAR_FIXED;
1798 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1799 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001800 if (skip_assign)
1801 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001802 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001803 fi->fi_varcount, flag, NULL) == OK;
1804 }
1805
1806 if (fi->fi_string != NULL)
1807 {
1808 typval_T tv;
1809 int len;
1810
1811 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1812 if (len == 0)
1813 return FALSE;
1814 tv.v_type = VAR_STRING;
1815 tv.v_lock = VAR_FIXED;
1816 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1817 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001818 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001819 if (skip_assign)
1820 result = TRUE;
1821 else
1822 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001823 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001824 vim_free(tv.vval.v_string);
1825 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001826 }
1827
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001828 item = fi->fi_lw.lw_item;
1829 if (item == NULL)
1830 result = FALSE;
1831 else
1832 {
1833 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001834 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001835 if (skip_assign)
1836 result = TRUE;
1837 else
1838 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001839 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001840 }
1841 return result;
1842}
1843
1844/*
1845 * Free the structure used to store info used by ":for".
1846 */
1847 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001848free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001849{
Bram Moolenaar33570922005-01-25 22:26:29 +00001850 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001851
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001852 if (fi == NULL)
1853 return;
1854 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001855 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001856 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001857 list_unref(fi->fi_list);
1858 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001859 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001860 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001861 else
1862 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001863 vim_free(fi);
1864}
1865
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001867set_context_for_expression(
1868 expand_T *xp,
1869 char_u *arg,
1870 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001872 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001874 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001876 if (cmdidx == CMD_let || cmdidx == CMD_var
1877 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001878 {
1879 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001880 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001881 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001882 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001883 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001884 {
1885 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001886 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001887 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001888 break;
1889 }
1890 return;
1891 }
1892 }
1893 else
1894 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1895 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 while ((xp->xp_pattern = vim_strpbrk(arg,
1897 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1898 {
1899 c = *xp->xp_pattern;
1900 if (c == '&')
1901 {
1902 c = xp->xp_pattern[1];
1903 if (c == '&')
1904 {
1905 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001906 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 }
1908 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001909 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001911 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1912 xp->xp_pattern += 2;
1913
1914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 }
1916 else if (c == '$')
1917 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001918 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 xp->xp_context = EXPAND_ENV_VARS;
1920 }
1921 else if (c == '=')
1922 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001923 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 xp->xp_context = EXPAND_EXPRESSION;
1925 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001926 else if (c == '#'
1927 && xp->xp_context == EXPAND_EXPRESSION)
1928 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001929 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001930 break;
1931 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001932 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 && xp->xp_context == EXPAND_FUNCTIONS
1934 && vim_strchr(xp->xp_pattern, '(') == NULL)
1935 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001936 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 break;
1938 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001939 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001941 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 {
1943 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1944 if (c == '\\' && xp->xp_pattern[1] != NUL)
1945 ++xp->xp_pattern;
1946 xp->xp_context = EXPAND_NOTHING;
1947 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001948 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001950 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1952 /* skip */ ;
1953 xp->xp_context = EXPAND_NOTHING;
1954 }
1955 else if (c == '|')
1956 {
1957 if (xp->xp_pattern[1] == '|')
1958 {
1959 ++xp->xp_pattern;
1960 xp->xp_context = EXPAND_EXPRESSION;
1961 }
1962 else
1963 xp->xp_context = EXPAND_COMMANDS;
1964 }
1965 else
1966 xp->xp_context = EXPAND_EXPRESSION;
1967 }
1968 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001969 // Doesn't look like something valid, expand as an expression
1970 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001971 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 arg = xp->xp_pattern;
1973 if (*arg != NUL)
1974 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1975 /* skip */ ;
1976 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01001977
1978 // ":exe one two" completes "two"
1979 if ((cmdidx == CMD_execute
1980 || cmdidx == CMD_echo
1981 || cmdidx == CMD_echon
1982 || cmdidx == CMD_echomsg)
1983 && xp->xp_context == EXPAND_EXPRESSION)
1984 {
1985 for (;;)
1986 {
1987 char_u *n = skiptowhite(arg);
1988
1989 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
1990 break;
1991 arg = skipwhite(n);
1992 }
1993 }
1994
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 xp->xp_pattern = arg;
1996}
1997
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001999 * Return TRUE if "pat" matches "text".
2000 * Does not use 'cpo' and always uses 'magic'.
2001 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002002 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002003pattern_match(char_u *pat, char_u *text, int ic)
2004{
2005 int matches = FALSE;
2006 char_u *save_cpo;
2007 regmatch_T regmatch;
2008
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002009 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002010 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002011 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002012 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2013 if (regmatch.regprog != NULL)
2014 {
2015 regmatch.rm_ic = ic;
2016 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2017 vim_regfree(regmatch.regprog);
2018 }
2019 p_cpo = save_cpo;
2020 return matches;
2021}
2022
2023/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002024 * Handle a name followed by "(". Both for just "name(arg)" and for
2025 * "expr->name(arg)".
2026 * Returns OK or FAIL.
2027 */
2028 static int
2029eval_func(
2030 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002031 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002032 char_u *name,
2033 int name_len,
2034 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002035 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002036 typval_T *basetv) // "expr" for "expr->name(arg)"
2037{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002038 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002039 char_u *s = name;
2040 int len = name_len;
2041 partial_T *partial;
2042 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002043 type_T *type = NULL;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002044
2045 if (!evaluate)
2046 check_vars(s, len);
2047
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002048 // If "s" is the name of a variable of type VAR_FUNC
2049 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002050 s = deref_func_name(s, &len, &partial,
2051 in_vim9script() ? &type : NULL, !evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002052
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002053 // Need to make a copy, in case evaluating the arguments makes
2054 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002055 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002056 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002057 ret = FAIL;
2058 else
2059 {
2060 funcexe_T funcexe;
2061
2062 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002063 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002064 funcexe.firstline = curwin->w_cursor.lnum;
2065 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002066 funcexe.evaluate = evaluate;
2067 funcexe.partial = partial;
2068 funcexe.basetv = basetv;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002069 funcexe.check_type = type;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002070 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002071 }
2072 vim_free(s);
2073
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002074 // If evaluate is FALSE rettv->v_type was not set in
2075 // get_func_tv, but it's needed in handle_subscript() to parse
2076 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002077 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2078 {
2079 rettv->vval.v_string = NULL;
2080 rettv->v_type = VAR_FUNC;
2081 }
2082
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002083 // Stop the expression evaluation when immediately
2084 // aborting on error, or when an interrupt occurred or
2085 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002086 if (evaluate && aborting())
2087 {
2088 if (ret == OK)
2089 clear_tv(rettv);
2090 ret = FAIL;
2091 }
2092 return ret;
2093}
2094
2095/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002096 * Get the next line source line without advancing. But do skip over comment
2097 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002098 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002099 */
2100 static char_u *
2101getline_peek_skip_comments(evalarg_T *evalarg)
2102{
2103 for (;;)
2104 {
2105 char_u *next = getline_peek(evalarg->eval_getline,
2106 evalarg->eval_cookie);
2107 char_u *p;
2108
2109 if (next == NULL)
2110 break;
2111 p = skipwhite(next);
2112 if (*p != NUL && !vim9_comment_start(p))
2113 return next;
2114 (void)eval_next_line(evalarg);
2115 }
2116 return NULL;
2117}
2118
2119/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002120 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2121 * comment) and there is a next line, return the next line (skipping blanks)
2122 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002123 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2124 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002125 * "arg" must point somewhere inside a line, not at the start.
2126 */
Yegappan Lakshmanana2438132021-07-10 21:29:18 +02002127 static char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002128eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2129{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002130 char_u *p = skipwhite(arg);
2131
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002132 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002133 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002134 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002135 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002136 && (*p == NUL || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002137 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002138 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002139
2140 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002141 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002142 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002143 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002144
Bram Moolenaar9d489562020-07-30 20:08:50 +02002145 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002146 {
2147 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002148 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002149 }
2150 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002151 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002152}
2153
2154/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002155 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002156 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002157 */
Yegappan Lakshmanana2438132021-07-10 21:29:18 +02002158 static char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002159eval_next_line(evalarg_T *evalarg)
2160{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002161 garray_T *gap = &evalarg->eval_ga;
2162 char_u *line;
2163
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002164 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002165 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2166 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002167 else
2168 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002169 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002170 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2171 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002172 char_u *p = skipwhite(line);
2173
2174 // Going to concatenate the lines after parsing. For an empty or
2175 // comment line use an empty string.
2176 if (*p == NUL || vim9_comment_start(p))
2177 {
2178 vim_free(line);
2179 line = vim_strsave((char_u *)"");
2180 }
2181
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002182 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2183 ++gap->ga_len;
2184 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002185 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002186 {
2187 vim_free(evalarg->eval_tofree);
2188 evalarg->eval_tofree = line;
2189 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002190
2191 // Advanced to the next line, "arg" no longer points into the previous
2192 // line.
2193 VIM_CLEAR(evalarg->eval_tofree_cmdline);
2194
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002195 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002196}
2197
Bram Moolenaare6b53242020-07-01 17:28:33 +02002198/*
2199 * Call eval_next_non_blank() and get the next line if needed.
2200 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002201 char_u *
2202skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2203{
2204 int getnext;
2205 char_u *p = skipwhite(arg);
2206
Bram Moolenaar962d7212020-07-04 14:15:00 +02002207 if (evalarg == NULL)
2208 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002209 eval_next_non_blank(p, evalarg, &getnext);
2210 if (getnext)
2211 return eval_next_line(evalarg);
2212 return p;
2213}
2214
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002215/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002216 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002217 */
2218 void
2219clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2220{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002221 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002222 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002223 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002224 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002225 if (eap != NULL)
2226 {
2227 // We may need to keep the original command line, e.g. for
2228 // ":let" it has the variable names. But we may also need the
2229 // new one, "nextcmd" points into it. Keep both.
2230 vim_free(eap->cmdline_tofree);
2231 eap->cmdline_tofree = *eap->cmdlinep;
2232 *eap->cmdlinep = evalarg->eval_tofree;
2233 }
2234 else
2235 vim_free(evalarg->eval_tofree);
2236 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002237 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002238
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002239 VIM_CLEAR(evalarg->eval_tofree_cmdline);
2240 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002241 }
2242}
2243
2244/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002246 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2248 */
2249
2250/*
2251 * Handle zero level expression.
2252 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002253 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002254 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002255 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 * Return OK or FAIL.
2257 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002258 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002259eval0(
2260 char_u *arg,
2261 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002262 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002263 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264{
2265 int ret;
2266 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002267 int did_emsg_before = did_emsg;
2268 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002269 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002270 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271
2272 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002273 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002274 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002275
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002276 if (ret != FAIL)
2277 end_error = !ends_excmd2(arg, p);
2278 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 {
2280 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002281 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 /*
2283 * Report the invalid expression unless the expression evaluation has
2284 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002285 * exception, or we already gave a more specific error.
2286 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002288 if (!aborting()
2289 && did_emsg == did_emsg_before
2290 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002291 && (flags & EVAL_CONSTANT) == 0
2292 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002293 {
2294 if (end_error)
2295 semsg(_(e_trailing_arg), p);
2296 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002297 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002298 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002299
2300 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002301 // a next command to avoid more errors, unless "|" is following, which
2302 // could only be a command separator.
2303 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2304 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002305 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002307
2308 if (eap != NULL)
2309 eap->nextcmd = check_nextcmd(p);
2310
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 return ret;
2312}
2313
2314/*
2315 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002316 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002317 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 *
2319 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002320 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002322 * Note: "rettv.v_lock" is not set.
2323 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 * Return OK or FAIL.
2325 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002326 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002327eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002329 char_u *p;
2330 int getnext;
2331
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002332 CLEAR_POINTER(rettv);
2333
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 /*
2335 * Get the first variable.
2336 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002337 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 return FAIL;
2339
Bram Moolenaar793648f2020-06-26 21:28:25 +02002340 p = eval_next_non_blank(*arg, evalarg, &getnext);
2341 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002343 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002344 int result;
2345 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002346 evalarg_T *evalarg_used = evalarg;
2347 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002348 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002349 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002350 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002351
2352 if (evalarg == NULL)
2353 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002354 CLEAR_FIELD(local_evalarg);
2355 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002356 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002357 orig_flags = evalarg_used->eval_flags;
2358 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002359
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002360 if (getnext)
2361 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002362 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002363 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002364 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002365 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002366 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002367 clear_tv(rettv);
2368 return FAIL;
2369 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002370 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002371 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002372
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002374 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002376 int error = FALSE;
2377
Bram Moolenaar13106602020-10-04 16:06:05 +02002378 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002379 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002380 else if (vim9script)
2381 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002382 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002384 if (error || !op_falsy || !result)
2385 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002386 if (error)
2387 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 }
2389
2390 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002391 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002393 if (op_falsy)
2394 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002395 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002396 {
mityu4ac198c2021-05-28 17:52:40 +02002397 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002398 clear_tv(rettv);
2399 return FAIL;
2400 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002401 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002402 evalarg_used->eval_flags = (op_falsy ? !result : result)
2403 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2404 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002405 {
2406 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002408 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002409 if (!op_falsy || !result)
2410 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002412 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002414 /*
2415 * Check for the ":".
2416 */
2417 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2418 if (*p != ':')
2419 {
2420 emsg(_(e_missing_colon));
2421 if (evaluate && result)
2422 clear_tv(rettv);
2423 evalarg_used->eval_flags = orig_flags;
2424 return FAIL;
2425 }
2426 if (getnext)
2427 *arg = eval_next_line(evalarg_used);
2428 else
2429 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002430 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002431 {
2432 error_white_both(p, 1);
2433 clear_tv(rettv);
2434 evalarg_used->eval_flags = orig_flags;
2435 return FAIL;
2436 }
2437 *arg = p;
2438 }
2439
2440 /*
2441 * Get the third variable. Recursive!
2442 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002443 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002444 {
mityu4ac198c2021-05-28 17:52:40 +02002445 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002446 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002447 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002448 return FAIL;
2449 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002450 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2451 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002452 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002453 if (eval1(arg, &var2, evalarg_used) == FAIL)
2454 {
2455 if (evaluate && result)
2456 clear_tv(rettv);
2457 evalarg_used->eval_flags = orig_flags;
2458 return FAIL;
2459 }
2460 if (evaluate && !result)
2461 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002463
2464 if (evalarg == NULL)
2465 clear_evalarg(&local_evalarg, NULL);
2466 else
2467 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 }
2469
2470 return OK;
2471}
2472
2473/*
2474 * Handle first level expression:
2475 * expr2 || expr2 || expr2 logical OR
2476 *
2477 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002478 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 *
2480 * Return OK or FAIL.
2481 */
2482 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002483eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002485 char_u *p;
2486 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487
2488 /*
2489 * Get the first variable.
2490 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002491 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 return FAIL;
2493
2494 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002495 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002497 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002498 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002500 evalarg_T *evalarg_used = evalarg;
2501 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002502 int evaluate;
2503 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002504 long result = FALSE;
2505 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002506 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002507 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002508
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002509 if (evalarg == NULL)
2510 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002511 CLEAR_FIELD(local_evalarg);
2512 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002513 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002514 orig_flags = evalarg_used->eval_flags;
2515 evaluate = orig_flags & EVAL_EVALUATE;
2516 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002517 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002518 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002519 result = tv_get_bool_chk(rettv, &error);
2520 else if (tv_get_number_chk(rettv, &error) != 0)
2521 result = TRUE;
2522 clear_tv(rettv);
2523 if (error)
2524 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 }
2526
2527 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002528 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002530 while (p[0] == '|' && p[1] == '|')
2531 {
2532 if (getnext)
2533 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002534 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002535 {
2536 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2537 {
2538 error_white_both(p, 2);
2539 clear_tv(rettv);
2540 return FAIL;
2541 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002542 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002543 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002544
2545 /*
2546 * Get the second variable.
2547 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002548 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2549 {
mityu4ac198c2021-05-28 17:52:40 +02002550 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002551 clear_tv(rettv);
2552 return FAIL;
2553 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002554 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2555 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002556 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002557 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002558 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002559
2560 /*
2561 * Compute the result.
2562 */
2563 if (evaluate && !result)
2564 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002565 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002566 result = tv_get_bool_chk(&var2, &error);
2567 else if (tv_get_number_chk(&var2, &error) != 0)
2568 result = TRUE;
2569 clear_tv(&var2);
2570 if (error)
2571 return FAIL;
2572 }
2573 if (evaluate)
2574 {
2575 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002576 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002577 rettv->v_type = VAR_BOOL;
2578 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002579 }
2580 else
2581 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002582 rettv->v_type = VAR_NUMBER;
2583 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002584 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002585 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002586
2587 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002589
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002590 if (evalarg == NULL)
2591 clear_evalarg(&local_evalarg, NULL);
2592 else
2593 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 }
2595
2596 return OK;
2597}
2598
2599/*
2600 * Handle second level expression:
2601 * expr3 && expr3 && expr3 logical AND
2602 *
2603 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002604 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 *
2606 * Return OK or FAIL.
2607 */
2608 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002609eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002611 char_u *p;
2612 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613
2614 /*
2615 * Get the first variable.
2616 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002617 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 return FAIL;
2619
2620 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002621 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002623 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002624 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002626 evalarg_T *evalarg_used = evalarg;
2627 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002628 int orig_flags;
2629 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002630 long result = TRUE;
2631 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002632 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002633 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002634
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002635 if (evalarg == NULL)
2636 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002637 CLEAR_FIELD(local_evalarg);
2638 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002639 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002640 orig_flags = evalarg_used->eval_flags;
2641 evaluate = orig_flags & EVAL_EVALUATE;
2642 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002643 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002644 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002645 result = tv_get_bool_chk(rettv, &error);
2646 else if (tv_get_number_chk(rettv, &error) == 0)
2647 result = FALSE;
2648 clear_tv(rettv);
2649 if (error)
2650 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 }
2652
2653 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002654 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002656 while (p[0] == '&' && p[1] == '&')
2657 {
2658 if (getnext)
2659 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002660 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002661 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002662 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002663 {
2664 error_white_both(p, 2);
2665 clear_tv(rettv);
2666 return FAIL;
2667 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002668 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002669 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002670
2671 /*
2672 * Get the second variable.
2673 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002674 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2675 {
mityu4ac198c2021-05-28 17:52:40 +02002676 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002677 clear_tv(rettv);
2678 return FAIL;
2679 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002680 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2681 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002682 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002683 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002684 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002685 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002686
2687 /*
2688 * Compute the result.
2689 */
2690 if (evaluate && result)
2691 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002692 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002693 result = tv_get_bool_chk(&var2, &error);
2694 else if (tv_get_number_chk(&var2, &error) == 0)
2695 result = FALSE;
2696 clear_tv(&var2);
2697 if (error)
2698 return FAIL;
2699 }
2700 if (evaluate)
2701 {
2702 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002703 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002704 rettv->v_type = VAR_BOOL;
2705 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002706 }
2707 else
2708 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002709 rettv->v_type = VAR_NUMBER;
2710 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002711 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002712 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002713
2714 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002716
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002717 if (evalarg == NULL)
2718 clear_evalarg(&local_evalarg, NULL);
2719 else
2720 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 }
2722
2723 return OK;
2724}
2725
2726/*
2727 * Handle third level expression:
2728 * var1 == var2
2729 * var1 =~ var2
2730 * var1 != var2
2731 * var1 !~ var2
2732 * var1 > var2
2733 * var1 >= var2
2734 * var1 < var2
2735 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002736 * var1 is var2
2737 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 *
2739 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002740 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 *
2742 * Return OK or FAIL.
2743 */
2744 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002745eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002748 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002749 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002751 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752
2753 /*
2754 * Get the first variable.
2755 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002756 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 return FAIL;
2758
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002759 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002760 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761
2762 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002763 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002765 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002767 typval_T var2;
2768 int ic;
2769 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002770 int evaluate = evalarg == NULL
2771 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002772
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002773 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002774 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002775 *arg = eval_next_line(evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002776 p = *arg;
2777 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002778 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2779 {
mityu4ac198c2021-05-28 17:52:40 +02002780 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002781 clear_tv(rettv);
2782 return FAIL;
2783 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002784
Bram Moolenaar696ba232020-07-29 21:20:41 +02002785 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2786 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002787 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002788 clear_tv(rettv);
2789 return FAIL;
2790 }
2791
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002792 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 if (p[len] == '?')
2794 {
2795 ic = TRUE;
2796 ++len;
2797 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002798 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 else if (p[len] == '#')
2800 {
2801 ic = FALSE;
2802 ++len;
2803 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002804 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002806 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807
2808 /*
2809 * Get the second variable.
2810 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002811 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2812 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002813 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002814 clear_tv(rettv);
2815 return FAIL;
2816 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002817 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002818 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002820 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 return FAIL;
2822 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002823 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002824 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002825 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002826
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002827 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002828 {
2829 ret = FAIL;
2830 clear_tv(rettv);
2831 }
2832 else
2833 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002834 clear_tv(&var2);
2835 return ret;
2836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 }
2838
2839 return OK;
2840}
2841
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002842/*
2843 * Make a copy of blob "tv1" and append blob "tv2".
2844 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002845 void
2846eval_addblob(typval_T *tv1, typval_T *tv2)
2847{
2848 blob_T *b1 = tv1->vval.v_blob;
2849 blob_T *b2 = tv2->vval.v_blob;
2850 blob_T *b = blob_alloc();
2851 int i;
2852
2853 if (b != NULL)
2854 {
2855 for (i = 0; i < blob_len(b1); i++)
2856 ga_append(&b->bv_ga, blob_get(b1, i));
2857 for (i = 0; i < blob_len(b2); i++)
2858 ga_append(&b->bv_ga, blob_get(b2, i));
2859
2860 clear_tv(tv1);
2861 rettv_blob_set(tv1, b);
2862 }
2863}
2864
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002865/*
2866 * Make a copy of list "tv1" and append list "tv2".
2867 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002868 int
2869eval_addlist(typval_T *tv1, typval_T *tv2)
2870{
2871 typval_T var3;
2872
2873 // concatenate Lists
2874 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2875 {
2876 clear_tv(tv1);
2877 clear_tv(tv2);
2878 return FAIL;
2879 }
2880 clear_tv(tv1);
2881 *tv1 = var3;
2882 return OK;
2883}
2884
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885/*
2886 * Handle fourth level expression:
2887 * + number addition
2888 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002889 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002890 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 *
2892 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002893 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 *
2895 * Return OK or FAIL.
2896 */
2897 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002898eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 /*
2901 * Get the first variable.
2902 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002903 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 return FAIL;
2905
2906 /*
2907 * Repeat computing, until no '+', '-' or '.' is following.
2908 */
2909 for (;;)
2910 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002911 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002912 int getnext;
2913 char_u *p;
2914 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002915 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002916 int concat;
2917 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002918 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002919
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002920 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002921 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002922 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002923 p = eval_next_non_blank(*arg, evalarg, &getnext);
2924 op = *p;
2925 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002926 if ((op != '+' && op != '-' && !concat) || p[1] == '='
2927 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002929 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
2930 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002931
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002932 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002933 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002934 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002935 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002936 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002937 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002938 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002939 {
mityu4ac198c2021-05-28 17:52:40 +02002940 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002941 clear_tv(rettv);
2942 return FAIL;
2943 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002944 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002945 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002946 if ((op != '+' || (rettv->v_type != VAR_LIST
2947 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002948#ifdef FEAT_FLOAT
2949 && (op == '.' || rettv->v_type != VAR_FLOAT)
2950#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002951 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002952 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002953 int error = FALSE;
2954
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002955 // For "list + ...", an illegal use of the first operand as
2956 // a number cannot be determined before evaluating the 2nd
2957 // operand: if this is also a list, all is ok.
2958 // For "something . ...", "something - ..." or "non-list + ...",
2959 // we know that the first operand needs to be a string or number
2960 // without evaluating the 2nd operand. So check before to avoid
2961 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002962 if (op != '.')
2963 tv_get_number_chk(rettv, &error);
2964 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002965 {
2966 clear_tv(rettv);
2967 return FAIL;
2968 }
2969 }
2970
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 /*
2972 * Get the second variable.
2973 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002974 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002975 {
mityu89dcb4d2021-05-28 13:50:17 +02002976 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002977 clear_tv(rettv);
2978 return FAIL;
2979 }
2980 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002981 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002983 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 return FAIL;
2985 }
2986
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002987 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 {
2989 /*
2990 * Compute the result.
2991 */
2992 if (op == '.')
2993 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002994 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2995 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002996 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002997
Bram Moolenaar2e086612020-08-25 22:37:48 +02002998 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002999 || var2.v_type == VAR_CHANNEL
3000 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003001 semsg(_(e_using_invalid_value_as_string_str),
3002 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003003#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02003004 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003005 {
3006 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3007 var2.vval.v_float);
3008 s2 = buf2;
3009 }
3010#endif
3011 else
3012 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003013 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003014 {
3015 clear_tv(rettv);
3016 clear_tv(&var2);
3017 return FAIL;
3018 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003019 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003020 clear_tv(rettv);
3021 rettv->v_type = VAR_STRING;
3022 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003024 else if (op == '+' && rettv->v_type == VAR_BLOB
3025 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003026 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003027 else if (op == '+' && rettv->v_type == VAR_LIST
3028 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003029 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003030 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003031 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 else
3034 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003035 int error = FALSE;
3036 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003037#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003038 float_T f1 = 0, f2 = 0;
3039
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003040 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003041 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003042 f1 = rettv->vval.v_float;
3043 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003044 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003045 else
3046#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003047 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003048 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003049 if (error)
3050 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003051 // This can only happen for "list + non-list" or
3052 // "blob + non-blob". For "non-list + ..." or
3053 // "something - ...", we returned before evaluating the
3054 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003055 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003056 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003057 return FAIL;
3058 }
3059#ifdef FEAT_FLOAT
3060 if (var2.v_type == VAR_FLOAT)
3061 f1 = n1;
3062#endif
3063 }
3064#ifdef FEAT_FLOAT
3065 if (var2.v_type == VAR_FLOAT)
3066 {
3067 f2 = var2.vval.v_float;
3068 n2 = 0;
3069 }
3070 else
3071#endif
3072 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003073 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003074 if (error)
3075 {
3076 clear_tv(rettv);
3077 clear_tv(&var2);
3078 return FAIL;
3079 }
3080#ifdef FEAT_FLOAT
3081 if (rettv->v_type == VAR_FLOAT)
3082 f2 = n2;
3083#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003084 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003085 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003086
3087#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003088 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003089 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3090 {
3091 if (op == '+')
3092 f1 = f1 + f2;
3093 else
3094 f1 = f1 - f2;
3095 rettv->v_type = VAR_FLOAT;
3096 rettv->vval.v_float = f1;
3097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003099#endif
3100 {
3101 if (op == '+')
3102 n1 = n1 + n2;
3103 else
3104 n1 = n1 - n2;
3105 rettv->v_type = VAR_NUMBER;
3106 rettv->vval.v_number = n1;
3107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003109 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 }
3111 }
3112 return OK;
3113}
3114
3115/*
3116 * Handle fifth level expression:
3117 * * number multiplication
3118 * / number division
3119 * % number modulo
3120 *
3121 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003122 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 *
3124 * Return OK or FAIL.
3125 */
3126 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003127eval6(
3128 char_u **arg,
3129 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003130 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003131 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003133#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003134 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003135#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136
3137 /*
3138 * Get the first variable.
3139 */
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003140 if (eval7t(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 return FAIL;
3142
3143 /*
3144 * Repeat computing, until no '*', '/' or '%' is following.
3145 */
3146 for (;;)
3147 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003148 int evaluate;
3149 int getnext;
3150 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003151 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003152 int op;
3153 varnumber_T n1, n2;
3154#ifdef FEAT_FLOAT
3155 float_T f1, f2;
3156#endif
3157 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003158
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003159 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003160 p = eval_next_non_blank(*arg, evalarg, &getnext);
3161 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003162 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003164
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003165 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003166 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003167 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003168 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003169 {
3170 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3171 {
mityu4ac198c2021-05-28 17:52:40 +02003172 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003173 clear_tv(rettv);
3174 return FAIL;
3175 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003176 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003179#ifdef FEAT_FLOAT
3180 f1 = 0;
3181 f2 = 0;
3182#endif
3183 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003184 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003186#ifdef FEAT_FLOAT
3187 if (rettv->v_type == VAR_FLOAT)
3188 {
3189 f1 = rettv->vval.v_float;
3190 use_float = TRUE;
3191 n1 = 0;
3192 }
3193 else
3194#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003195 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003196 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003197 if (error)
3198 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 }
3200 else
3201 n1 = 0;
3202
3203 /*
3204 * Get the second variable.
3205 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003206 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3207 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003208 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003209 clear_tv(rettv);
3210 return FAIL;
3211 }
3212 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003213 if (eval7t(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 return FAIL;
3215
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003216 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003218#ifdef FEAT_FLOAT
3219 if (var2.v_type == VAR_FLOAT)
3220 {
3221 if (!use_float)
3222 {
3223 f1 = n1;
3224 use_float = TRUE;
3225 }
3226 f2 = var2.vval.v_float;
3227 n2 = 0;
3228 }
3229 else
3230#endif
3231 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003232 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003233 clear_tv(&var2);
3234 if (error)
3235 return FAIL;
3236#ifdef FEAT_FLOAT
3237 if (use_float)
3238 f2 = n2;
3239#endif
3240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241
3242 /*
3243 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003244 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003246#ifdef FEAT_FLOAT
3247 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003249 if (op == '*')
3250 f1 = f1 * f2;
3251 else if (op == '/')
3252 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003253# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003254 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003255 if (f2 == 0.0)
3256 {
3257 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003258 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003259 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003260 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003261 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003262 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003263 }
3264 else
3265 f1 = f1 / f2;
3266# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003267 // We rely on the floating point library to handle divide
3268 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003269 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003270# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003273 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003274 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003275 return FAIL;
3276 }
3277 rettv->v_type = VAR_FLOAT;
3278 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 }
3280 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003281#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003283 int failed = FALSE;
3284
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003285 if (op == '*')
3286 n1 = n1 * n2;
3287 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003288 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003290 n1 = num_modulus(n1, n2, &failed);
3291 if (failed)
3292 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003293
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003294 rettv->v_type = VAR_NUMBER;
3295 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 }
3298 }
3299
3300 return OK;
3301}
3302
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003303/*
3304 * Handle a type cast before a base level expression.
3305 * "arg" must point to the first non-white of the expression.
3306 * "arg" is advanced to just after the recognized expression.
3307 * Return OK or FAIL.
3308 */
3309 static int
3310eval7t(
3311 char_u **arg,
3312 typval_T *rettv,
3313 evalarg_T *evalarg,
3314 int want_string) // after "." operator
3315{
3316 type_T *want_type = NULL;
3317 garray_T type_list; // list of pointers to allocated types
3318 int res;
3319 int evaluate = evalarg == NULL ? 0
3320 : (evalarg->eval_flags & EVAL_EVALUATE);
3321
3322 // Recognize <type> in Vim9 script only.
3323 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1]))
3324 {
3325 ++*arg;
3326 ga_init2(&type_list, sizeof(type_T *), 10);
3327 want_type = parse_type(arg, &type_list, TRUE);
3328 if (want_type == NULL && (evaluate || **arg != '>'))
3329 {
3330 clear_type_list(&type_list);
3331 return FAIL;
3332 }
3333
3334 if (**arg != '>')
3335 {
3336 if (*skipwhite(*arg) == '>')
3337 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3338 else
3339 emsg(_(e_missing_gt));
3340 clear_type_list(&type_list);
3341 return FAIL;
3342 }
3343 ++*arg;
3344 *arg = skipwhite_and_linebreak(*arg, evalarg);
3345 }
3346
3347 res = eval7(arg, rettv, evalarg, want_string);
3348
3349 if (want_type != NULL && evaluate)
3350 {
3351 if (res == OK)
3352 {
3353 type_T *actual = typval2type(rettv, get_copyID(), &type_list, TRUE);
3354
3355 if (!equal_type(want_type, actual))
3356 {
3357 if (want_type == &t_bool && actual != &t_bool
3358 && (actual->tt_flags & TTFLAG_BOOL_OK))
3359 {
3360 int n = tv2bool(rettv);
3361
3362 // can use "0" and "1" for boolean in some places
3363 clear_tv(rettv);
3364 rettv->v_type = VAR_BOOL;
3365 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3366 }
3367 else
3368 {
3369 where_T where;
3370
3371 where.wt_index = 0;
3372 where.wt_variable = TRUE;
3373 res = check_type(want_type, actual, TRUE, where);
3374 }
3375 }
3376 }
3377 clear_type_list(&type_list);
3378 }
3379
3380 return res;
3381}
3382
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003383 int
3384eval_leader(char_u **arg, int vim9)
3385{
3386 char_u *s = *arg;
3387 char_u *p = *arg;
3388
3389 while (*p == '!' || *p == '-' || *p == '+')
3390 {
3391 char_u *n = skipwhite(p + 1);
3392
3393 // ++, --, -+ and +- are not accepted in Vim9 script
3394 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3395 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003396 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003397 return FAIL;
3398 }
3399 p = n;
3400 }
3401 *arg = p;
3402 return OK;
3403}
3404
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405/*
3406 * Handle sixth level expression:
3407 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003408 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003409 * "string" string constant
3410 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 * &option-name option value
3412 * @r register contents
3413 * identifier variable value
3414 * function() function call
3415 * $VAR environment variable
3416 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003417 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003418 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003419 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003420 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 *
3422 * Also handle:
3423 * ! in front logical NOT
3424 * - in front unary minus
3425 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003426 * trailing [] subscript in String or List
3427 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003428 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 *
3430 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003431 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 *
3433 * Return OK or FAIL.
3434 */
3435 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003436eval7(
3437 char_u **arg,
3438 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003439 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003440 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003442 int evaluate = evalarg != NULL
3443 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 int len;
3445 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 char_u *start_leader, *end_leader;
3447 int ret = OK;
3448 char_u *alias;
3449
3450 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003451 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003452 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003454 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455
3456 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003457 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 */
3459 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003460 if (eval_leader(arg, in_vim9script()) == FAIL)
3461 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 end_leader = *arg;
3463
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003464 if (**arg == '.' && (!isdigit(*(*arg + 1))
3465#ifdef FEAT_FLOAT
3466 || current_sctx.sc_version < 2
3467#endif
3468 ))
3469 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003470 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003471 ++*arg;
3472 return FAIL;
3473 }
3474
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 switch (**arg)
3476 {
3477 /*
3478 * Number constant.
3479 */
3480 case '0':
3481 case '1':
3482 case '2':
3483 case '3':
3484 case '4':
3485 case '5':
3486 case '6':
3487 case '7':
3488 case '8':
3489 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003490 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003491
3492 // Apply prefixed "-" and "+" now. Matters especially when
3493 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003494 if (ret == OK && evaluate && end_leader > start_leader
3495 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003496 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 break;
3498
3499 /*
3500 * String constant: "string".
3501 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003502 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 break;
3504
3505 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003506 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003508 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003509 break;
3510
3511 /*
3512 * List: [expr, expr]
3513 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003514 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 break;
3516
3517 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003518 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003519 */
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003520 case '#': if (in_vim9script())
3521 {
3522 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3523 }
3524 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003525 {
3526 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003527 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003528 }
3529 else
3530 ret = NOTDONE;
3531 break;
3532
3533 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003534 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003535 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003536 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003537 case '{': if (in_vim9script())
3538 ret = NOTDONE;
3539 else
3540 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003541 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003542 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003543 break;
3544
3545 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003546 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003548 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 break;
3550
3551 /*
3552 * Environment variable: $VAR.
3553 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003554 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 break;
3556
3557 /*
3558 * Register contents: @r.
3559 */
3560 case '@': ++*arg;
3561 if (evaluate)
3562 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003563 if (in_vim9script() && IS_WHITE_OR_NUL(**arg))
3564 semsg(_(e_syntax_error_at_str), *arg);
3565 else if (in_vim9script() && !valid_yank_reg(**arg, FALSE))
3566 emsg_invreg(**arg);
3567 else
3568 {
3569 rettv->v_type = VAR_STRING;
3570 rettv->vval.v_string = get_reg_contents(**arg,
3571 GREG_EXPR_SRC);
3572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 }
3574 if (**arg != NUL)
3575 ++*arg;
3576 break;
3577
3578 /*
3579 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003580 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003582 case '(': ret = NOTDONE;
3583 if (in_vim9script())
Bram Moolenaar06409502021-02-17 17:00:27 +01003584 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003585 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003586 if (ret == OK && evaluate)
3587 {
3588 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3589
Bram Moolenaara9931532021-06-12 15:58:16 +02003590 // Compile it here to get the return type. The return
3591 // type is optional, when it's missing use t_unknown.
3592 // This is recognized in compile_return().
3593 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3594 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003595 if (compile_def_function(ufunc,
Bram Moolenaare99d4222021-06-13 14:01:26 +02003596 FALSE, COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003597 {
3598 clear_tv(rettv);
3599 ret = FAIL;
3600 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003601 }
3602 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003603 if (ret == NOTDONE)
3604 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003605 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003606 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003607
Bram Moolenaar9215f012020-06-27 21:18:00 +02003608 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003609 if (**arg == ')')
3610 ++*arg;
3611 else if (ret == OK)
3612 {
3613 emsg(_(e_missing_close));
3614 clear_tv(rettv);
3615 ret = FAIL;
3616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 }
3618 break;
3619
Bram Moolenaar8c711452005-01-14 21:53:12 +00003620 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 break;
3622 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003623
3624 if (ret == NOTDONE)
3625 {
3626 /*
3627 * Must be a variable or function name.
3628 * Can also be a curly-braces kind of name: {expr}.
3629 */
3630 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003631 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003632 if (alias != NULL)
3633 s = alias;
3634
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003635 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003636 ret = FAIL;
3637 else
3638 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003639 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3640
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003641 if (evaluate && in_vim9script() && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003642 {
3643 emsg(_(e_cannot_use_underscore_here));
3644 ret = FAIL;
3645 }
3646 else if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003647 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003648 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003649 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003650 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003651 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003652 else if (flags & EVAL_CONSTANT)
3653 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003654 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003655 {
3656 // get the value of "true", "false" or a variable
3657 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3658 {
3659 rettv->v_type = VAR_BOOL;
3660 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003661 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003662 }
3663 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003664 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003665 {
3666 rettv->v_type = VAR_BOOL;
3667 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003668 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003669 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003670 else if (len == 4 && in_vim9script()
3671 && STRNCMP(s, "null", 4) == 0)
3672 {
3673 rettv->v_type = VAR_SPECIAL;
3674 rettv->vval.v_number = VVAL_NULL;
3675 ret = OK;
3676 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003677 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003678 ret = eval_variable(s, len, rettv, NULL,
3679 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003680 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003681 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003682 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003683 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003684 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003685 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003686 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003687 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003688 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003689 }
3690
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003691 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3692 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003693 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003694 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695
3696 /*
3697 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3698 */
3699 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003700 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003701 return ret;
3702}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003703
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003704/*
3705 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003706 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003707 * Adjusts "end_leaderp" until it is at "start_leader".
3708 */
3709 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003710eval7_leader(
3711 typval_T *rettv,
3712 int numeric_only,
3713 char_u *start_leader,
3714 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003715{
3716 char_u *end_leader = *end_leaderp;
3717 int ret = OK;
3718 int error = FALSE;
3719 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003720 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003721#ifdef FEAT_FLOAT
3722 float_T f = 0.0;
3723
3724 if (rettv->v_type == VAR_FLOAT)
3725 f = rettv->vval.v_float;
3726 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003727#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003728 {
3729 while (VIM_ISWHITE(end_leader[-1]))
3730 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003731 if (in_vim9script() && end_leader[-1] == '!')
3732 val = tv2bool(rettv);
3733 else
3734 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003735 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003736 if (error)
3737 {
3738 clear_tv(rettv);
3739 ret = FAIL;
3740 }
3741 else
3742 {
3743 while (end_leader > start_leader)
3744 {
3745 --end_leader;
3746 if (*end_leader == '!')
3747 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003748 if (numeric_only)
3749 {
3750 ++end_leader;
3751 break;
3752 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003753#ifdef FEAT_FLOAT
3754 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003755 {
3756 if (in_vim9script())
3757 {
3758 rettv->v_type = VAR_BOOL;
3759 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3760 }
3761 else
3762 f = !f;
3763 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003764 else
3765#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003766 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003767 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003768 type = VAR_BOOL;
3769 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003770 }
3771 else if (*end_leader == '-')
3772 {
3773#ifdef FEAT_FLOAT
3774 if (rettv->v_type == VAR_FLOAT)
3775 f = -f;
3776 else
3777#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003778 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003779 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003780 type = VAR_NUMBER;
3781 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003782 }
3783 }
3784#ifdef FEAT_FLOAT
3785 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003787 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003788 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003790 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003791#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003792 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003793 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003794 if (in_vim9script())
3795 rettv->v_type = type;
3796 else
3797 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003798 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003801 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 return ret;
3803}
3804
3805/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003806 * Call the function referred to in "rettv".
3807 */
3808 static int
3809call_func_rettv(
3810 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003811 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003812 typval_T *rettv,
3813 int evaluate,
3814 dict_T *selfdict,
3815 typval_T *basetv)
3816{
3817 partial_T *pt = NULL;
3818 funcexe_T funcexe;
3819 typval_T functv;
3820 char_u *s;
3821 int ret;
3822
3823 // need to copy the funcref so that we can clear rettv
3824 if (evaluate)
3825 {
3826 functv = *rettv;
3827 rettv->v_type = VAR_UNKNOWN;
3828
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003829 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003830 if (functv.v_type == VAR_PARTIAL)
3831 {
3832 pt = functv.vval.v_partial;
3833 s = partial_name(pt);
3834 }
3835 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003836 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003837 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003838 if (s == NULL || *s == NUL)
3839 {
3840 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02003841 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003842 goto theend;
3843 }
3844 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003845 }
3846 else
3847 s = (char_u *)"";
3848
Bram Moolenaara80faa82020-04-12 19:37:17 +02003849 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003850 funcexe.firstline = curwin->w_cursor.lnum;
3851 funcexe.lastline = curwin->w_cursor.lnum;
3852 funcexe.evaluate = evaluate;
3853 funcexe.partial = pt;
3854 funcexe.selfdict = selfdict;
3855 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003856 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003857
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003858theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003859 // Clear the funcref afterwards, so that deleting it while
3860 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003861 if (evaluate)
3862 clear_tv(&functv);
3863
3864 return ret;
3865}
3866
3867/*
3868 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003869 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003870 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3871 */
3872 static int
3873eval_lambda(
3874 char_u **arg,
3875 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003876 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003877 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003878{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003879 int evaluate = evalarg != NULL
3880 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003881 typval_T base = *rettv;
3882 int ret;
3883
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003884 rettv->v_type = VAR_UNKNOWN;
3885
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003886 if (**arg == '{')
3887 {
3888 // ->{lambda}()
3889 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3890 }
3891 else
3892 {
3893 // ->(lambda)()
3894 ++*arg;
3895 ret = eval1(arg, rettv, evalarg);
3896 *arg = skipwhite_and_linebreak(*arg, evalarg);
3897 if (**arg != ')')
3898 {
3899 emsg(_(e_missing_close));
3900 ret = FAIL;
3901 }
3902 ++*arg;
3903 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003904 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003905 return FAIL;
3906 else if (**arg != '(')
3907 {
3908 if (verbose)
3909 {
3910 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003911 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003912 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003913 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003914 }
3915 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003916 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003917 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003918 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003919 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003920
3921 // Clear the funcref afterwards, so that deleting it while
3922 // evaluating the arguments is possible (see test55).
3923 if (evaluate)
3924 clear_tv(&base);
3925
3926 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003927}
3928
3929/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003930 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003931 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02003932 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3933 */
3934 static int
3935eval_method(
3936 char_u **arg,
3937 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003938 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003939 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003940{
3941 char_u *name;
3942 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003943 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003944 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003945 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003946 int evaluate = evalarg != NULL
3947 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003948
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003949 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003950
Bram Moolenaarac92e252019-08-03 21:58:38 +02003951 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003952 len = get_name_len(arg, &alias, evaluate, TRUE);
3953 if (alias != NULL)
3954 name = alias;
3955
3956 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003957 {
3958 if (verbose)
3959 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003960 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003961 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003962 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003963 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003964 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003965 if (**arg != '(')
3966 {
3967 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003968 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003969 ret = FAIL;
3970 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003971 else if (VIM_ISWHITE((*arg)[-1]))
3972 {
3973 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003974 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003975 ret = FAIL;
3976 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003977 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003978 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003979 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003980 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003981
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003982 // Clear the funcref afterwards, so that deleting it while
3983 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003984 if (evaluate)
3985 clear_tv(&base);
3986
Bram Moolenaarac92e252019-08-03 21:58:38 +02003987 return ret;
3988}
3989
3990/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003991 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3992 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003993 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3994 */
3995 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003996eval_index(
3997 char_u **arg,
3998 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003999 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004000 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004001{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004002 int evaluate = evalarg != NULL
4003 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004004 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004005 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004006 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004007 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004008 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004009 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004010
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004011 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4012 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004013
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004014 init_tv(&var1);
4015 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004016 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004017 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004018 /*
4019 * dict.name
4020 */
4021 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004022 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004023 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004024 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004025 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004026 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004027 }
4028 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004029 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004030 /*
4031 * something[idx]
4032 *
4033 * Get the (first) variable from inside the [].
4034 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004035 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004036 if (**arg == ':')
4037 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004038 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004039 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004040 else if (vim9 && **arg == ':')
4041 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004042 semsg(_(e_white_space_required_before_and_after_str_at_str),
4043 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004044 clear_tv(&var1);
4045 return FAIL;
4046 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004047 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004048 {
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004049#ifdef FEAT_FLOAT
4050 // allow for indexing with float
4051 if (vim9 && rettv->v_type == VAR_DICT
4052 && var1.v_type == VAR_FLOAT)
4053 {
4054 var1.vval.v_string = typval_tostring(&var1, TRUE);
4055 var1.v_type = VAR_STRING;
4056 }
4057#endif
4058 if (tv_get_string_chk(&var1) == NULL)
4059 {
4060 // not a number or string
4061 clear_tv(&var1);
4062 return FAIL;
4063 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004064 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004065
4066 /*
4067 * Get the second variable from inside the [:].
4068 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004069 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004070 if (**arg == ':')
4071 {
4072 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004073 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01004074 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004075 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004076 semsg(_(e_white_space_required_before_and_after_str_at_str),
4077 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004078 if (!empty1)
4079 clear_tv(&var1);
4080 return FAIL;
4081 }
4082 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004083 if (**arg == ']')
4084 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004085 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004086 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004087 if (!empty1)
4088 clear_tv(&var1);
4089 return FAIL;
4090 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004091 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004092 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004093 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004094 if (!empty1)
4095 clear_tv(&var1);
4096 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004097 return FAIL;
4098 }
4099 }
4100
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004101 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004102 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004103 if (**arg != ']')
4104 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004105 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004106 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004107 clear_tv(&var1);
4108 if (range)
4109 clear_tv(&var2);
4110 return FAIL;
4111 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004112 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004113 }
4114
4115 if (evaluate)
4116 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004117 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004118 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004119 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004120
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004121 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004122 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004123 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004124 clear_tv(&var2);
4125 return res;
4126 }
4127 return OK;
4128}
4129
4130/*
4131 * Check if "rettv" can have an [index] or [sli:ce]
4132 */
4133 int
4134check_can_index(typval_T *rettv, int evaluate, int verbose)
4135{
4136 switch (rettv->v_type)
4137 {
4138 case VAR_FUNC:
4139 case VAR_PARTIAL:
4140 if (verbose)
4141 emsg(_("E695: Cannot index a Funcref"));
4142 return FAIL;
4143 case VAR_FLOAT:
4144#ifdef FEAT_FLOAT
4145 if (verbose)
4146 emsg(_(e_float_as_string));
4147 return FAIL;
4148#endif
4149 case VAR_BOOL:
4150 case VAR_SPECIAL:
4151 case VAR_JOB:
4152 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004153 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004154 if (verbose)
4155 emsg(_(e_cannot_index_special_variable));
4156 return FAIL;
4157 case VAR_UNKNOWN:
4158 case VAR_ANY:
4159 case VAR_VOID:
4160 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004161 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004162 emsg(_(e_cannot_index_special_variable));
4163 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004164 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004165 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004166
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004167 case VAR_STRING:
4168 case VAR_LIST:
4169 case VAR_DICT:
4170 case VAR_BLOB:
4171 break;
4172 case VAR_NUMBER:
4173 if (in_vim9script())
4174 emsg(_(e_cannot_index_number));
4175 break;
4176 }
4177 return OK;
4178}
4179
4180/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004181 * slice() function
4182 */
4183 void
4184f_slice(typval_T *argvars, typval_T *rettv)
4185{
4186 if (check_can_index(argvars, TRUE, FALSE) == OK)
4187 {
4188 copy_tv(argvars, rettv);
4189 eval_index_inner(rettv, TRUE, argvars + 1,
4190 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4191 TRUE, NULL, 0, FALSE);
4192 }
4193}
4194
4195/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004196 * Apply index or range to "rettv".
4197 * "var1" is the first index, NULL for [:expr].
4198 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004199 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4200 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004201 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4202 */
4203 int
4204eval_index_inner(
4205 typval_T *rettv,
4206 int is_range,
4207 typval_T *var1,
4208 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004209 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004210 char_u *key,
4211 int keylen,
4212 int verbose)
4213{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004214 varnumber_T n1, n2 = 0;
4215 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004216
4217 n1 = 0;
4218 if (var1 != NULL && rettv->v_type != VAR_DICT)
4219 n1 = tv_get_number(var1);
4220
4221 if (is_range)
4222 {
4223 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004224 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004225 if (verbose)
4226 emsg(_(e_cannot_slice_dictionary));
4227 return FAIL;
4228 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004229 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004230 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004231 else
4232 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004233 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004234
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004235 switch (rettv->v_type)
4236 {
4237 case VAR_UNKNOWN:
4238 case VAR_ANY:
4239 case VAR_VOID:
4240 case VAR_FUNC:
4241 case VAR_PARTIAL:
4242 case VAR_FLOAT:
4243 case VAR_BOOL:
4244 case VAR_SPECIAL:
4245 case VAR_JOB:
4246 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004247 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004248 break; // not evaluating, skipping over subscript
4249
4250 case VAR_NUMBER:
4251 case VAR_STRING:
4252 {
4253 char_u *s = tv_get_string(rettv);
4254
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004255 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004256 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004257 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004258 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004259 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004260 else
4261 s = char_from_string(s, n1);
4262 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004263 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004264 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004265 // The resulting variable is a substring. If the indexes
4266 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004267 if (n1 < 0)
4268 {
4269 n1 = len + n1;
4270 if (n1 < 0)
4271 n1 = 0;
4272 }
4273 if (n2 < 0)
4274 n2 = len + n2;
4275 else if (n2 >= len)
4276 n2 = len;
4277 if (n1 >= len || n2 < 0 || n1 > n2)
4278 s = NULL;
4279 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004280 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004281 }
4282 else
4283 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004284 // The resulting variable is a string of a single
4285 // character. If the index is too big or negative the
4286 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004287 if (n1 >= len || n1 < 0)
4288 s = NULL;
4289 else
4290 s = vim_strnsave(s + n1, 1);
4291 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004292 clear_tv(rettv);
4293 rettv->v_type = VAR_STRING;
4294 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004295 }
4296 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004297
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004298 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004299 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4300 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004301 break;
4302
4303 case VAR_LIST:
4304 if (var1 == NULL)
4305 n1 = 0;
4306 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004307 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004308 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004309 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004310 return FAIL;
4311 break;
4312
4313 case VAR_DICT:
4314 {
4315 dictitem_T *item;
4316 typval_T tmp;
4317
4318 if (key == NULL)
4319 {
4320 key = tv_get_string_chk(var1);
4321 if (key == NULL)
4322 return FAIL;
4323 }
4324
4325 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
4326
4327 if (item == NULL && verbose)
4328 semsg(_(e_dictkey), key);
4329 if (item == NULL)
4330 return FAIL;
4331
4332 copy_tv(&item->di_tv, &tmp);
4333 clear_tv(rettv);
4334 *rettv = tmp;
4335 }
4336 break;
4337 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004338 return OK;
4339}
4340
4341/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004342 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004343 */
4344 char_u *
4345partial_name(partial_T *pt)
4346{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004347 if (pt != NULL)
4348 {
4349 if (pt->pt_name != NULL)
4350 return pt->pt_name;
4351 if (pt->pt_func != NULL)
4352 return pt->pt_func->uf_name;
4353 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004354 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004355}
4356
Bram Moolenaarddecc252016-04-06 22:59:37 +02004357 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004358partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004359{
4360 int i;
4361
4362 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004363 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004364 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004365 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004366 if (pt->pt_name != NULL)
4367 {
4368 func_unref(pt->pt_name);
4369 vim_free(pt->pt_name);
4370 }
4371 else
4372 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004373
Bram Moolenaar54656012021-06-09 20:50:46 +02004374 // "out_up" is no longer used, decrement refcount on partial that owns it.
4375 partial_unref(pt->pt_outer.out_up_partial);
4376
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004377 // Decrease the reference count for the context of a closure. If down
4378 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004379 if (pt->pt_funcstack != NULL)
4380 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004381 --pt->pt_funcstack->fs_refcount;
4382 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004383 }
4384
Bram Moolenaarddecc252016-04-06 22:59:37 +02004385 vim_free(pt);
4386}
4387
4388/*
4389 * Unreference a closure: decrement the reference count and free it when it
4390 * becomes zero.
4391 */
4392 void
4393partial_unref(partial_T *pt)
4394{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004395 if (pt != NULL)
4396 {
4397 if (--pt->pt_refcount <= 0)
4398 partial_free(pt);
4399
4400 // If the reference count goes down to one, the funcstack may be the
4401 // only reference and can be freed if no other partials reference it.
4402 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4403 funcstack_check_refcount(pt->pt_funcstack);
4404 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004405}
4406
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004407/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004408 * Return the next (unique) copy ID.
4409 * Used for serializing nested structures.
4410 */
4411 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004412get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004413{
4414 current_copyID += COPYID_INC;
4415 return current_copyID;
4416}
4417
4418/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004419 * Garbage collection for lists and dictionaries.
4420 *
4421 * We use reference counts to be able to free most items right away when they
4422 * are no longer used. But for composite items it's possible that it becomes
4423 * unused while the reference count is > 0: When there is a recursive
4424 * reference. Example:
4425 * :let l = [1, 2, 3]
4426 * :let d = {9: l}
4427 * :let l[1] = d
4428 *
4429 * Since this is quite unusual we handle this with garbage collection: every
4430 * once in a while find out which lists and dicts are not referenced from any
4431 * variable.
4432 *
4433 * Here is a good reference text about garbage collection (refers to Python
4434 * but it applies to all reference-counting mechanisms):
4435 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004436 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004437
4438/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004439 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004440 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004441 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004442 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004443 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004444garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004445{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004446 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004447 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004448 buf_T *buf;
4449 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004450 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004451 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004452
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004453 if (!testing)
4454 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004455 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004456 want_garbage_collect = FALSE;
4457 may_garbage_collect = FALSE;
4458 garbage_collect_at_exit = FALSE;
4459 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004460
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004461 // The execution stack can grow big, limit the size.
4462 if (exestack.ga_maxlen - exestack.ga_len > 500)
4463 {
4464 size_t new_len;
4465 char_u *pp;
4466 int n;
4467
4468 // Keep 150% of the current size, with a minimum of the growth size.
4469 n = exestack.ga_len / 2;
4470 if (n < exestack.ga_growsize)
4471 n = exestack.ga_growsize;
4472
4473 // Don't make it bigger though.
4474 if (exestack.ga_len + n < exestack.ga_maxlen)
4475 {
4476 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4477 pp = vim_realloc(exestack.ga_data, new_len);
4478 if (pp == NULL)
4479 return FAIL;
4480 exestack.ga_maxlen = exestack.ga_len + n;
4481 exestack.ga_data = pp;
4482 }
4483 }
4484
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004485 // We advance by two because we add one for items referenced through
4486 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004487 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004488
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004489 /*
4490 * 1. Go through all accessible variables and mark all lists and dicts
4491 * with copyID.
4492 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004493
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004494 // Don't free variables in the previous_funccal list unless they are only
4495 // referenced through previous_funccal. This must be first, because if
4496 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004497 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004498
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004499 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004500 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004501
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004502 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004503 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004504 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4505 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004506
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004507 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004508 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004509 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4510 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004511 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004512 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4513 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004514#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004515 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004516 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4517 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004518 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004519 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004520 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4521 NULL, NULL);
4522#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004523
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004524 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004525 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004526 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4527 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004528 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004529 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004530
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004531 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004532 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004533
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004534 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004535 abort = abort || set_ref_in_functions(copyID);
4536
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004537 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004538 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004539
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004540 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004541 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004542
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004543 // callbacks in buffers
4544 abort = abort || set_ref_in_buffers(copyID);
4545
Bram Moolenaar1dced572012-04-05 16:54:08 +02004546#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004547 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004548#endif
4549
Bram Moolenaardb913952012-06-29 12:54:53 +02004550#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004551 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004552#endif
4553
4554#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004555 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004556#endif
4557
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004558#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004559 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004560 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004561#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004562#ifdef FEAT_NETBEANS_INTG
4563 abort = abort || set_ref_in_nb_channel(copyID);
4564#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004565
Bram Moolenaare3188e22016-05-31 21:13:04 +02004566#ifdef FEAT_TIMERS
4567 abort = abort || set_ref_in_timer(copyID);
4568#endif
4569
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004570#ifdef FEAT_QUICKFIX
4571 abort = abort || set_ref_in_quickfix(copyID);
4572#endif
4573
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004574#ifdef FEAT_TERMINAL
4575 abort = abort || set_ref_in_term(copyID);
4576#endif
4577
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004578#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004579 abort = abort || set_ref_in_popups(copyID);
4580#endif
4581
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004582 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004583 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004584 /*
4585 * 2. Free lists and dictionaries that are not referenced.
4586 */
4587 did_free = free_unref_items(copyID);
4588
4589 /*
4590 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004591 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004592 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004593 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004594 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004595 else if (p_verbose > 0)
4596 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004597 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004598 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004599
4600 return did_free;
4601}
4602
4603/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004604 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004605 */
4606 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004607free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004608{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004609 int did_free = FALSE;
4610
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004611 // Let all "free" functions know that we are here. This means no
4612 // dictionaries, lists, channels or jobs are to be freed, because we will
4613 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004614 in_free_unref_items = TRUE;
4615
4616 /*
4617 * PASS 1: free the contents of the items. We don't free the items
4618 * themselves yet, so that it is possible to decrement refcount counters
4619 */
4620
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004621 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004622 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004623
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004624 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004625 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004626
4627#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004628 // Go through the list of jobs and free items without the copyID. This
4629 // must happen before doing channels, because jobs refer to channels, but
4630 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004631 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4632
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004633 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004634 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4635#endif
4636
4637 /*
4638 * PASS 2: free the items themselves.
4639 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004640 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004641 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004642
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004643#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004644 // Go through the list of jobs and free items without the copyID. This
4645 // must happen before doing channels, because jobs refer to channels, but
4646 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004647 free_unused_jobs(copyID, COPYID_MASK);
4648
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004649 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004650 free_unused_channels(copyID, COPYID_MASK);
4651#endif
4652
4653 in_free_unref_items = FALSE;
4654
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004655 return did_free;
4656}
4657
4658/*
4659 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004660 * "list_stack" is used to add lists to be marked. Can be NULL.
4661 *
4662 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004663 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004664 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004665set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004666{
4667 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004668 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004669 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004670 hashtab_T *cur_ht;
4671 ht_stack_T *ht_stack = NULL;
4672 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004673
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004674 cur_ht = ht;
4675 for (;;)
4676 {
4677 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004678 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004679 // Mark each item in the hashtab. If the item contains a hashtab
4680 // it is added to ht_stack, if it contains a list it is added to
4681 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004682 todo = (int)cur_ht->ht_used;
4683 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4684 if (!HASHITEM_EMPTY(hi))
4685 {
4686 --todo;
4687 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4688 &ht_stack, list_stack);
4689 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004690 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004691
4692 if (ht_stack == NULL)
4693 break;
4694
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004695 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004696 cur_ht = ht_stack->ht;
4697 tempitem = ht_stack;
4698 ht_stack = ht_stack->prev;
4699 free(tempitem);
4700 }
4701
4702 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004703}
4704
4705/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004706 * Mark a dict and its items with "copyID".
4707 * Returns TRUE if setting references failed somehow.
4708 */
4709 int
4710set_ref_in_dict(dict_T *d, int copyID)
4711{
4712 if (d != NULL && d->dv_copyID != copyID)
4713 {
4714 d->dv_copyID = copyID;
4715 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4716 }
4717 return FALSE;
4718}
4719
4720/*
4721 * Mark a list and its items with "copyID".
4722 * Returns TRUE if setting references failed somehow.
4723 */
4724 int
4725set_ref_in_list(list_T *ll, int copyID)
4726{
4727 if (ll != NULL && ll->lv_copyID != copyID)
4728 {
4729 ll->lv_copyID = copyID;
4730 return set_ref_in_list_items(ll, copyID, NULL);
4731 }
4732 return FALSE;
4733}
4734
4735/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004736 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004737 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4738 *
4739 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004740 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004741 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004742set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004743{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004744 listitem_T *li;
4745 int abort = FALSE;
4746 list_T *cur_l;
4747 list_stack_T *list_stack = NULL;
4748 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004749
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004750 cur_l = l;
4751 for (;;)
4752 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004753 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004754 // Mark each item in the list. If the item contains a hashtab
4755 // it is added to ht_stack, if it contains a list it is added to
4756 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004757 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4758 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4759 ht_stack, &list_stack);
4760 if (list_stack == NULL)
4761 break;
4762
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004763 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004764 cur_l = list_stack->list;
4765 tempitem = list_stack;
4766 list_stack = list_stack->prev;
4767 free(tempitem);
4768 }
4769
4770 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004771}
4772
4773/*
4774 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004775 * "list_stack" is used to add lists to be marked. Can be NULL.
4776 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4777 *
4778 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004779 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004780 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004781set_ref_in_item(
4782 typval_T *tv,
4783 int copyID,
4784 ht_stack_T **ht_stack,
4785 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004786{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004787 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004788
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004789 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004790 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004791 dict_T *dd = tv->vval.v_dict;
4792
Bram Moolenaara03f2332016-02-06 18:09:59 +01004793 if (dd != NULL && dd->dv_copyID != copyID)
4794 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004795 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004796 dd->dv_copyID = copyID;
4797 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004798 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004799 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4800 }
4801 else
4802 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004803 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4804
Bram Moolenaara03f2332016-02-06 18:09:59 +01004805 if (newitem == NULL)
4806 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004807 else
4808 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004809 newitem->ht = &dd->dv_hashtab;
4810 newitem->prev = *ht_stack;
4811 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004812 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004813 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004814 }
4815 }
4816 else if (tv->v_type == VAR_LIST)
4817 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004818 list_T *ll = tv->vval.v_list;
4819
Bram Moolenaara03f2332016-02-06 18:09:59 +01004820 if (ll != NULL && ll->lv_copyID != copyID)
4821 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004822 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004823 ll->lv_copyID = copyID;
4824 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004825 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004826 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004827 }
4828 else
4829 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004830 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4831
Bram Moolenaara03f2332016-02-06 18:09:59 +01004832 if (newitem == NULL)
4833 abort = TRUE;
4834 else
4835 {
4836 newitem->list = ll;
4837 newitem->prev = *list_stack;
4838 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004839 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004840 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004841 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004842 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004843 else if (tv->v_type == VAR_FUNC)
4844 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004845 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004846 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004847 else if (tv->v_type == VAR_PARTIAL)
4848 {
4849 partial_T *pt = tv->vval.v_partial;
4850 int i;
4851
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004852 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004853 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004854 // Didn't see this partial yet.
4855 pt->pt_copyID = copyID;
4856
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004857 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004858
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004859 if (pt->pt_dict != NULL)
4860 {
4861 typval_T dtv;
4862
4863 dtv.v_type = VAR_DICT;
4864 dtv.vval.v_dict = pt->pt_dict;
4865 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4866 }
4867
4868 for (i = 0; i < pt->pt_argc; ++i)
4869 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4870 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004871 if (pt->pt_funcstack != NULL)
4872 {
4873 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4874
4875 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4876 abort = abort || set_ref_in_item(stack + i, copyID,
4877 ht_stack, list_stack);
4878 }
4879
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004880 }
4881 }
4882#ifdef FEAT_JOB_CHANNEL
4883 else if (tv->v_type == VAR_JOB)
4884 {
4885 job_T *job = tv->vval.v_job;
4886 typval_T dtv;
4887
4888 if (job != NULL && job->jv_copyID != copyID)
4889 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004890 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004891 if (job->jv_channel != NULL)
4892 {
4893 dtv.v_type = VAR_CHANNEL;
4894 dtv.vval.v_channel = job->jv_channel;
4895 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4896 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004897 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004898 {
4899 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004900 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004901 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4902 }
4903 }
4904 }
4905 else if (tv->v_type == VAR_CHANNEL)
4906 {
4907 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004908 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004909 typval_T dtv;
4910 jsonq_T *jq;
4911 cbq_T *cq;
4912
4913 if (ch != NULL && ch->ch_copyID != copyID)
4914 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004915 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004916 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004917 {
4918 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4919 jq = jq->jq_next)
4920 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4921 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4922 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004923 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004924 {
4925 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004926 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004927 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4928 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004929 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004930 {
4931 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004932 dtv.vval.v_partial =
4933 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004934 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4935 }
4936 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004937 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004938 {
4939 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004940 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004941 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4942 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004943 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004944 {
4945 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004946 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004947 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4948 }
4949 }
4950 }
4951#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004952 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004953}
4954
Bram Moolenaar8c711452005-01-14 21:53:12 +00004955/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004956 * Return a string with the string representation of a variable.
4957 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004958 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004959 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004960 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004961 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02004962 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004963 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4964 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004965 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004966 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004967 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004968echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004969 typval_T *tv,
4970 char_u **tofree,
4971 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004972 int copyID,
4973 int echo_style,
4974 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004975 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004976{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004977 static int recurse = 0;
4978 char_u *r = NULL;
4979
Bram Moolenaar33570922005-01-25 22:26:29 +00004980 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004981 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004982 if (!did_echo_string_emsg)
4983 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004984 // Only give this message once for a recursive call to avoid
4985 // flooding the user with errors. And stop iterating over lists
4986 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004987 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004988 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004989 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004990 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004991 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004992 }
4993 ++recurse;
4994
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004995 switch (tv->v_type)
4996 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004997 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004998 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004999 {
5000 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005001 r = tv->vval.v_string;
5002 if (r == NULL)
5003 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005004 }
5005 else
5006 {
5007 *tofree = string_quote(tv->vval.v_string, FALSE);
5008 r = *tofree;
5009 }
5010 break;
5011
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005012 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005013 if (echo_style)
5014 {
5015 *tofree = NULL;
5016 r = tv->vval.v_string;
5017 }
5018 else
5019 {
5020 *tofree = string_quote(tv->vval.v_string, TRUE);
5021 r = *tofree;
5022 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005023 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005024
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005025 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005026 {
5027 partial_T *pt = tv->vval.v_partial;
5028 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005029 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005030 garray_T ga;
5031 int i;
5032 char_u *tf;
5033
5034 ga_init2(&ga, 1, 100);
5035 ga_concat(&ga, (char_u *)"function(");
5036 if (fname != NULL)
5037 {
5038 ga_concat(&ga, fname);
5039 vim_free(fname);
5040 }
5041 if (pt != NULL && pt->pt_argc > 0)
5042 {
5043 ga_concat(&ga, (char_u *)", [");
5044 for (i = 0; i < pt->pt_argc; ++i)
5045 {
5046 if (i > 0)
5047 ga_concat(&ga, (char_u *)", ");
5048 ga_concat(&ga,
5049 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5050 vim_free(tf);
5051 }
5052 ga_concat(&ga, (char_u *)"]");
5053 }
5054 if (pt != NULL && pt->pt_dict != NULL)
5055 {
5056 typval_T dtv;
5057
5058 ga_concat(&ga, (char_u *)", ");
5059 dtv.v_type = VAR_DICT;
5060 dtv.vval.v_dict = pt->pt_dict;
5061 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5062 vim_free(tf);
5063 }
5064 ga_concat(&ga, (char_u *)")");
5065
5066 *tofree = ga.ga_data;
5067 r = *tofree;
5068 break;
5069 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005070
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005071 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005072 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005073 break;
5074
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005075 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005076 if (tv->vval.v_list == NULL)
5077 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005078 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005079 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005080 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005081 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005082 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5083 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005084 {
5085 *tofree = NULL;
5086 r = (char_u *)"[...]";
5087 }
5088 else
5089 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005090 int old_copyID = tv->vval.v_list->lv_copyID;
5091
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005092 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005093 *tofree = list2string(tv, copyID, restore_copyID);
5094 if (restore_copyID)
5095 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005096 r = *tofree;
5097 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005098 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005099
Bram Moolenaar8c711452005-01-14 21:53:12 +00005100 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005101 if (tv->vval.v_dict == NULL)
5102 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005103 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005104 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005105 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005106 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005107 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5108 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005109 {
5110 *tofree = NULL;
5111 r = (char_u *)"{...}";
5112 }
5113 else
5114 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005115 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005116
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005117 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005118 *tofree = dict2string(tv, copyID, restore_copyID);
5119 if (restore_copyID)
5120 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005121 r = *tofree;
5122 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005123 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005124
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005125 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005126 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005127 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005128 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005129 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005130 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005131 break;
5132
Bram Moolenaar835dc632016-02-07 14:27:38 +01005133 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005134 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005135#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005136 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005137 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5138 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005139 if (composite_val)
5140 {
5141 *tofree = string_quote(r, FALSE);
5142 r = *tofree;
5143 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005144#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005145 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005146
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005147 case VAR_INSTR:
5148 *tofree = NULL;
5149 r = (char_u *)"instructions";
5150 break;
5151
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005152 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005153#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005154 *tofree = NULL;
5155 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5156 r = numbuf;
5157 break;
5158#endif
5159
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005160 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005161 case VAR_SPECIAL:
5162 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005163 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005164 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005165 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005166
Bram Moolenaar8502c702014-06-17 12:51:16 +02005167 if (--recurse == 0)
5168 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005169 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005170}
5171
5172/*
5173 * Return a string with the string representation of a variable.
5174 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5175 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005176 * Does not put quotes around strings, as ":echo" displays values.
5177 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5178 * May return NULL.
5179 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005180 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005181echo_string(
5182 typval_T *tv,
5183 char_u **tofree,
5184 char_u *numbuf,
5185 int copyID)
5186{
5187 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5188}
5189
5190/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005191 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5192 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005193 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005194 */
5195 int
5196buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5197{
5198 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005199 char_u *t;
5200 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005201
5202 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5203 return -1;
5204
5205 if (lnum > buf->b_ml.ml_line_count)
5206 lnum = buf->b_ml.ml_line_count;
5207
5208 str = ml_get_buf(buf, lnum, FALSE);
5209 if (str == NULL)
5210 return -1;
5211
5212 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005213 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005214
Bram Moolenaar91458462021-01-13 20:08:38 +01005215 // count the number of characters
5216 t = str;
5217 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5218 t += mb_ptr2len(t);
5219
5220 // In insert mode, when the cursor is at the end of a non-empty line,
5221 // byteidx points to the NUL character immediately past the end of the
5222 // string. In this case, add one to the character count.
5223 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5224 count++;
5225
5226 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005227}
5228
5229/*
5230 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005231 * byte index. Works only for loaded buffers. Returns -1 on failure.
5232 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005233 */
5234 int
5235buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5236{
5237 char_u *str;
5238 char_u *t;
5239
5240 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5241 return -1;
5242
5243 if (lnum > buf->b_ml.ml_line_count)
5244 lnum = buf->b_ml.ml_line_count;
5245
5246 str = ml_get_buf(buf, lnum, FALSE);
5247 if (str == NULL)
5248 return -1;
5249
5250 // Convert the character offset to a byte offset
5251 t = str;
5252 while (*t != NUL && --charidx > 0)
5253 t += mb_ptr2len(t);
5254
Bram Moolenaar91458462021-01-13 20:08:38 +01005255 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005256}
5257
5258/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005260 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005262 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005263var2fpos(
5264 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005265 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005266 int *fnum, // set to fnum for '0, 'A, etc.
5267 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005269 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005271 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005273 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005274 if (varp->v_type == VAR_LIST)
5275 {
5276 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005277 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005278 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005279 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005280
5281 l = varp->vval.v_list;
5282 if (l == NULL)
5283 return NULL;
5284
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005285 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005286 pos.lnum = list_find_nr(l, 0L, &error);
5287 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005288 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005289 if (charcol)
5290 len = (long)mb_charlen(ml_get(pos.lnum));
5291 else
5292 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005293
Bram Moolenaarec65d772020-08-20 22:29:12 +02005294 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005295 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005296 li = list_find(l, 1L);
5297 if (li != NULL && li->li_tv.v_type == VAR_STRING
5298 && li->li_tv.vval.v_string != NULL
5299 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005300 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005301 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005302 }
5303 else
5304 {
5305 pos.col = list_find_nr(l, 1L, &error);
5306 if (error)
5307 return NULL;
5308 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005309
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005310 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005311 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005312 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005313 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005314
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005315 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005316 pos.coladd = list_find_nr(l, 2L, &error);
5317 if (error)
5318 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005319
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005320 return &pos;
5321 }
5322
Bram Moolenaarc5809432021-03-27 21:23:30 +01005323 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5324 return NULL;
5325
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005326 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005327 if (name == NULL)
5328 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005329 if (name[0] == '.') // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005330 {
5331 pos = curwin->w_cursor;
5332 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005333 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005334 return &pos;
5335 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005336 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005337 {
5338 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005339 pos = VIsual;
5340 else
5341 pos = curwin->w_cursor;
5342 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005343 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005344 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005345 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005346 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005348 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5350 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005351 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005352 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 return pp;
5354 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005355
Bram Moolenaara5525202006-03-02 22:52:09 +00005356 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005357
Bram Moolenaar477933c2007-07-17 14:32:23 +00005358 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005359 {
5360 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005361 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005362 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005363 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005364 // In silent Ex mode topline is zero, but that's not a valid line
5365 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005366 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005367 return &pos;
5368 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005369 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005370 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005371 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005372 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005373 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005374 return &pos;
5375 }
5376 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005377 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005379 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 {
5381 pos.lnum = curbuf->b_ml.ml_line_count;
5382 pos.col = 0;
5383 }
5384 else
5385 {
5386 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005387 if (charcol)
5388 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5389 else
5390 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 }
5392 return &pos;
5393 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005394 if (in_vim9script())
5395 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 return NULL;
5397}
5398
5399/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005400 * Convert list in "arg" into a position and optional file number.
5401 * When "fnump" is NULL there is no file number, only 3 items.
5402 * Note that the column is passed on as-is, the caller may want to decrement
5403 * it to use 1 for the first column.
5404 * Return FAIL when conversion is not possible, doesn't check the position for
5405 * validity.
5406 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005407 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005408list2fpos(
5409 typval_T *arg,
5410 pos_T *posp,
5411 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005412 colnr_T *curswantp,
5413 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005414{
5415 list_T *l = arg->vval.v_list;
5416 long i = 0;
5417 long n;
5418
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005419 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5420 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005421 if (arg->v_type != VAR_LIST
5422 || l == NULL
5423 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005424 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005425 return FAIL;
5426
5427 if (fnump != NULL)
5428 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005429 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005430 if (n < 0)
5431 return FAIL;
5432 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005433 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005434 *fnump = n;
5435 }
5436
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005437 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005438 if (n < 0)
5439 return FAIL;
5440 posp->lnum = n;
5441
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005442 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005443 if (n < 0)
5444 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005445 // If character position is specified, then convert to byte position
5446 if (charcol)
5447 {
5448 buf_T *buf;
5449
5450 // Get the text for the specified line in a loaded buffer
5451 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5452 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5453 return FAIL;
5454
Bram Moolenaar91458462021-01-13 20:08:38 +01005455 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005456 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005457 posp->col = n;
5458
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005459 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005460 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005461 posp->coladd = 0;
5462 else
5463 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005464
Bram Moolenaar493c1782014-05-28 14:34:46 +02005465 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005466 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005467
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005468 return OK;
5469}
5470
5471/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 * Get the length of an environment variable name.
5473 * Advance "arg" to the first character after the name.
5474 * Return 0 for error.
5475 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005476 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005477get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478{
5479 char_u *p;
5480 int len;
5481
5482 for (p = *arg; vim_isIDc(*p); ++p)
5483 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005484 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 return 0;
5486
5487 len = (int)(p - *arg);
5488 *arg = p;
5489 return len;
5490}
5491
5492/*
5493 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005494 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 * Return 0 if something is wrong.
5496 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005497 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005498get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499{
5500 char_u *p;
5501 int len;
5502
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005503 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005505 {
5506 if (*p == ':')
5507 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005508 // "s:" is start of "s:var", but "n:" is not and can be used in
5509 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005510 len = (int)(p - *arg);
5511 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5512 || len > 1)
5513 break;
5514 }
5515 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005516 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 return 0;
5518
5519 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005520 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521
5522 return len;
5523}
5524
5525/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005526 * Get the length of the name of a variable or function.
5527 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005529 * Return -1 if curly braces expansion failed.
5530 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 * If the name contains 'magic' {}'s, expand them and return the
5532 * expanded name in an allocated string via 'alias' - caller must free.
5533 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005534 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005535get_name_len(
5536 char_u **arg,
5537 char_u **alias,
5538 int evaluate,
5539 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540{
5541 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 char_u *p;
5543 char_u *expr_start;
5544 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005546 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547
5548 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5549 && (*arg)[2] == (int)KE_SNR)
5550 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005551 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 *arg += 3;
5553 return get_id_len(arg) + 3;
5554 }
5555 len = eval_fname_script(*arg);
5556 if (len > 0)
5557 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005558 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 *arg += len;
5560 }
5561
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005563 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005565 p = find_name_end(*arg, &expr_start, &expr_end,
5566 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 if (expr_start != NULL)
5568 {
5569 char_u *temp_string;
5570
5571 if (!evaluate)
5572 {
5573 len += (int)(p - *arg);
5574 *arg = skipwhite(p);
5575 return len;
5576 }
5577
5578 /*
5579 * Include any <SID> etc in the expanded string:
5580 * Thus the -len here.
5581 */
5582 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5583 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005584 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 *alias = temp_string;
5586 *arg = skipwhite(p);
5587 return (int)STRLEN(temp_string);
5588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589
5590 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005591 // Only give an error when there is something, otherwise it will be
5592 // reported at a higher level.
5593 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005594 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595
5596 return len;
5597}
5598
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005599/*
5600 * Find the end of a variable or function name, taking care of magic braces.
5601 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5602 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005603 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005604 * Return a pointer to just after the name. Equal to "arg" if there is no
5605 * valid name.
5606 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005607 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005608find_name_end(
5609 char_u *arg,
5610 char_u **expr_start,
5611 char_u **expr_end,
5612 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005614 int mb_nest = 0;
5615 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005617 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005618 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005620 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005622 *expr_start = NULL;
5623 *expr_end = NULL;
5624 }
5625
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005626 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005627 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5628 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005629 return arg;
5630
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005631 for (p = arg; *p != NUL
5632 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005633 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005634 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005635 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005636 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005637 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005638 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005639 if (*p == '\'')
5640 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005641 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005642 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005643 ;
5644 if (*p == NUL)
5645 break;
5646 }
5647 else if (*p == '"')
5648 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005649 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005650 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005651 if (*p == '\\' && p[1] != NUL)
5652 ++p;
5653 if (*p == NUL)
5654 break;
5655 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005656 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5657 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005658 // "s:" is start of "s:var", but "n:" is not and can be used in
5659 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005660 len = (int)(p - arg);
5661 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005662 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005663 break;
5664 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005665
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005666 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005668 if (*p == '[')
5669 ++br_nest;
5670 else if (*p == ']')
5671 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005673
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005674 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005676 if (*p == '{')
5677 {
5678 mb_nest++;
5679 if (expr_start != NULL && *expr_start == NULL)
5680 *expr_start = p;
5681 }
5682 else if (*p == '}')
5683 {
5684 mb_nest--;
5685 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5686 *expr_end = p;
5687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 }
5690
5691 return p;
5692}
5693
5694/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005695 * Expands out the 'magic' {}'s in a variable/function name.
5696 * Note that this can call itself recursively, to deal with
5697 * constructs like foo{bar}{baz}{bam}
5698 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5699 * "in_start" ^
5700 * "expr_start" ^
5701 * "expr_end" ^
5702 * "in_end" ^
5703 *
5704 * Returns a new allocated string, which the caller must free.
5705 * Returns NULL for failure.
5706 */
5707 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005708make_expanded_name(
5709 char_u *in_start,
5710 char_u *expr_start,
5711 char_u *expr_end,
5712 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005713{
5714 char_u c1;
5715 char_u *retval = NULL;
5716 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005717
5718 if (expr_end == NULL || in_end == NULL)
5719 return NULL;
5720 *expr_start = NUL;
5721 *expr_end = NUL;
5722 c1 = *in_end;
5723 *in_end = NUL;
5724
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005725 temp_result = eval_to_string(expr_start + 1, FALSE);
5726 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005727 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005728 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5729 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005730 if (retval != NULL)
5731 {
5732 STRCPY(retval, in_start);
5733 STRCAT(retval, temp_result);
5734 STRCAT(retval, expr_end + 1);
5735 }
5736 }
5737 vim_free(temp_result);
5738
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005739 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005740 *expr_start = '{';
5741 *expr_end = '}';
5742
5743 if (retval != NULL)
5744 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005745 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005746 if (expr_start != NULL)
5747 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005748 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005749 temp_result = make_expanded_name(retval, expr_start,
5750 expr_end, temp_result);
5751 vim_free(retval);
5752 retval = temp_result;
5753 }
5754 }
5755
5756 return retval;
5757}
5758
5759/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005761 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005763 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005764eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005766 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005767}
5768
5769/*
5770 * Return TRUE if character "c" can be used as the first character in a
5771 * variable or function name (excluding '{' and '}').
5772 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005773 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005774eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005775{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005776 return ASCII_ISALPHA(c) || c == '_';
5777}
5778
5779/*
5780 * Return TRUE if character "c" can be used as the first character of a
5781 * dictionary key.
5782 */
5783 int
5784eval_isdictc(int c)
5785{
5786 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787}
5788
5789/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005790 * Handle:
5791 * - expr[expr], expr[expr:expr] subscript
5792 * - ".name" lookup
5793 * - function call with Funcref variable: func(expr)
5794 * - method call: var->method()
5795 *
5796 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005797 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005798 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005799handle_subscript(
5800 char_u **arg,
5801 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005802 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005803 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005804{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005805 int evaluate = evalarg != NULL
5806 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005807 int ret = OK;
5808 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005809 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005810 int getnext;
5811 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005812
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005813 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005814 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005815 // When at the end of the line and ".name" or "->{" or "->X" follows in
5816 // the next line then consume the line break.
5817 p = eval_next_non_blank(*arg, evalarg, &getnext);
5818 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005819 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02005820 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
5821 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
5822 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005823 {
5824 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005825 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005826 check_white = FALSE;
5827 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005828
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01005829 if (rettv->v_type == VAR_ANY)
5830 {
5831 char_u *exp_name;
5832 int cc;
5833 int idx;
5834 ufunc_T *ufunc;
5835 type_T *type;
5836
5837 // Found script from "import * as {name}", script item name must
5838 // follow.
5839 if (**arg != '.')
5840 {
5841 if (verbose)
5842 semsg(_(e_expected_str_but_got_str), "'.'", *arg);
5843 ret = FAIL;
5844 break;
5845 }
5846 ++*arg;
5847 if (IS_WHITE_OR_NUL(**arg))
5848 {
5849 if (verbose)
5850 emsg(_(e_no_white_space_allowed_after_dot));
5851 ret = FAIL;
5852 break;
5853 }
5854
5855 // isolate the name
5856 exp_name = *arg;
5857 while (eval_isnamec(**arg))
5858 ++*arg;
5859 cc = **arg;
5860 **arg = NUL;
5861
5862 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
5863 evalarg->eval_cctx, verbose);
5864 **arg = cc;
5865 *arg = skipwhite(*arg);
5866
5867 if (idx < 0 && ufunc == NULL)
5868 {
5869 ret = FAIL;
5870 break;
5871 }
5872 if (idx >= 0)
5873 {
5874 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
5875 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
5876
5877 copy_tv(sv->sv_tv, rettv);
5878 }
5879 else
5880 {
5881 rettv->v_type = VAR_FUNC;
5882 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
5883 }
5884 }
5885
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005886 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5887 || rettv->v_type == VAR_PARTIAL))
5888 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005889 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005890 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5891 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005892
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005893 // Stop the expression evaluation when immediately aborting on
5894 // error, or when an interrupt occurred or an exception was thrown
5895 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005896 if (aborting())
5897 {
5898 if (ret == OK)
5899 clear_tv(rettv);
5900 ret = FAIL;
5901 }
5902 dict_unref(selfdict);
5903 selfdict = NULL;
5904 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005905 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005906 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005907 if (in_vim9script())
5908 *arg = skipwhite(p + 2);
5909 else
5910 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005911 if (ret == OK)
5912 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005913 if (VIM_ISWHITE(**arg))
5914 {
5915 emsg(_(e_nowhitespace));
5916 ret = FAIL;
5917 }
5918 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005919 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005920 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005921 else
5922 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005923 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005924 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005925 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005926 // "." is ".name" lookup when we found a dict or when evaluating and
5927 // scriptversion is at least 2, where string concatenation is "..".
5928 else if (**arg == '['
5929 || (**arg == '.' && (rettv->v_type == VAR_DICT
5930 || (!evaluate
5931 && (*arg)[1] != '.'
5932 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005933 {
5934 dict_unref(selfdict);
5935 if (rettv->v_type == VAR_DICT)
5936 {
5937 selfdict = rettv->vval.v_dict;
5938 if (selfdict != NULL)
5939 ++selfdict->dv_refcount;
5940 }
5941 else
5942 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005943 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005944 {
5945 clear_tv(rettv);
5946 ret = FAIL;
5947 }
5948 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005949 else
5950 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005951 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005952
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005953 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5954 // Don't do this when "Func" is already a partial that was bound
5955 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005956 if (selfdict != NULL
5957 && (rettv->v_type == VAR_FUNC
5958 || (rettv->v_type == VAR_PARTIAL
5959 && (rettv->vval.v_partial->pt_auto
5960 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005961 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005962
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005963 dict_unref(selfdict);
5964 return ret;
5965}
5966
5967/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005968 * Make a copy of an item.
5969 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005970 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5971 * reference to an already copied list/dict can be used.
5972 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005973 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005974 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005975item_copy(
5976 typval_T *from,
5977 typval_T *to,
5978 int deep,
5979 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005980{
5981 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005982 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005983
Bram Moolenaar33570922005-01-25 22:26:29 +00005984 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005985 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005986 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005987 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005988 }
5989 ++recurse;
5990
5991 switch (from->v_type)
5992 {
5993 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005994 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005995 case VAR_STRING:
5996 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005997 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005998 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005999 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006000 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006001 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006002 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006003 copy_tv(from, to);
6004 break;
6005 case VAR_LIST:
6006 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006007 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006008 if (from->vval.v_list == NULL)
6009 to->vval.v_list = NULL;
6010 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6011 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006012 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006013 to->vval.v_list = from->vval.v_list->lv_copylist;
6014 ++to->vval.v_list->lv_refcount;
6015 }
6016 else
6017 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
6018 if (to->vval.v_list == NULL)
6019 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006020 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006021 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006022 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006023 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006024 case VAR_DICT:
6025 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006026 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006027 if (from->vval.v_dict == NULL)
6028 to->vval.v_dict = NULL;
6029 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6030 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006031 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006032 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6033 ++to->vval.v_dict->dv_refcount;
6034 }
6035 else
6036 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
6037 if (to->vval.v_dict == NULL)
6038 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006039 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006040 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006041 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006042 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006043 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006044 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006045 }
6046 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006047 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006048}
6049
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006050 void
6051echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6052{
6053 char_u *tofree;
6054 char_u numbuf[NUMBUFLEN];
6055 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6056
6057 if (*atstart)
6058 {
6059 *atstart = FALSE;
6060 // Call msg_start() after eval1(), evaluating the expression
6061 // may cause a message to appear.
6062 if (with_space)
6063 {
6064 // Mark the saved text as finishing the line, so that what
6065 // follows is displayed on a new line when scrolling back
6066 // at the more prompt.
6067 msg_sb_eol();
6068 msg_start();
6069 }
6070 }
6071 else if (with_space)
6072 msg_puts_attr(" ", echo_attr);
6073
6074 if (p != NULL)
6075 for ( ; *p != NUL && !got_int; ++p)
6076 {
6077 if (*p == '\n' || *p == '\r' || *p == TAB)
6078 {
6079 if (*p != TAB && *needclr)
6080 {
6081 // remove any text still there from the command
6082 msg_clr_eos();
6083 *needclr = FALSE;
6084 }
6085 msg_putchar_attr(*p, echo_attr);
6086 }
6087 else
6088 {
6089 if (has_mbyte)
6090 {
6091 int i = (*mb_ptr2len)(p);
6092
6093 (void)msg_outtrans_len_attr(p, i, echo_attr);
6094 p += i - 1;
6095 }
6096 else
6097 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6098 }
6099 }
6100 vim_free(tofree);
6101}
6102
Bram Moolenaare9a41262005-01-15 22:18:47 +00006103/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 * ":echo expr1 ..." print each argument separated with a space, add a
6105 * newline at the end.
6106 * ":echon expr1 ..." print each argument plain.
6107 */
6108 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006109ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110{
6111 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006112 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006113 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 int needclr = TRUE;
6115 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006116 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006117 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006118 evalarg_T evalarg;
6119
Bram Moolenaare707c882020-07-01 13:07:37 +02006120 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121
6122 if (eap->skip)
6123 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006124 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006126 // If eval1() causes an error message the text from the command may
6127 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006128 need_clr_eos = needclr;
6129
Bram Moolenaar68db9962021-05-09 23:19:22 +02006130 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006131 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 {
6133 /*
6134 * Report the invalid expression unless the expression evaluation
6135 * has been cancelled due to an aborting error, an interrupt, or an
6136 * exception.
6137 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006138 if (!aborting() && did_emsg == did_emsg_before
6139 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006140 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006141 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 break;
6143 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006144 need_clr_eos = FALSE;
6145
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006147 {
6148 if (rettv.v_type == VAR_VOID)
6149 {
6150 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6151 break;
6152 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006153 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006154 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006155
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006156 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 arg = skipwhite(arg);
6158 }
6159 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006160 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161
6162 if (eap->skip)
6163 --emsg_skip;
6164 else
6165 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006166 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 if (needclr)
6168 msg_clr_eos();
6169 if (eap->cmdidx == CMD_echo)
6170 msg_end();
6171 }
6172}
6173
6174/*
6175 * ":echohl {name}".
6176 */
6177 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006178ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006180 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181}
6182
6183/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006184 * Returns the :echo attribute
6185 */
6186 int
6187get_echo_attr(void)
6188{
6189 return echo_attr;
6190}
6191
6192/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 * ":execute expr1 ..." execute the result of an expression.
6194 * ":echomsg expr1 ..." Print a message
6195 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006196 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 * Each gets spaces around each argument and a newline at the end for
6198 * echo commands
6199 */
6200 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006201ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202{
6203 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006204 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 int ret = OK;
6206 char_u *p;
6207 garray_T ga;
6208 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006209 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210
6211 ga_init2(&ga, 1, 80);
6212
6213 if (eap->skip)
6214 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006215 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006217 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006218 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220
6221 if (!eap->skip)
6222 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006223 char_u buf[NUMBUFLEN];
6224
6225 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006226 {
6227 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6228 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006229 semsg(_(e_using_invalid_value_as_string_str),
6230 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006231 p = NULL;
6232 }
6233 else
6234 p = tv_get_string_buf(&rettv, buf);
6235 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006236 else
6237 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006238 if (p == NULL)
6239 {
6240 clear_tv(&rettv);
6241 ret = FAIL;
6242 break;
6243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 len = (int)STRLEN(p);
6245 if (ga_grow(&ga, len + 2) == FAIL)
6246 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006247 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 ret = FAIL;
6249 break;
6250 }
6251 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 ga.ga_len += len;
6255 }
6256
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006257 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 arg = skipwhite(arg);
6259 }
6260
6261 if (ret != FAIL && ga.ga_data != NULL)
6262 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006263 // use the first line of continuation lines for messages
6264 SOURCING_LNUM = start_lnum;
6265
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006266 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6267 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006268 // Mark the already saved text as finishing the line, so that what
6269 // follows is displayed on a new line when scrolling back at the
6270 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006271 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006272 }
6273
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006275 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006276 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006277 out_flush();
6278 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006279 else if (eap->cmdidx == CMD_echoconsole)
6280 {
6281 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6282 ui_write((char_u *)"\r\n", 2, TRUE);
6283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 else if (eap->cmdidx == CMD_echoerr)
6285 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006286 int save_did_emsg = did_emsg;
6287
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006288 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006289 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 if (!force_abort)
6291 did_emsg = save_did_emsg;
6292 }
6293 else if (eap->cmdidx == CMD_execute)
6294 do_cmdline((char_u *)ga.ga_data,
6295 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6296 }
6297
6298 ga_clear(&ga);
6299
6300 if (eap->skip)
6301 --emsg_skip;
6302
6303 eap->nextcmd = check_nextcmd(arg);
6304}
6305
6306/*
6307 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6308 * "arg" points to the "&" or '+' when called, to "option" when returning.
6309 * Returns NULL when no option name found. Otherwise pointer to the char
6310 * after the option name.
6311 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006312 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006313find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314{
6315 char_u *p = *arg;
6316
6317 ++p;
6318 if (*p == 'g' && p[1] == ':')
6319 {
6320 *opt_flags = OPT_GLOBAL;
6321 p += 2;
6322 }
6323 else if (*p == 'l' && p[1] == ':')
6324 {
6325 *opt_flags = OPT_LOCAL;
6326 p += 2;
6327 }
6328 else
6329 *opt_flags = 0;
6330
6331 if (!ASCII_ISALPHA(*p))
6332 return NULL;
6333 *arg = p;
6334
6335 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006336 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 else
6338 while (ASCII_ISALPHA(*p))
6339 ++p;
6340 return p;
6341}
6342
6343/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006344 * Display script name where an item was last set.
6345 * Should only be invoked when 'verbose' is non-zero.
6346 */
6347 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006348last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006349{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006350 char_u *p;
6351
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006352 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006353 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006354 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006355 if (p != NULL)
6356 {
6357 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006358 msg_puts(_("\n\tLast set from "));
6359 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006360 if (script_ctx.sc_lnum > 0)
6361 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006362 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006363 msg_outnum((long)script_ctx.sc_lnum);
6364 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006365 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006366 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006367 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006368 }
6369}
6370
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006371#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372
6373/*
6374 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006375 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 * "flags" can be "g" to do a global substitute.
6377 * Returns an allocated string, NULL for error.
6378 */
6379 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006380do_string_sub(
6381 char_u *str,
6382 char_u *pat,
6383 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006384 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006385 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386{
6387 int sublen;
6388 regmatch_T regmatch;
6389 int i;
6390 int do_all;
6391 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006392 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 garray_T ga;
6394 char_u *ret;
6395 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006396 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006398 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006400 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401
6402 ga_init2(&ga, 1, 200);
6403
6404 do_all = (flags[0] == 'g');
6405
6406 regmatch.rm_ic = p_ic;
6407 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6408 if (regmatch.regprog != NULL)
6409 {
6410 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006411 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6413 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006414 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006415 if (regmatch.startp[0] == regmatch.endp[0])
6416 {
6417 if (zero_width == regmatch.startp[0])
6418 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006419 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006420 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006421 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6422 (size_t)i);
6423 ga.ga_len += i;
6424 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006425 continue;
6426 }
6427 zero_width = regmatch.startp[0];
6428 }
6429
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 /*
6431 * Get some space for a temporary buffer to do the substitution
6432 * into. It will contain:
6433 * - The text up to where the match is.
6434 * - The substituted text.
6435 * - The text after the match.
6436 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006437 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006438 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6440 {
6441 ga_clear(&ga);
6442 break;
6443 }
6444
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006445 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 i = (int)(regmatch.startp[0] - tail);
6447 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006448 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006449 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 + ga.ga_len + i, TRUE, TRUE, FALSE);
6451 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006452 tail = regmatch.endp[0];
6453 if (*tail == NUL)
6454 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 if (!do_all)
6456 break;
6457 }
6458
6459 if (ga.ga_data != NULL)
6460 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6461
Bram Moolenaar473de612013-06-08 18:19:48 +02006462 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 }
6464
6465 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6466 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006467 if (p_cpo == empty_option)
6468 p_cpo = save_cpo;
6469 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006470 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006471 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006472 // If it's still empty it was changed and restored, need to restore in
6473 // the complicated way.
6474 if (*p_cpo == NUL)
6475 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006476 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478
6479 return ret;
6480}