blob: a5c889af87de6715864584ca62feb620ad9b204d [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010023#define NAMESPACE_CHAR (char_u *)"abglstvw"
24
Bram Moolenaar532c7802005-01-27 14:44:31 +000025/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000026 * When recursively copying lists and dicts we need to remember which ones we
27 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000028 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000029 */
30static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020031
Bram Moolenaar071d4272004-06-13 20:20:40 +000032/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000033 * Info used by a ":for" loop.
34 */
Bram Moolenaar33570922005-01-25 22:26:29 +000035typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000036{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010037 int fi_semicolon; // TRUE if ending in '; var]'
38 int fi_varcount; // nr of variables in the list
Bram Moolenaarb7a78f72020-06-28 18:43:40 +020039 int fi_break_count; // nr of line breaks encountered
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010040 listwatch_T fi_lw; // keep an eye on the item used.
41 list_T *fi_list; // list being used
42 int fi_bi; // index of blob
43 blob_T *fi_blob; // blob being used
Bram Moolenaar74e54fc2021-03-26 20:41:29 +010044 char_u *fi_string; // copy of string being used
45 int fi_byte_idx; // byte index in fi_string
Bram Moolenaar33570922005-01-25 22:26:29 +000046} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000047
Bram Moolenaar48e697e2016-01-23 22:17:30 +010048static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020049static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
50static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
51static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
52static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
53static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +020054static int eval7t(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020055static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +020056static int eval7_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000057
Bram Moolenaar48e697e2016-01-23 22:17:30 +010058static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020059static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Yegappan Lakshmanana2438132021-07-10 21:29:18 +020060static char_u *eval_next_line(evalarg_T *evalarg);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020061
Bram Moolenaare21c1582019-03-02 11:57:09 +010062/*
63 * Return "n1" divided by "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010064 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010065 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020066 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010067num_divide(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010068{
69 varnumber_T result;
70
Bram Moolenaar99880f92021-01-20 21:23:14 +010071 if (n2 == 0)
Bram Moolenaare21c1582019-03-02 11:57:09 +010072 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010073 if (in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010074 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010075 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010076 if (failed != NULL)
77 *failed = TRUE;
78 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010079 if (n1 == 0)
80 result = VARNUM_MIN; // similar to NaN
81 else if (n1 < 0)
82 result = -VARNUM_MAX;
83 else
84 result = VARNUM_MAX;
85 }
86 else
87 result = n1 / n2;
88
89 return result;
90}
91
92/*
93 * Return "n1" modulus "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010094 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010095 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020096 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010097num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010098{
Bram Moolenaar99880f92021-01-20 21:23:14 +010099 if (n2 == 0 && in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +0100100 {
Bram Moolenaar99880f92021-01-20 21:23:14 +0100101 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +0100102 if (failed != NULL)
103 *failed = TRUE;
104 }
Bram Moolenaare21c1582019-03-02 11:57:09 +0100105 return (n2 == 0) ? 0 : (n1 % n2);
106}
107
Bram Moolenaar33570922005-01-25 22:26:29 +0000108/*
109 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000110 */
111 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100112eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000113{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200114 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200115 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000116
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200117#ifdef EBCDIC
118 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100119 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200120 */
121 sortFunctions();
122#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000123}
124
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000125#if defined(EXITFREE) || defined(PROTO)
126 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100127eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000128{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200129 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100130 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200131 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000132
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200133 // autoloaded script names
134 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000135
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200136 // unreferenced lists and dicts
137 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200138
139 // functions not garbage collected
140 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000141}
142#endif
143
Bram Moolenaare6b53242020-07-01 17:28:33 +0200144 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200145fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
146{
147 CLEAR_FIELD(*evalarg);
148 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
149 if (eap != NULL && getline_equal(eap->getline, eap->cookie, getsourceline))
150 {
151 evalarg->eval_getline = eap->getline;
152 evalarg->eval_cookie = eap->cookie;
153 }
154}
155
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156/*
157 * Top level evaluation function, returning a boolean.
158 * Sets "error" to TRUE if there was an error.
159 * Return TRUE or FALSE.
160 */
161 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100162eval_to_bool(
163 char_u *arg,
164 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200165 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100166 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167{
Bram Moolenaar33570922005-01-25 22:26:29 +0000168 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200169 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200170 evalarg_T evalarg;
171
Bram Moolenaar37c83712020-06-30 21:18:36 +0200172 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173
174 if (skip)
175 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200176 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178 else
179 {
180 *error = FALSE;
181 if (!skip)
182 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200183 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200184 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200185 else
186 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000187 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 }
189 }
190 if (skip)
191 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200192 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200194 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195}
196
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100197/*
198 * Call eval1() and give an error message if not done at a lower level.
199 */
200 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200201eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100202{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100203 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100204 int ret;
205 int did_emsg_before = did_emsg;
206 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200207 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100208
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200209 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
210
211 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100212 if (ret == FAIL)
213 {
214 // Report the invalid expression unless the expression evaluation has
215 // been cancelled due to an aborting error, an interrupt, or an
216 // exception, or we already gave a more specific error.
217 // Also check called_emsg for when using assert_fails().
218 if (!aborting() && did_emsg == did_emsg_before
219 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200220 semsg(_(e_invalid_expression_str), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100221 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200222 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100223 return ret;
224}
225
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100226/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200227 * Return whether a typval is a valid expression to pass to eval_expr_typval()
228 * or eval_expr_to_bool(). An empty string returns FALSE;
229 */
230 int
231eval_expr_valid_arg(typval_T *tv)
232{
233 return tv->v_type != VAR_UNKNOWN
234 && (tv->v_type != VAR_STRING
235 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
236}
237
238/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100239 * Evaluate an expression, which can be a function, partial or string.
240 * Pass arguments "argv[argc]".
241 * Return the result in "rettv" and OK or FAIL.
242 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200243 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100244eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
245{
246 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100247 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200248 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100249
250 if (expr->v_type == VAR_FUNC)
251 {
252 s = expr->vval.v_string;
253 if (s == NULL || *s == NUL)
254 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200255 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200256 funcexe.evaluate = TRUE;
257 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100258 return FAIL;
259 }
260 else if (expr->v_type == VAR_PARTIAL)
261 {
262 partial_T *partial = expr->vval.v_partial;
263
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200264 if (partial == NULL)
265 return FAIL;
266
Bram Moolenaar822ba242020-05-24 23:00:18 +0200267 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200268 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100269 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200270 if (call_def_function(partial->pt_func, argc, argv,
271 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100272 return FAIL;
273 }
274 else
275 {
276 s = partial_name(partial);
277 if (s == NULL || *s == NUL)
278 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200279 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100280 funcexe.evaluate = TRUE;
281 funcexe.partial = partial;
282 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
283 return FAIL;
284 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100285 }
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200286 else if (expr->v_type == VAR_INSTR)
287 {
288 return exe_typval_instr(expr, rettv);
289 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100290 else
291 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100292 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100293 if (s == NULL)
294 return FAIL;
295 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200296 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100297 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200298 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100299 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200300 clear_tv(rettv);
Bram Moolenaar108010a2021-06-27 22:03:33 +0200301 semsg(_(e_invalid_expression_str), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100302 return FAIL;
303 }
304 }
305 return OK;
306}
307
308/*
309 * Like eval_to_bool() but using a typval_T instead of a string.
310 * Works for string, funcref and partial.
311 */
312 int
313eval_expr_to_bool(typval_T *expr, int *error)
314{
315 typval_T rettv;
316 int res;
317
318 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
319 {
320 *error = TRUE;
321 return FALSE;
322 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200323 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100324 clear_tv(&rettv);
325 return res;
326}
327
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328/*
329 * Top level evaluation function, returning a string. If "skip" is TRUE,
330 * only parsing to "nextcmd" is done, without reporting errors. Return
331 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
332 */
333 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100334eval_to_string_skip(
335 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200336 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100337 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338{
Bram Moolenaar33570922005-01-25 22:26:29 +0000339 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200341 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342
Bram Moolenaar37c83712020-06-30 21:18:36 +0200343 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 if (skip)
345 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200346 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 retval = NULL;
348 else
349 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100350 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000351 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 }
353 if (skip)
354 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200355 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356
357 return retval;
358}
359
360/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000361 * Skip over an expression at "*pp".
362 * Return FAIL for an error, OK otherwise.
363 */
364 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200365skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000366{
Bram Moolenaar33570922005-01-25 22:26:29 +0000367 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000368
369 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200370 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000371}
372
373/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200374 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200375 * If in Vim9 script and line breaks are encountered, the lines are
376 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200377 * "arg" is advanced to just after the expression.
378 * "start" is set to the start of the expression, "end" to just after the end.
379 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200380 * Return FAIL for an error, OK otherwise.
381 */
382 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200383skip_expr_concatenate(
384 char_u **arg,
385 char_u **start,
386 char_u **end,
387 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200388{
389 typval_T rettv;
390 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200391 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200392 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200393 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200394 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200395 int evaluate = evalarg == NULL
396 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200397
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200398 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200399 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200400 {
401 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200402 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200403 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200404 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200405 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200406 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200407 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200408
409 // Don't evaluate the expression.
410 if (evalarg != NULL)
411 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200412 *arg = skipwhite(*arg);
413 res = eval1(arg, &rettv, evalarg);
414 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200415 if (evalarg != NULL)
416 evalarg->eval_flags = save_flags;
417
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200418 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200419 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200420 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200421 if (evalarg->eval_ga.ga_len == 1)
422 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200423 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200424 ga_clear(gap);
425 gap->ga_itemsize = 0;
426 }
427 else
428 {
429 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200430 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200431
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200432 // Line breaks encountered, concatenate all the lines.
433 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200434 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200435
436 // free the lines only when using getsourceline()
437 if (evalarg->eval_cookie != NULL)
438 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200439 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200440 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200441 // Do not free the last line, "arg" points into it, free it
442 // later.
443 vim_free(evalarg->eval_tofree);
444 evalarg->eval_tofree =
445 ((char_u **)gap->ga_data)[gap->ga_len - 1];
446 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200447 ga_clear_strings(gap);
448 }
449 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200450 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200451 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200452
453 // free lines that were explicitly marked for freeing
454 ga_clear_strings(freegap);
455 }
456
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200457 gap->ga_itemsize = 0;
458 if (p == NULL)
459 return FAIL;
460 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200461 vim_free(evalarg->eval_tofree_lambda);
462 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200463 // Compute "end" relative to the end.
464 *end = *start + STRLEN(*start) - endoff;
465 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200466 }
467
468 return res;
469}
470
471/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100472 * Convert "tv" to a string.
473 * When "convert" is TRUE convert a List into a sequence of lines and convert
474 * a Float to a String.
475 * Returns an allocated string (NULL when out of memory).
476 */
477 char_u *
478typval2string(typval_T *tv, int convert)
479{
480 garray_T ga;
481 char_u *retval;
482#ifdef FEAT_FLOAT
483 char_u numbuf[NUMBUFLEN];
484#endif
485
486 if (convert && tv->v_type == VAR_LIST)
487 {
488 ga_init2(&ga, (int)sizeof(char), 80);
489 if (tv->vval.v_list != NULL)
490 {
491 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
492 if (tv->vval.v_list->lv_len > 0)
493 ga_append(&ga, NL);
494 }
495 ga_append(&ga, NUL);
496 retval = (char_u *)ga.ga_data;
497 }
498#ifdef FEAT_FLOAT
499 else if (convert && tv->v_type == VAR_FLOAT)
500 {
501 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
502 retval = vim_strsave(numbuf);
503 }
504#endif
505 else
506 retval = vim_strsave(tv_get_string(tv));
507 return retval;
508}
509
510/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200511 * Top level evaluation function, returning a string. Does not handle line
512 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000513 * When "convert" is TRUE convert a List into a sequence of lines and convert
514 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 * Return pointer to allocated memory, or NULL for failure.
516 */
517 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100518eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100519 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100520 int convert,
521 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522{
Bram Moolenaar33570922005-01-25 22:26:29 +0000523 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100525 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100527 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
528 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 retval = NULL;
530 else
531 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100532 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000533 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100535 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536
537 return retval;
538}
539
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100540 char_u *
541eval_to_string(
542 char_u *arg,
543 int convert)
544{
545 return eval_to_string_eap(arg, convert, NULL);
546}
547
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000549 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200550 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200551 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 */
553 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100554eval_to_string_safe(
555 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100556 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557{
558 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200559 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200560 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200562 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200563 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000564 if (use_sandbox)
565 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200566 ++textwinlock;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200567 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000568 if (use_sandbox)
569 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200570 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200571 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200572 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 return retval;
574}
575
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576/*
577 * Top level evaluation function, returning a number.
578 * Evaluates "expr" silently.
579 * Returns -1 for an error.
580 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200581 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100582eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583{
Bram Moolenaar33570922005-01-25 22:26:29 +0000584 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200585 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000586 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587
588 ++emsg_off;
589
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200590 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 retval = -1;
592 else
593 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100594 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000595 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 }
597 --emsg_off;
598
599 return retval;
600}
601
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000602/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000603 * Top level evaluation function.
604 * Returns an allocated typval_T with the result.
605 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000606 */
607 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200608eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000609{
610 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200611 evalarg_T evalarg;
612
613 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000614
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200615 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200616 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100617 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000618
Bram Moolenaar37c83712020-06-30 21:18:36 +0200619 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000620 return tv;
621}
622
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100624 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200625 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
626 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000627 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200629 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100630call_vim_function(
631 char_u *func,
632 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200633 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200634 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000636 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200637 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100639 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200640 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200641 funcexe.firstline = curwin->w_cursor.lnum;
642 funcexe.lastline = curwin->w_cursor.lnum;
643 funcexe.evaluate = TRUE;
644 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000645 if (ret == FAIL)
646 clear_tv(rettv);
647
648 return ret;
649}
650
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100651/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100652 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100653 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200654 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
655 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100656 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200657 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100658call_func_retnr(
659 char_u *func,
660 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200661 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100662{
663 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200664 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100665
Bram Moolenaarded27a12018-08-01 19:06:03 +0200666 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100667 return -1;
668
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100669 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100670 clear_tv(&rettv);
671 return retval;
672}
673
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000674/*
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100675 * Call Vim script function like call_func_retnr() and drop the result.
676 * Returns FAIL when calling the function fails.
677 */
678 int
679call_func_noret(
680 char_u *func,
681 int argc,
682 typval_T *argv)
683{
684 typval_T rettv;
685
686 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
687 return FAIL;
688 clear_tv(&rettv);
689 return OK;
690}
691
692/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100693 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100694 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000695 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000696 */
697 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100698call_func_retstr(
699 char_u *func,
700 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200701 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000702{
703 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000704 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000705
Bram Moolenaarded27a12018-08-01 19:06:03 +0200706 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000707 return NULL;
708
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100709 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000710 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 return retval;
712}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000713
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000714/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100715 * Call Vim script function "func" and return the result as a List.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100716 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000717 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000718 */
719 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100720call_func_retlist(
721 char_u *func,
722 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200723 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000724{
725 typval_T rettv;
726
Bram Moolenaarded27a12018-08-01 19:06:03 +0200727 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000728 return NULL;
729
730 if (rettv.v_type != VAR_LIST)
731 {
732 clear_tv(&rettv);
733 return NULL;
734 }
735
736 return rettv.vval.v_list;
737}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739#ifdef FEAT_FOLDING
740/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100741 * Evaluate "arg", which is 'foldexpr'.
742 * Note: caller must set "curwin" to match "arg".
743 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
744 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 */
746 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100747eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748{
Bram Moolenaar33570922005-01-25 22:26:29 +0000749 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200750 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000752 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
753 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754
755 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000756 if (use_sandbox)
757 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200758 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200760 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 retval = 0;
762 else
763 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100764 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000765 if (tv.v_type == VAR_NUMBER)
766 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000767 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 retval = 0;
769 else
770 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100771 // If the result is a string, check if there is a non-digit before
772 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000773 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 if (!VIM_ISDIGIT(*s) && *s != '-')
775 *cp = *s++;
776 retval = atol((char *)s);
777 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000778 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 }
780 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000781 if (use_sandbox)
782 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200783 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200784 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200786 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787}
788#endif
789
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000791 * Get an lval: variable, Dict item or List item that can be assigned a value
792 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
793 * "name.key", "name.key[expr]" etc.
794 * Indexing only works if "name" is an existing List or Dictionary.
795 * "name" points to the start of the name.
796 * If "rettv" is not NULL it points to the value to be assigned.
797 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
798 * wrong; must end in space or cmd separator.
799 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100800 * flags:
801 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100802 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100803 * GLV_NO_AUTOLOAD: do not use script autoloading
804 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000805 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000806 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000807 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000808 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200809 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100810get_lval(
811 char_u *name,
812 typval_T *rettv,
813 lval_T *lp,
814 int unlet,
815 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100816 int flags, // GLV_ values
817 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000818{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000819 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000820 char_u *expr_start, *expr_end;
821 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000822 dictitem_T *v;
823 typval_T var1;
824 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000825 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000826 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000827 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000828 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100829 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100830 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100831 int writing;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000832
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100833 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200834 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000835
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100836 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000837 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100838 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000839 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200840 lp->ll_name_end = find_name_end(name, NULL, NULL,
841 FNE_INCL_BR | fne_flags);
842 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000843 }
844
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100845 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000846 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100847 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000848 if (expr_start != NULL)
849 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100850 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100851 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000852 && *p != '[' && *p != '.')
853 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200854 semsg(_(e_trailing_arg), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000855 return NULL;
856 }
857
858 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
859 if (lp->ll_exp_name == NULL)
860 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100861 // Report an invalid expression in braces, unless the
862 // expression evaluation has been cancelled due to an
863 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000864 if (!aborting() && !quiet)
865 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000866 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100867 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000868 return NULL;
869 }
870 }
871 lp->ll_name = lp->ll_exp_name;
872 }
873 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100874 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000875 lp->ll_name = name;
876
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200877 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100878 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200879 // "a: type" is declaring variable "a" with a type, not "a:".
880 if (p == name + 2 && p[-1] == ':')
881 {
882 --p;
883 lp->ll_name_end = p;
884 }
885 if (*p == ':')
886 {
887 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
888 char_u *tp = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100889
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200890 // parse the type after the name
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100891 lp->ll_type = parse_type(&tp, &si->sn_type_list, !quiet);
892 if (lp->ll_type == NULL && !quiet)
893 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200894 lp->ll_name_end = tp;
895 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100896 }
897 }
898
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100899 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000900 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
901 return p;
902
903 cc = *p;
904 *p = NUL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100905 // When we would write to the variable pass &ht and prevent autoload.
906 writing = !(flags & GLV_READ_ONLY);
907 v = find_var(lp->ll_name, writing ? &ht : NULL,
908 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000909 if (v == NULL && !quiet)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200910 semsg(_(e_undefined_variable_str), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000911 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000912 if (v == NULL)
913 return NULL;
914
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +0100915 if (in_vim9script() && (flags & GLV_NO_DECL) == 0)
916 {
917 if (!quiet)
918 semsg(_(e_variable_already_declared), lp->ll_name);
919 return NULL;
920 }
921
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000922 /*
923 * Loop until no more [idx] or .key is following.
924 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000925 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100926 var1.v_type = VAR_UNKNOWN;
927 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +0200928 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000929 {
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +0200930 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
931 {
932 if (!quiet)
933 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
934 return NULL;
935 }
Bram Moolenaare65081d2021-06-27 15:04:05 +0200936 if (lp->ll_tv->v_type != VAR_LIST
937 && lp->ll_tv->v_type != VAR_DICT
938 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000939 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000940 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100941 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000942 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000943 }
Bram Moolenaare65081d2021-06-27 15:04:05 +0200944
945 // a NULL list/blob works like an empty list/blob, allocate one now.
946 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
947 rettv_list_alloc(lp->ll_tv);
948 else if (lp->ll_tv->v_type == VAR_BLOB
949 && lp->ll_tv->vval.v_blob == NULL)
950 rettv_blob_alloc(lp->ll_tv);
951
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000952 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000953 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000954 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100955 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000956 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000957 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000958
Bram Moolenaar10c65862020-10-08 21:16:42 +0200959 if (in_vim9script() && lp->ll_valtype == NULL
960 && lp->ll_tv == &v->di_tv
961 && ht != NULL && ht == get_script_local_ht())
962 {
Bram Moolenaarc967d572021-07-08 21:38:50 +0200963 svar_T *sv = find_typval_in_script(lp->ll_tv);
Bram Moolenaar10c65862020-10-08 21:16:42 +0200964
965 // Vim9 script local variable: get the type
966 if (sv != NULL)
967 lp->ll_valtype = sv->sv_type;
968 }
969
Bram Moolenaar8c711452005-01-14 21:53:12 +0000970 len = -1;
971 if (*p == '.')
972 {
973 key = p + 1;
974 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
975 ;
976 if (len == 0)
977 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000978 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100979 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000980 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000981 }
982 p = key + len;
983 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000984 else
985 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100986 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000987 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000988 if (*p == ':')
989 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000990 else
991 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000992 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200993 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000994 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100995 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000996 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100997 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000998 clear_tv(&var1);
999 return NULL;
1000 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001001 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001002 }
1003
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001004 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001005 if (*p == ':')
1006 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001007 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001008 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001009 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001010 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001011 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001012 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001013 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001014 if (rettv != NULL
1015 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001016 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001017 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001018 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001019 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001020 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001021 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001022 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001023 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001024 }
1025 p = skipwhite(p + 1);
1026 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001027 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001028 else
1029 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001030 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001031 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001032 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001033 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001034 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001035 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001036 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001037 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001038 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001039 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001040 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001041 clear_tv(&var2);
1042 return NULL;
1043 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001044 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001045 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001046 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001047 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001048 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001049
Bram Moolenaar8c711452005-01-14 21:53:12 +00001050 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001051 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001052 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001053 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001054 clear_tv(&var1);
1055 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001056 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001057 }
1058
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001059 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001060 ++p;
1061 }
1062
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001063 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001064 {
1065 if (len == -1)
1066 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001067 // "[key]": get key from "var1"
1068 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001069 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001070 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001071 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001072 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001073 }
1074 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001075 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001076
1077 // a NULL dict is equivalent with an empty dict
1078 if (lp->ll_tv->vval.v_dict == NULL)
1079 {
1080 lp->ll_tv->vval.v_dict = dict_alloc();
1081 if (lp->ll_tv->vval.v_dict == NULL)
1082 {
1083 clear_tv(&var1);
1084 return NULL;
1085 }
1086 ++lp->ll_tv->vval.v_dict->dv_refcount;
1087 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001088 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001089
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001090 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001091
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001092 // When assigning to a scope dictionary check that a function and
1093 // variable name is valid (only variable name unless it is l: or
1094 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001095 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001096 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001097 int prevval;
1098 int wrong;
1099
1100 if (len != -1)
1101 {
1102 prevval = key[len];
1103 key[len] = NUL;
1104 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001105 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001106 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001107 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1108 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001109 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar03290b82020-12-19 16:30:44 +01001110 || !valid_varname(key, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001111 if (len != -1)
1112 key[len] = prevval;
1113 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001114 {
1115 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001116 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001117 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001118 }
1119
Bram Moolenaar10c65862020-10-08 21:16:42 +02001120 if (lp->ll_valtype != NULL)
1121 // use the type of the member
1122 lp->ll_valtype = lp->ll_valtype->tt_member;
1123
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001124 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001125 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001126 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001127 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001128 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001129 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001130 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001131 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001132 return NULL;
1133 }
1134
Bram Moolenaar31b81602019-02-10 22:14:27 +01001135 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001136 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001137 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001138 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001139 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001140 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001141 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001142 }
1143 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001144 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001145 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001146 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001147 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001148 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001149 p = NULL;
1150 break;
1151 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001152 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001153 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001154 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1155 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001156 {
1157 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001158 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001159 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001160
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001161 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001162 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001163 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001164 else if (lp->ll_tv->v_type == VAR_BLOB)
1165 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001166 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1167
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001168 /*
1169 * Get the number and item for the only or first index of the List.
1170 */
1171 if (empty1)
1172 lp->ll_n1 = 0;
1173 else
1174 // is number or string
1175 lp->ll_n1 = (long)tv_get_number(&var1);
1176 clear_tv(&var1);
1177
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001178 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001179 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001180 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001181 return NULL;
1182 }
1183 if (lp->ll_range && !lp->ll_empty2)
1184 {
1185 lp->ll_n2 = (long)tv_get_number(&var2);
1186 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001187 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1188 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001189 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001190 }
1191 lp->ll_blob = lp->ll_tv->vval.v_blob;
1192 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001193 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001194 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001195 else
1196 {
1197 /*
1198 * Get the number and item for the only or first index of the List.
1199 */
1200 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001201 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001202 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001203 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001204 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001205 clear_tv(&var1);
1206
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001207 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001208 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001209 lp->ll_li = list_find_index(lp->ll_list, &lp->ll_n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001210 if (lp->ll_li == NULL)
1211 {
Bram Moolenaare65081d2021-06-27 15:04:05 +02001212 // Vim9: Allow for adding an item at the end.
1213 if (in_vim9script() && lp->ll_n1 == lp->ll_list->lv_len
1214 && lp->ll_list->lv_lock == 0)
1215 {
1216 list_append_number(lp->ll_list, 0);
1217 lp->ll_li = list_find_index(lp->ll_list, &lp->ll_n1);
1218 }
1219 if (lp->ll_li == NULL)
1220 {
1221 clear_tv(&var2);
1222 if (!quiet)
1223 semsg(_(e_listidx), lp->ll_n1);
1224 return NULL;
1225 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001226 }
1227
Bram Moolenaar10c65862020-10-08 21:16:42 +02001228 if (lp->ll_valtype != NULL)
1229 // use the type of the member
1230 lp->ll_valtype = lp->ll_valtype->tt_member;
1231
Bram Moolenaar8c711452005-01-14 21:53:12 +00001232 /*
1233 * May need to find the item or absolute index for the second
1234 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001235 * When no index given: "lp->ll_empty2" is TRUE.
1236 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001237 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001238 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001239 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001240 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001241 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001242 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001243 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001244 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001245 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001246 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001247 {
1248 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001249 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001250 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001251 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001252 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001253 }
1254
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001255 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001256 if (lp->ll_n1 < 0)
1257 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1258 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001259 {
1260 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001261 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001262 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001263 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001264 }
1265
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001266 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001267 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001268 }
1269
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001270 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001271 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001272 return p;
1273}
1274
1275/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001276 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001277 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001278 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001279clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001280{
1281 vim_free(lp->ll_exp_name);
1282 vim_free(lp->ll_newkey);
1283}
1284
1285/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001286 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001287 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001288 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1289 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001290 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001291 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001292set_var_lval(
1293 lval_T *lp,
1294 char_u *endp,
1295 typval_T *rettv,
1296 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001297 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1298 char_u *op,
1299 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001300{
1301 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001302 listitem_T *ri;
1303 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001304
1305 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001306 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001307 cc = *endp;
1308 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001309 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1310 return;
1311
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001312 if (lp->ll_blob != NULL)
1313 {
1314 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001315
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001316 if (op != NULL && *op != '=')
1317 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001318 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001319 return;
1320 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001321 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001322 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001323
1324 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1325 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001326 if (lp->ll_empty2)
1327 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1328
Bram Moolenaar68452172021-04-12 21:21:02 +02001329 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1330 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001331 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001332 }
1333 else
1334 {
1335 val = (int)tv_get_number_chk(rettv, &error);
1336 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001337 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001338 }
1339 }
1340 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001341 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001342 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001343
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001344 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1345 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001346 {
1347 emsg(_(e_cannot_mod));
1348 *endp = cc;
1349 return;
1350 }
1351
Bram Moolenaarff697e62019-02-12 22:28:33 +01001352 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001353 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001354 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001355 &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001356 {
1357 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001358 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1359 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001360 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar24e93162021-07-18 20:40:33 +02001361 set_var_const(lp->ll_name, NULL, &tv, FALSE,
1362 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001363 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001364 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001365 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001366 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001367 {
1368 if (lp->ll_type != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001369 && check_typval_arg_type(lp->ll_type, rettv, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001370 return;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001371 set_var_const(lp->ll_name, lp->ll_type, rettv, copy,
1372 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001373 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001374 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001375 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001376 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001377 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001378 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001379 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001380 else if (lp->ll_range)
1381 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001382 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001383 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001384
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001385 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1386 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001387 {
1388 emsg(_("E996: Cannot lock a range"));
1389 return;
1390 }
1391
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001392 /*
1393 * Check whether any of the list items is locked
1394 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001395 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001396 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001397 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001398 return;
1399 ri = ri->li_next;
1400 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1401 break;
1402 ll_li = ll_li->li_next;
1403 ++ll_n1;
1404 }
1405
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001406 /*
1407 * Assign the List values to the list items.
1408 */
1409 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001410 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001411 if (op != NULL && *op != '=')
1412 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1413 else
1414 {
1415 clear_tv(&lp->ll_li->li_tv);
1416 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1417 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001418 ri = ri->li_next;
1419 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1420 break;
1421 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001422 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001423 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001424 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001425 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001426 ri = NULL;
1427 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001428 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001429 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001430 lp->ll_li = lp->ll_li->li_next;
1431 ++lp->ll_n1;
1432 }
1433 if (ri != NULL)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001434 emsg(_(e_list_value_has_more_items_than_targets));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001435 else if (lp->ll_empty2
1436 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001437 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001438 emsg(_(e_list_value_does_not_have_enough_items));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001439 }
1440 else
1441 {
1442 /*
1443 * Assign to a List or Dictionary item.
1444 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001445 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1446 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001447 {
1448 emsg(_("E996: Cannot lock a list or dict"));
1449 return;
1450 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001451
1452 if (lp->ll_valtype != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001453 && check_typval_arg_type(lp->ll_valtype, rettv, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001454 return;
1455
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001456 if (lp->ll_newkey != NULL)
1457 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001458 if (op != NULL && *op != '=')
1459 {
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02001460 semsg(_(e_dictkey), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001461 return;
1462 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001463 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1464 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001465 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001466
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001467 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001468 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001469 if (di == NULL)
1470 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001471 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1472 {
1473 vim_free(di);
1474 return;
1475 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001476 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001477 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001478 else if (op != NULL && *op != '=')
1479 {
1480 tv_op(lp->ll_tv, rettv, op);
1481 return;
1482 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001483 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001484 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001485
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001486 /*
1487 * Assign the value to the variable or list item.
1488 */
1489 if (copy)
1490 copy_tv(rettv, lp->ll_tv);
1491 else
1492 {
1493 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001494 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001495 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001496 }
1497 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001498}
1499
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001500/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001501 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1502 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001503 * Returns OK or FAIL.
1504 */
1505 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001506tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001507{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001508 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001509 char_u numbuf[NUMBUFLEN];
1510 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001511 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001512
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001513 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001514 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001515 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001516 {
1517 switch (tv1->v_type)
1518 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001519 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001520 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001521 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001522 case VAR_DICT:
1523 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001524 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001525 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001526 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001527 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001528 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001529 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001530 break;
1531
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001532 case VAR_BLOB:
1533 if (*op != '+' || tv2->v_type != VAR_BLOB)
1534 break;
1535 // BLOB += BLOB
1536 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1537 {
1538 blob_T *b1 = tv1->vval.v_blob;
1539 blob_T *b2 = tv2->vval.v_blob;
1540 int i, len = blob_len(b2);
1541 for (i = 0; i < len; i++)
1542 ga_append(&b1->bv_ga, blob_get(b2, i));
1543 }
1544 return OK;
1545
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001546 case VAR_LIST:
1547 if (*op != '+' || tv2->v_type != VAR_LIST)
1548 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001549 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001550 if (tv2->vval.v_list != NULL)
1551 {
1552 if (tv1->vval.v_list == NULL)
1553 {
1554 tv1->vval.v_list = tv2->vval.v_list;
1555 ++tv1->vval.v_list->lv_refcount;
1556 }
1557 else
1558 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1559 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001560 return OK;
1561
1562 case VAR_NUMBER:
1563 case VAR_STRING:
1564 if (tv2->v_type == VAR_LIST)
1565 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001566 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001567 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001568 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001569 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001570#ifdef FEAT_FLOAT
1571 if (tv2->v_type == VAR_FLOAT)
1572 {
1573 float_T f = n;
1574
Bram Moolenaarff697e62019-02-12 22:28:33 +01001575 if (*op == '%')
1576 break;
1577 switch (*op)
1578 {
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 case '/': f /= tv2->vval.v_float; break;
1583 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001584 clear_tv(tv1);
1585 tv1->v_type = VAR_FLOAT;
1586 tv1->vval.v_float = f;
1587 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001588 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001589#endif
1590 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001591 switch (*op)
1592 {
1593 case '+': n += tv_get_number(tv2); break;
1594 case '-': n -= tv_get_number(tv2); break;
1595 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001596 case '/': n = num_divide(n, tv_get_number(tv2),
1597 &failed); break;
1598 case '%': n = num_modulus(n, tv_get_number(tv2),
1599 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001600 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001601 clear_tv(tv1);
1602 tv1->v_type = VAR_NUMBER;
1603 tv1->vval.v_number = n;
1604 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001605 }
1606 else
1607 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001608 if (tv2->v_type == VAR_FLOAT)
1609 break;
1610
Bram Moolenaarff697e62019-02-12 22:28:33 +01001611 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001612 s = tv_get_string(tv1);
1613 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001614 clear_tv(tv1);
1615 tv1->v_type = VAR_STRING;
1616 tv1->vval.v_string = s;
1617 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001618 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001619
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001620 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001621#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001622 {
1623 float_T f;
1624
Bram Moolenaarff697e62019-02-12 22:28:33 +01001625 if (*op == '%' || *op == '.'
1626 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001627 && tv2->v_type != VAR_NUMBER
1628 && tv2->v_type != VAR_STRING))
1629 break;
1630 if (tv2->v_type == VAR_FLOAT)
1631 f = tv2->vval.v_float;
1632 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001633 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001634 switch (*op)
1635 {
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 case '/': tv1->vval.v_float /= f; break;
1640 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001641 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001642#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001643 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001644 }
1645 }
1646
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001647 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001648 return FAIL;
1649}
1650
1651/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001652 * Evaluate the expression used in a ":for var in expr" command.
1653 * "arg" points to "var".
1654 * Set "*errp" to TRUE for an error, FALSE otherwise;
1655 * Return a pointer that holds the info. Null when there is an error.
1656 */
1657 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001658eval_for_line(
1659 char_u *arg,
1660 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001661 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001662 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001663{
Bram Moolenaar33570922005-01-25 22:26:29 +00001664 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001665 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001666 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001667 typval_T tv;
1668 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001669 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001670
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001671 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001672
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001673 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001674 if (fi == NULL)
1675 return NULL;
1676
Bram Moolenaar404557e2021-07-05 21:41:48 +02001677 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1678 &fi->fi_semicolon, FALSE);
1679 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001680 return fi;
1681
Bram Moolenaar404557e2021-07-05 21:41:48 +02001682 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001683 if (expr[0] != 'i' || expr[1] != 'n'
1684 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001685 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001686 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1687 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1688 else
1689 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001690 return fi;
1691 }
1692
1693 if (skip)
1694 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001695 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1696 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001697 {
1698 *errp = FALSE;
1699 if (!skip)
1700 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001701 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001702 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001703 l = tv.vval.v_list;
1704 if (l == NULL)
1705 {
1706 // a null list is like an empty list: do nothing
1707 clear_tv(&tv);
1708 }
1709 else
1710 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001711 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001712 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001713
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001714 // No need to increment the refcount, it's already set for
1715 // the list being used in "tv".
1716 fi->fi_list = l;
1717 list_add_watch(l, &fi->fi_lw);
1718 fi->fi_lw.lw_item = l->lv_first;
1719 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001720 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001721 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001722 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001723 fi->fi_bi = 0;
1724 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001725 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001726 typval_T btv;
1727
1728 // Make a copy, so that the iteration still works when the
1729 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001730 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001731 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001732 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001733 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001734 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001735 else if (tv.v_type == VAR_STRING)
1736 {
1737 fi->fi_byte_idx = 0;
1738 fi->fi_string = tv.vval.v_string;
1739 tv.vval.v_string = NULL;
1740 if (fi->fi_string == NULL)
1741 fi->fi_string = vim_strsave((char_u *)"");
1742 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001743 else
1744 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001745 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001746 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001747 }
1748 }
1749 }
1750 if (skip)
1751 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001752 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001753
1754 return fi;
1755}
1756
1757/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001758 * Used when looping over a :for line, skip the "in expr" part.
1759 */
1760 void
1761skip_for_lines(void *fi_void, evalarg_T *evalarg)
1762{
1763 forinfo_T *fi = (forinfo_T *)fi_void;
1764 int i;
1765
1766 for (i = 0; i < fi->fi_break_count; ++i)
1767 eval_next_line(evalarg);
1768}
1769
1770/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001771 * Use the first item in a ":for" list. Advance to the next.
1772 * Assign the values to the variable (list). "arg" points to the first one.
1773 * Return TRUE when a valid item was found, FALSE when at end of list or
1774 * something wrong.
1775 */
1776 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001777next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001778{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001779 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001780 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001781 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001782 ? (ASSIGN_FINAL
1783 // first round: error if variable exists
1784 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
1785 | ASSIGN_NO_MEMBER_TYPE)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001786 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001787 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001788 int skip_assign = in_vim9script() && arg[0] == '_'
1789 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001790
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001791 if (fi->fi_blob != NULL)
1792 {
1793 typval_T tv;
1794
1795 if (fi->fi_bi >= blob_len(fi->fi_blob))
1796 return FALSE;
1797 tv.v_type = VAR_NUMBER;
1798 tv.v_lock = VAR_FIXED;
1799 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1800 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001801 if (skip_assign)
1802 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001803 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001804 fi->fi_varcount, flag, NULL) == OK;
1805 }
1806
1807 if (fi->fi_string != NULL)
1808 {
1809 typval_T tv;
1810 int len;
1811
1812 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1813 if (len == 0)
1814 return FALSE;
1815 tv.v_type = VAR_STRING;
1816 tv.v_lock = VAR_FIXED;
1817 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1818 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001819 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001820 if (skip_assign)
1821 result = TRUE;
1822 else
1823 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001824 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001825 vim_free(tv.vval.v_string);
1826 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001827 }
1828
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001829 item = fi->fi_lw.lw_item;
1830 if (item == NULL)
1831 result = FALSE;
1832 else
1833 {
1834 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001835 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001836 if (skip_assign)
1837 result = TRUE;
1838 else
1839 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001840 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001841 }
1842 return result;
1843}
1844
1845/*
1846 * Free the structure used to store info used by ":for".
1847 */
1848 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001849free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001850{
Bram Moolenaar33570922005-01-25 22:26:29 +00001851 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001852
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001853 if (fi == NULL)
1854 return;
1855 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001856 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001857 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001858 list_unref(fi->fi_list);
1859 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001860 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001861 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001862 else
1863 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001864 vim_free(fi);
1865}
1866
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001868set_context_for_expression(
1869 expand_T *xp,
1870 char_u *arg,
1871 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001873 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001875 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001877 if (cmdidx == CMD_let || cmdidx == CMD_var
1878 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001879 {
1880 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001881 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001882 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001883 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001884 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001885 {
1886 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001887 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001888 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001889 break;
1890 }
1891 return;
1892 }
1893 }
1894 else
1895 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1896 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 while ((xp->xp_pattern = vim_strpbrk(arg,
1898 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1899 {
1900 c = *xp->xp_pattern;
1901 if (c == '&')
1902 {
1903 c = xp->xp_pattern[1];
1904 if (c == '&')
1905 {
1906 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001907 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 }
1909 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001910 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001912 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1913 xp->xp_pattern += 2;
1914
1915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 }
1917 else if (c == '$')
1918 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001919 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 xp->xp_context = EXPAND_ENV_VARS;
1921 }
1922 else if (c == '=')
1923 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001924 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 xp->xp_context = EXPAND_EXPRESSION;
1926 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001927 else if (c == '#'
1928 && xp->xp_context == EXPAND_EXPRESSION)
1929 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001930 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001931 break;
1932 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001933 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 && xp->xp_context == EXPAND_FUNCTIONS
1935 && vim_strchr(xp->xp_pattern, '(') == NULL)
1936 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001937 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 break;
1939 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001940 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001942 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 {
1944 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1945 if (c == '\\' && xp->xp_pattern[1] != NUL)
1946 ++xp->xp_pattern;
1947 xp->xp_context = EXPAND_NOTHING;
1948 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001949 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001951 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1953 /* skip */ ;
1954 xp->xp_context = EXPAND_NOTHING;
1955 }
1956 else if (c == '|')
1957 {
1958 if (xp->xp_pattern[1] == '|')
1959 {
1960 ++xp->xp_pattern;
1961 xp->xp_context = EXPAND_EXPRESSION;
1962 }
1963 else
1964 xp->xp_context = EXPAND_COMMANDS;
1965 }
1966 else
1967 xp->xp_context = EXPAND_EXPRESSION;
1968 }
1969 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001970 // Doesn't look like something valid, expand as an expression
1971 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001972 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 arg = xp->xp_pattern;
1974 if (*arg != NUL)
1975 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1976 /* skip */ ;
1977 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01001978
1979 // ":exe one two" completes "two"
1980 if ((cmdidx == CMD_execute
1981 || cmdidx == CMD_echo
1982 || cmdidx == CMD_echon
1983 || cmdidx == CMD_echomsg)
1984 && xp->xp_context == EXPAND_EXPRESSION)
1985 {
1986 for (;;)
1987 {
1988 char_u *n = skiptowhite(arg);
1989
1990 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
1991 break;
1992 arg = skipwhite(n);
1993 }
1994 }
1995
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 xp->xp_pattern = arg;
1997}
1998
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002000 * Return TRUE if "pat" matches "text".
2001 * Does not use 'cpo' and always uses 'magic'.
2002 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002003 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002004pattern_match(char_u *pat, char_u *text, int ic)
2005{
2006 int matches = FALSE;
2007 char_u *save_cpo;
2008 regmatch_T regmatch;
2009
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002010 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002011 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002012 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002013 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2014 if (regmatch.regprog != NULL)
2015 {
2016 regmatch.rm_ic = ic;
2017 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2018 vim_regfree(regmatch.regprog);
2019 }
2020 p_cpo = save_cpo;
2021 return matches;
2022}
2023
2024/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002025 * Handle a name followed by "(". Both for just "name(arg)" and for
2026 * "expr->name(arg)".
2027 * Returns OK or FAIL.
2028 */
2029 static int
2030eval_func(
2031 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002032 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002033 char_u *name,
2034 int name_len,
2035 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002036 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002037 typval_T *basetv) // "expr" for "expr->name(arg)"
2038{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002039 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002040 char_u *s = name;
2041 int len = name_len;
2042 partial_T *partial;
2043 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002044 type_T *type = NULL;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002045
2046 if (!evaluate)
2047 check_vars(s, len);
2048
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002049 // If "s" is the name of a variable of type VAR_FUNC
2050 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002051 s = deref_func_name(s, &len, &partial,
2052 in_vim9script() ? &type : NULL, !evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002053
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002054 // Need to make a copy, in case evaluating the arguments makes
2055 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002056 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002057 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002058 ret = FAIL;
2059 else
2060 {
2061 funcexe_T funcexe;
2062
2063 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002064 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002065 funcexe.firstline = curwin->w_cursor.lnum;
2066 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002067 funcexe.evaluate = evaluate;
2068 funcexe.partial = partial;
2069 funcexe.basetv = basetv;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002070 funcexe.check_type = type;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002071 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002072 }
2073 vim_free(s);
2074
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002075 // If evaluate is FALSE rettv->v_type was not set in
2076 // get_func_tv, but it's needed in handle_subscript() to parse
2077 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002078 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2079 {
2080 rettv->vval.v_string = NULL;
2081 rettv->v_type = VAR_FUNC;
2082 }
2083
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002084 // Stop the expression evaluation when immediately
2085 // aborting on error, or when an interrupt occurred or
2086 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002087 if (evaluate && aborting())
2088 {
2089 if (ret == OK)
2090 clear_tv(rettv);
2091 ret = FAIL;
2092 }
2093 return ret;
2094}
2095
2096/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002097 * Get the next line source line without advancing. But do skip over comment
2098 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002099 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002100 */
2101 static char_u *
2102getline_peek_skip_comments(evalarg_T *evalarg)
2103{
2104 for (;;)
2105 {
2106 char_u *next = getline_peek(evalarg->eval_getline,
2107 evalarg->eval_cookie);
2108 char_u *p;
2109
2110 if (next == NULL)
2111 break;
2112 p = skipwhite(next);
2113 if (*p != NUL && !vim9_comment_start(p))
2114 return next;
2115 (void)eval_next_line(evalarg);
2116 }
2117 return NULL;
2118}
2119
2120/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002121 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2122 * comment) and there is a next line, return the next line (skipping blanks)
2123 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002124 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2125 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002126 * "arg" must point somewhere inside a line, not at the start.
2127 */
Yegappan Lakshmanana2438132021-07-10 21:29:18 +02002128 static char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002129eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2130{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002131 char_u *p = skipwhite(arg);
2132
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002133 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002134 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002135 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002136 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002137 && (*p == NUL || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002138 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002139 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002140
2141 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002142 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002143 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002144 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002145
Bram Moolenaar9d489562020-07-30 20:08:50 +02002146 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002147 {
2148 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002149 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002150 }
2151 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002152 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002153}
2154
2155/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002156 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002157 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002158 */
Yegappan Lakshmanana2438132021-07-10 21:29:18 +02002159 static char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002160eval_next_line(evalarg_T *evalarg)
2161{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002162 garray_T *gap = &evalarg->eval_ga;
2163 char_u *line;
2164
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002165 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002166 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2167 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002168 else
2169 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002170 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002171 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2172 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002173 char_u *p = skipwhite(line);
2174
2175 // Going to concatenate the lines after parsing. For an empty or
2176 // comment line use an empty string.
2177 if (*p == NUL || vim9_comment_start(p))
2178 {
2179 vim_free(line);
2180 line = vim_strsave((char_u *)"");
2181 }
2182
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002183 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2184 ++gap->ga_len;
2185 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002186 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002187 {
2188 vim_free(evalarg->eval_tofree);
2189 evalarg->eval_tofree = line;
2190 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002191
2192 // Advanced to the next line, "arg" no longer points into the previous
2193 // line.
2194 VIM_CLEAR(evalarg->eval_tofree_cmdline);
2195
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002196 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002197}
2198
Bram Moolenaare6b53242020-07-01 17:28:33 +02002199/*
2200 * Call eval_next_non_blank() and get the next line if needed.
2201 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002202 char_u *
2203skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2204{
2205 int getnext;
2206 char_u *p = skipwhite(arg);
2207
Bram Moolenaar962d7212020-07-04 14:15:00 +02002208 if (evalarg == NULL)
2209 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002210 eval_next_non_blank(p, evalarg, &getnext);
2211 if (getnext)
2212 return eval_next_line(evalarg);
2213 return p;
2214}
2215
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002216/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002217 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002218 */
2219 void
2220clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2221{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002222 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002223 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002224 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002225 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002226 if (eap != NULL)
2227 {
2228 // We may need to keep the original command line, e.g. for
2229 // ":let" it has the variable names. But we may also need the
2230 // new one, "nextcmd" points into it. Keep both.
2231 vim_free(eap->cmdline_tofree);
2232 eap->cmdline_tofree = *eap->cmdlinep;
2233 *eap->cmdlinep = evalarg->eval_tofree;
2234 }
2235 else
2236 vim_free(evalarg->eval_tofree);
2237 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002238 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002239
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002240 VIM_CLEAR(evalarg->eval_tofree_cmdline);
2241 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002242 }
2243}
2244
2245/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002247 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2249 */
2250
2251/*
2252 * Handle zero level expression.
2253 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002255 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002256 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 * Return OK or FAIL.
2258 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002259 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002260eval0(
2261 char_u *arg,
2262 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002263 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002264 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265{
2266 int ret;
2267 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002268 int did_emsg_before = did_emsg;
2269 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002270 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002271 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272
2273 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002274 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002275 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002276
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002277 if (ret != FAIL)
2278 end_error = !ends_excmd2(arg, p);
2279 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 {
2281 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002282 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 /*
2284 * Report the invalid expression unless the expression evaluation has
2285 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002286 * exception, or we already gave a more specific error.
2287 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002289 if (!aborting()
2290 && did_emsg == did_emsg_before
2291 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002292 && (flags & EVAL_CONSTANT) == 0
2293 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002294 {
2295 if (end_error)
2296 semsg(_(e_trailing_arg), p);
2297 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002298 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002299 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002300
2301 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002302 // a next command to avoid more errors, unless "|" is following, which
2303 // could only be a command separator.
2304 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2305 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002306 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002308
2309 if (eap != NULL)
2310 eap->nextcmd = check_nextcmd(p);
2311
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 return ret;
2313}
2314
2315/*
2316 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002317 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002318 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 *
2320 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002321 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002323 * Note: "rettv.v_lock" is not set.
2324 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 * Return OK or FAIL.
2326 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002327 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002328eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002330 char_u *p;
2331 int getnext;
2332
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002333 CLEAR_POINTER(rettv);
2334
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 /*
2336 * Get the first variable.
2337 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002338 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 return FAIL;
2340
Bram Moolenaar793648f2020-06-26 21:28:25 +02002341 p = eval_next_non_blank(*arg, evalarg, &getnext);
2342 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002344 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002345 int result;
2346 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002347 evalarg_T *evalarg_used = evalarg;
2348 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002349 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002350 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002351 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002352
2353 if (evalarg == NULL)
2354 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002355 CLEAR_FIELD(local_evalarg);
2356 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002357 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002358 orig_flags = evalarg_used->eval_flags;
2359 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002360
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002361 if (getnext)
2362 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002363 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002364 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002365 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002366 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002367 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002368 clear_tv(rettv);
2369 return FAIL;
2370 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002371 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002372 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002373
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002375 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002377 int error = FALSE;
2378
Bram Moolenaar13106602020-10-04 16:06:05 +02002379 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002380 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002381 else if (vim9script)
2382 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002383 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002385 if (error || !op_falsy || !result)
2386 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002387 if (error)
2388 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 }
2390
2391 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002392 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002394 if (op_falsy)
2395 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002396 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002397 {
mityu4ac198c2021-05-28 17:52:40 +02002398 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002399 clear_tv(rettv);
2400 return FAIL;
2401 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002402 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002403 evalarg_used->eval_flags = (op_falsy ? !result : result)
2404 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2405 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002406 {
2407 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002409 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002410 if (!op_falsy || !result)
2411 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002413 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002415 /*
2416 * Check for the ":".
2417 */
2418 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2419 if (*p != ':')
2420 {
2421 emsg(_(e_missing_colon));
2422 if (evaluate && result)
2423 clear_tv(rettv);
2424 evalarg_used->eval_flags = orig_flags;
2425 return FAIL;
2426 }
2427 if (getnext)
2428 *arg = eval_next_line(evalarg_used);
2429 else
2430 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002431 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002432 {
2433 error_white_both(p, 1);
2434 clear_tv(rettv);
2435 evalarg_used->eval_flags = orig_flags;
2436 return FAIL;
2437 }
2438 *arg = p;
2439 }
2440
2441 /*
2442 * Get the third variable. Recursive!
2443 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002444 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002445 {
mityu4ac198c2021-05-28 17:52:40 +02002446 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002447 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002448 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002449 return FAIL;
2450 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002451 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2452 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002453 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002454 if (eval1(arg, &var2, evalarg_used) == FAIL)
2455 {
2456 if (evaluate && result)
2457 clear_tv(rettv);
2458 evalarg_used->eval_flags = orig_flags;
2459 return FAIL;
2460 }
2461 if (evaluate && !result)
2462 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002464
2465 if (evalarg == NULL)
2466 clear_evalarg(&local_evalarg, NULL);
2467 else
2468 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 }
2470
2471 return OK;
2472}
2473
2474/*
2475 * Handle first level expression:
2476 * expr2 || expr2 || expr2 logical OR
2477 *
2478 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002479 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 *
2481 * Return OK or FAIL.
2482 */
2483 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002484eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002486 char_u *p;
2487 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488
2489 /*
2490 * Get the first variable.
2491 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002492 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 return FAIL;
2494
2495 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002496 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002498 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002499 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002501 evalarg_T *evalarg_used = evalarg;
2502 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002503 int evaluate;
2504 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002505 long result = FALSE;
2506 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002507 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002508 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002509
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002510 if (evalarg == NULL)
2511 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002512 CLEAR_FIELD(local_evalarg);
2513 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002514 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002515 orig_flags = evalarg_used->eval_flags;
2516 evaluate = orig_flags & EVAL_EVALUATE;
2517 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002518 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002519 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002520 result = tv_get_bool_chk(rettv, &error);
2521 else if (tv_get_number_chk(rettv, &error) != 0)
2522 result = TRUE;
2523 clear_tv(rettv);
2524 if (error)
2525 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 }
2527
2528 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002529 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002531 while (p[0] == '|' && p[1] == '|')
2532 {
2533 if (getnext)
2534 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002535 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002536 {
2537 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2538 {
2539 error_white_both(p, 2);
2540 clear_tv(rettv);
2541 return FAIL;
2542 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002543 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002544 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002545
2546 /*
2547 * Get the second variable.
2548 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002549 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2550 {
mityu4ac198c2021-05-28 17:52:40 +02002551 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002552 clear_tv(rettv);
2553 return FAIL;
2554 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002555 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2556 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002557 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002558 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002559 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002560
2561 /*
2562 * Compute the result.
2563 */
2564 if (evaluate && !result)
2565 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002566 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002567 result = tv_get_bool_chk(&var2, &error);
2568 else if (tv_get_number_chk(&var2, &error) != 0)
2569 result = TRUE;
2570 clear_tv(&var2);
2571 if (error)
2572 return FAIL;
2573 }
2574 if (evaluate)
2575 {
2576 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002577 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002578 rettv->v_type = VAR_BOOL;
2579 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002580 }
2581 else
2582 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002583 rettv->v_type = VAR_NUMBER;
2584 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002585 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002586 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002587
2588 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002590
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002591 if (evalarg == NULL)
2592 clear_evalarg(&local_evalarg, NULL);
2593 else
2594 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 }
2596
2597 return OK;
2598}
2599
2600/*
2601 * Handle second level expression:
2602 * expr3 && expr3 && expr3 logical AND
2603 *
2604 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002605 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 *
2607 * Return OK or FAIL.
2608 */
2609 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002610eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002612 char_u *p;
2613 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614
2615 /*
2616 * Get the first variable.
2617 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002618 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 return FAIL;
2620
2621 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002622 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002624 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002625 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002627 evalarg_T *evalarg_used = evalarg;
2628 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002629 int orig_flags;
2630 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002631 long result = TRUE;
2632 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002633 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002634 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002635
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002636 if (evalarg == NULL)
2637 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002638 CLEAR_FIELD(local_evalarg);
2639 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002640 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002641 orig_flags = evalarg_used->eval_flags;
2642 evaluate = orig_flags & EVAL_EVALUATE;
2643 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002644 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002645 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002646 result = tv_get_bool_chk(rettv, &error);
2647 else if (tv_get_number_chk(rettv, &error) == 0)
2648 result = FALSE;
2649 clear_tv(rettv);
2650 if (error)
2651 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 }
2653
2654 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002655 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002657 while (p[0] == '&' && p[1] == '&')
2658 {
2659 if (getnext)
2660 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002661 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002662 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002663 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002664 {
2665 error_white_both(p, 2);
2666 clear_tv(rettv);
2667 return FAIL;
2668 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002669 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002670 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002671
2672 /*
2673 * Get the second variable.
2674 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002675 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2676 {
mityu4ac198c2021-05-28 17:52:40 +02002677 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002678 clear_tv(rettv);
2679 return FAIL;
2680 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002681 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2682 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002683 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002684 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002685 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002686 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002687
2688 /*
2689 * Compute the result.
2690 */
2691 if (evaluate && result)
2692 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002693 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002694 result = tv_get_bool_chk(&var2, &error);
2695 else if (tv_get_number_chk(&var2, &error) == 0)
2696 result = FALSE;
2697 clear_tv(&var2);
2698 if (error)
2699 return FAIL;
2700 }
2701 if (evaluate)
2702 {
2703 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002704 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002705 rettv->v_type = VAR_BOOL;
2706 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002707 }
2708 else
2709 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002710 rettv->v_type = VAR_NUMBER;
2711 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002712 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002713 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002714
2715 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002717
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002718 if (evalarg == NULL)
2719 clear_evalarg(&local_evalarg, NULL);
2720 else
2721 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 }
2723
2724 return OK;
2725}
2726
2727/*
2728 * Handle third level expression:
2729 * var1 == var2
2730 * var1 =~ var2
2731 * var1 != var2
2732 * var1 !~ var2
2733 * var1 > var2
2734 * var1 >= var2
2735 * var1 < var2
2736 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002737 * var1 is var2
2738 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 *
2740 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002741 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 *
2743 * Return OK or FAIL.
2744 */
2745 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002746eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002749 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002750 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002752 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753
2754 /*
2755 * Get the first variable.
2756 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002757 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 return FAIL;
2759
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002760 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002761 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762
2763 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002764 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002766 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002768 typval_T var2;
2769 int ic;
2770 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002771 int evaluate = evalarg == NULL
2772 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002773
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002774 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002775 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002776 *arg = eval_next_line(evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002777 p = *arg;
2778 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002779 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2780 {
mityu4ac198c2021-05-28 17:52:40 +02002781 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002782 clear_tv(rettv);
2783 return FAIL;
2784 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002785
Bram Moolenaar696ba232020-07-29 21:20:41 +02002786 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2787 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002788 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002789 clear_tv(rettv);
2790 return FAIL;
2791 }
2792
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002793 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 if (p[len] == '?')
2795 {
2796 ic = TRUE;
2797 ++len;
2798 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002799 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 else if (p[len] == '#')
2801 {
2802 ic = FALSE;
2803 ++len;
2804 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002805 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002807 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808
2809 /*
2810 * Get the second variable.
2811 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002812 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2813 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002814 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002815 clear_tv(rettv);
2816 return FAIL;
2817 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002818 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002819 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002821 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 return FAIL;
2823 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002824 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002825 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002826 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002827
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002828 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002829 {
2830 ret = FAIL;
2831 clear_tv(rettv);
2832 }
2833 else
2834 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002835 clear_tv(&var2);
2836 return ret;
2837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 }
2839
2840 return OK;
2841}
2842
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002843/*
2844 * Make a copy of blob "tv1" and append blob "tv2".
2845 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002846 void
2847eval_addblob(typval_T *tv1, typval_T *tv2)
2848{
2849 blob_T *b1 = tv1->vval.v_blob;
2850 blob_T *b2 = tv2->vval.v_blob;
2851 blob_T *b = blob_alloc();
2852 int i;
2853
2854 if (b != NULL)
2855 {
2856 for (i = 0; i < blob_len(b1); i++)
2857 ga_append(&b->bv_ga, blob_get(b1, i));
2858 for (i = 0; i < blob_len(b2); i++)
2859 ga_append(&b->bv_ga, blob_get(b2, i));
2860
2861 clear_tv(tv1);
2862 rettv_blob_set(tv1, b);
2863 }
2864}
2865
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002866/*
2867 * Make a copy of list "tv1" and append list "tv2".
2868 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002869 int
2870eval_addlist(typval_T *tv1, typval_T *tv2)
2871{
2872 typval_T var3;
2873
2874 // concatenate Lists
2875 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2876 {
2877 clear_tv(tv1);
2878 clear_tv(tv2);
2879 return FAIL;
2880 }
2881 clear_tv(tv1);
2882 *tv1 = var3;
2883 return OK;
2884}
2885
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886/*
2887 * Handle fourth level expression:
2888 * + number addition
2889 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002890 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002891 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 *
2893 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002894 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 *
2896 * Return OK or FAIL.
2897 */
2898 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002899eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 /*
2902 * Get the first variable.
2903 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002904 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 return FAIL;
2906
2907 /*
2908 * Repeat computing, until no '+', '-' or '.' is following.
2909 */
2910 for (;;)
2911 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002912 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002913 int getnext;
2914 char_u *p;
2915 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002916 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002917 int concat;
2918 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002919 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002920
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002921 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002922 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002923 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002924 p = eval_next_non_blank(*arg, evalarg, &getnext);
2925 op = *p;
2926 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002927 if ((op != '+' && op != '-' && !concat) || p[1] == '='
2928 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002930 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
2931 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002932
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002933 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002934 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002935 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002936 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002937 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002938 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002939 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002940 {
mityu4ac198c2021-05-28 17:52:40 +02002941 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002942 clear_tv(rettv);
2943 return FAIL;
2944 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002945 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002946 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002947 if ((op != '+' || (rettv->v_type != VAR_LIST
2948 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002949#ifdef FEAT_FLOAT
2950 && (op == '.' || rettv->v_type != VAR_FLOAT)
2951#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002952 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002953 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002954 int error = FALSE;
2955
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002956 // For "list + ...", an illegal use of the first operand as
2957 // a number cannot be determined before evaluating the 2nd
2958 // operand: if this is also a list, all is ok.
2959 // For "something . ...", "something - ..." or "non-list + ...",
2960 // we know that the first operand needs to be a string or number
2961 // without evaluating the 2nd operand. So check before to avoid
2962 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002963 if (op != '.')
2964 tv_get_number_chk(rettv, &error);
2965 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002966 {
2967 clear_tv(rettv);
2968 return FAIL;
2969 }
2970 }
2971
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 /*
2973 * Get the second variable.
2974 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002975 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002976 {
mityu89dcb4d2021-05-28 13:50:17 +02002977 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002978 clear_tv(rettv);
2979 return FAIL;
2980 }
2981 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002982 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002984 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 return FAIL;
2986 }
2987
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002988 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 {
2990 /*
2991 * Compute the result.
2992 */
2993 if (op == '.')
2994 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002995 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2996 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002997 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002998
Bram Moolenaar2e086612020-08-25 22:37:48 +02002999 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003000 || var2.v_type == VAR_CHANNEL
3001 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003002 semsg(_(e_using_invalid_value_as_string_str),
3003 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003004#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02003005 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003006 {
3007 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3008 var2.vval.v_float);
3009 s2 = buf2;
3010 }
3011#endif
3012 else
3013 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003014 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003015 {
3016 clear_tv(rettv);
3017 clear_tv(&var2);
3018 return FAIL;
3019 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003020 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003021 clear_tv(rettv);
3022 rettv->v_type = VAR_STRING;
3023 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003025 else if (op == '+' && rettv->v_type == VAR_BLOB
3026 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003027 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003028 else if (op == '+' && rettv->v_type == VAR_LIST
3029 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003030 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003031 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003032 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 else
3035 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003036 int error = FALSE;
3037 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003038#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003039 float_T f1 = 0, f2 = 0;
3040
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003041 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003042 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003043 f1 = rettv->vval.v_float;
3044 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003045 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003046 else
3047#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003048 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003049 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003050 if (error)
3051 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003052 // This can only happen for "list + non-list" or
3053 // "blob + non-blob". For "non-list + ..." or
3054 // "something - ...", we returned before evaluating the
3055 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003056 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003057 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003058 return FAIL;
3059 }
3060#ifdef FEAT_FLOAT
3061 if (var2.v_type == VAR_FLOAT)
3062 f1 = n1;
3063#endif
3064 }
3065#ifdef FEAT_FLOAT
3066 if (var2.v_type == VAR_FLOAT)
3067 {
3068 f2 = var2.vval.v_float;
3069 n2 = 0;
3070 }
3071 else
3072#endif
3073 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003074 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003075 if (error)
3076 {
3077 clear_tv(rettv);
3078 clear_tv(&var2);
3079 return FAIL;
3080 }
3081#ifdef FEAT_FLOAT
3082 if (rettv->v_type == VAR_FLOAT)
3083 f2 = n2;
3084#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003085 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003086 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003087
3088#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003089 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003090 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3091 {
3092 if (op == '+')
3093 f1 = f1 + f2;
3094 else
3095 f1 = f1 - f2;
3096 rettv->v_type = VAR_FLOAT;
3097 rettv->vval.v_float = f1;
3098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003100#endif
3101 {
3102 if (op == '+')
3103 n1 = n1 + n2;
3104 else
3105 n1 = n1 - n2;
3106 rettv->v_type = VAR_NUMBER;
3107 rettv->vval.v_number = n1;
3108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003110 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 }
3112 }
3113 return OK;
3114}
3115
3116/*
3117 * Handle fifth level expression:
3118 * * number multiplication
3119 * / number division
3120 * % number modulo
3121 *
3122 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003123 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 *
3125 * Return OK or FAIL.
3126 */
3127 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003128eval6(
3129 char_u **arg,
3130 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003131 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003132 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003134#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003135 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137
3138 /*
3139 * Get the first variable.
3140 */
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003141 if (eval7t(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 return FAIL;
3143
3144 /*
3145 * Repeat computing, until no '*', '/' or '%' is following.
3146 */
3147 for (;;)
3148 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003149 int evaluate;
3150 int getnext;
3151 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003152 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003153 int op;
3154 varnumber_T n1, n2;
3155#ifdef FEAT_FLOAT
3156 float_T f1, f2;
3157#endif
3158 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003159
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003160 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003161 p = eval_next_non_blank(*arg, evalarg, &getnext);
3162 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003163 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003165
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003166 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003167 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003168 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003169 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003170 {
3171 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3172 {
mityu4ac198c2021-05-28 17:52:40 +02003173 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003174 clear_tv(rettv);
3175 return FAIL;
3176 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003177 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003180#ifdef FEAT_FLOAT
3181 f1 = 0;
3182 f2 = 0;
3183#endif
3184 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003185 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003187#ifdef FEAT_FLOAT
3188 if (rettv->v_type == VAR_FLOAT)
3189 {
3190 f1 = rettv->vval.v_float;
3191 use_float = TRUE;
3192 n1 = 0;
3193 }
3194 else
3195#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003196 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003197 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003198 if (error)
3199 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 }
3201 else
3202 n1 = 0;
3203
3204 /*
3205 * Get the second variable.
3206 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003207 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3208 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003209 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003210 clear_tv(rettv);
3211 return FAIL;
3212 }
3213 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003214 if (eval7t(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 return FAIL;
3216
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003217 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003219#ifdef FEAT_FLOAT
3220 if (var2.v_type == VAR_FLOAT)
3221 {
3222 if (!use_float)
3223 {
3224 f1 = n1;
3225 use_float = TRUE;
3226 }
3227 f2 = var2.vval.v_float;
3228 n2 = 0;
3229 }
3230 else
3231#endif
3232 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003233 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003234 clear_tv(&var2);
3235 if (error)
3236 return FAIL;
3237#ifdef FEAT_FLOAT
3238 if (use_float)
3239 f2 = n2;
3240#endif
3241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242
3243 /*
3244 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003245 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003247#ifdef FEAT_FLOAT
3248 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003250 if (op == '*')
3251 f1 = f1 * f2;
3252 else if (op == '/')
3253 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003254# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003255 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003256 if (f2 == 0.0)
3257 {
3258 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003259 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003260 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003261 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003262 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003263 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003264 }
3265 else
3266 f1 = f1 / f2;
3267# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003268 // We rely on the floating point library to handle divide
3269 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003270 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003271# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003274 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003275 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003276 return FAIL;
3277 }
3278 rettv->v_type = VAR_FLOAT;
3279 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 }
3281 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003284 int failed = FALSE;
3285
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003286 if (op == '*')
3287 n1 = n1 * n2;
3288 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003289 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003291 n1 = num_modulus(n1, n2, &failed);
3292 if (failed)
3293 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003294
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003295 rettv->v_type = VAR_NUMBER;
3296 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 }
3299 }
3300
3301 return OK;
3302}
3303
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003304/*
3305 * Handle a type cast before a base level expression.
3306 * "arg" must point to the first non-white of the expression.
3307 * "arg" is advanced to just after the recognized expression.
3308 * Return OK or FAIL.
3309 */
3310 static int
3311eval7t(
3312 char_u **arg,
3313 typval_T *rettv,
3314 evalarg_T *evalarg,
3315 int want_string) // after "." operator
3316{
3317 type_T *want_type = NULL;
3318 garray_T type_list; // list of pointers to allocated types
3319 int res;
3320 int evaluate = evalarg == NULL ? 0
3321 : (evalarg->eval_flags & EVAL_EVALUATE);
3322
3323 // Recognize <type> in Vim9 script only.
3324 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1]))
3325 {
3326 ++*arg;
3327 ga_init2(&type_list, sizeof(type_T *), 10);
3328 want_type = parse_type(arg, &type_list, TRUE);
3329 if (want_type == NULL && (evaluate || **arg != '>'))
3330 {
3331 clear_type_list(&type_list);
3332 return FAIL;
3333 }
3334
3335 if (**arg != '>')
3336 {
3337 if (*skipwhite(*arg) == '>')
3338 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3339 else
3340 emsg(_(e_missing_gt));
3341 clear_type_list(&type_list);
3342 return FAIL;
3343 }
3344 ++*arg;
3345 *arg = skipwhite_and_linebreak(*arg, evalarg);
3346 }
3347
3348 res = eval7(arg, rettv, evalarg, want_string);
3349
3350 if (want_type != NULL && evaluate)
3351 {
3352 if (res == OK)
3353 {
3354 type_T *actual = typval2type(rettv, get_copyID(), &type_list, TRUE);
3355
3356 if (!equal_type(want_type, actual))
3357 {
3358 if (want_type == &t_bool && actual != &t_bool
3359 && (actual->tt_flags & TTFLAG_BOOL_OK))
3360 {
3361 int n = tv2bool(rettv);
3362
3363 // can use "0" and "1" for boolean in some places
3364 clear_tv(rettv);
3365 rettv->v_type = VAR_BOOL;
3366 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3367 }
3368 else
3369 {
3370 where_T where;
3371
3372 where.wt_index = 0;
3373 where.wt_variable = TRUE;
3374 res = check_type(want_type, actual, TRUE, where);
3375 }
3376 }
3377 }
3378 clear_type_list(&type_list);
3379 }
3380
3381 return res;
3382}
3383
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003384 int
3385eval_leader(char_u **arg, int vim9)
3386{
3387 char_u *s = *arg;
3388 char_u *p = *arg;
3389
3390 while (*p == '!' || *p == '-' || *p == '+')
3391 {
3392 char_u *n = skipwhite(p + 1);
3393
3394 // ++, --, -+ and +- are not accepted in Vim9 script
3395 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3396 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003397 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003398 return FAIL;
3399 }
3400 p = n;
3401 }
3402 *arg = p;
3403 return OK;
3404}
3405
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406/*
3407 * Handle sixth level expression:
3408 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003409 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003410 * "string" string constant
3411 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 * &option-name option value
3413 * @r register contents
3414 * identifier variable value
3415 * function() function call
3416 * $VAR environment variable
3417 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003418 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003419 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003420 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003421 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 *
3423 * Also handle:
3424 * ! in front logical NOT
3425 * - in front unary minus
3426 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003427 * trailing [] subscript in String or List
3428 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003429 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 *
3431 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003432 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 *
3434 * Return OK or FAIL.
3435 */
3436 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003437eval7(
3438 char_u **arg,
3439 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003440 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003441 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003443 int evaluate = evalarg != NULL
3444 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 int len;
3446 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 char_u *start_leader, *end_leader;
3448 int ret = OK;
3449 char_u *alias;
3450
3451 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003452 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003453 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003455 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456
3457 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003458 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 */
3460 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003461 if (eval_leader(arg, in_vim9script()) == FAIL)
3462 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 end_leader = *arg;
3464
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003465 if (**arg == '.' && (!isdigit(*(*arg + 1))
3466#ifdef FEAT_FLOAT
3467 || current_sctx.sc_version < 2
3468#endif
3469 ))
3470 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003471 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003472 ++*arg;
3473 return FAIL;
3474 }
3475
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 switch (**arg)
3477 {
3478 /*
3479 * Number constant.
3480 */
3481 case '0':
3482 case '1':
3483 case '2':
3484 case '3':
3485 case '4':
3486 case '5':
3487 case '6':
3488 case '7':
3489 case '8':
3490 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003491 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003492
3493 // Apply prefixed "-" and "+" now. Matters especially when
3494 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003495 if (ret == OK && evaluate && end_leader > start_leader
3496 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003497 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 break;
3499
3500 /*
3501 * String constant: "string".
3502 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003503 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 break;
3505
3506 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003507 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003509 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003510 break;
3511
3512 /*
3513 * List: [expr, expr]
3514 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003515 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 break;
3517
3518 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003519 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003520 */
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003521 case '#': if (in_vim9script())
3522 {
3523 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3524 }
3525 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003526 {
3527 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003528 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003529 }
3530 else
3531 ret = NOTDONE;
3532 break;
3533
3534 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003535 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003536 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003537 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003538 case '{': if (in_vim9script())
3539 ret = NOTDONE;
3540 else
3541 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003542 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003543 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003544 break;
3545
3546 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003547 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003549 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 break;
3551
3552 /*
3553 * Environment variable: $VAR.
3554 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003555 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 break;
3557
3558 /*
3559 * Register contents: @r.
3560 */
3561 case '@': ++*arg;
3562 if (evaluate)
3563 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003564 if (in_vim9script() && IS_WHITE_OR_NUL(**arg))
3565 semsg(_(e_syntax_error_at_str), *arg);
3566 else if (in_vim9script() && !valid_yank_reg(**arg, FALSE))
3567 emsg_invreg(**arg);
3568 else
3569 {
3570 rettv->v_type = VAR_STRING;
3571 rettv->vval.v_string = get_reg_contents(**arg,
3572 GREG_EXPR_SRC);
3573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 }
3575 if (**arg != NUL)
3576 ++*arg;
3577 break;
3578
3579 /*
3580 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003581 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003583 case '(': ret = NOTDONE;
3584 if (in_vim9script())
Bram Moolenaar06409502021-02-17 17:00:27 +01003585 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003586 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003587 if (ret == OK && evaluate)
3588 {
3589 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3590
Bram Moolenaara9931532021-06-12 15:58:16 +02003591 // Compile it here to get the return type. The return
3592 // type is optional, when it's missing use t_unknown.
3593 // This is recognized in compile_return().
3594 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3595 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003596 if (compile_def_function(ufunc,
Bram Moolenaare99d4222021-06-13 14:01:26 +02003597 FALSE, COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003598 {
3599 clear_tv(rettv);
3600 ret = FAIL;
3601 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003602 }
3603 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003604 if (ret == NOTDONE)
3605 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003606 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003607 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003608
Bram Moolenaar9215f012020-06-27 21:18:00 +02003609 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003610 if (**arg == ')')
3611 ++*arg;
3612 else if (ret == OK)
3613 {
3614 emsg(_(e_missing_close));
3615 clear_tv(rettv);
3616 ret = FAIL;
3617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 }
3619 break;
3620
Bram Moolenaar8c711452005-01-14 21:53:12 +00003621 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 break;
3623 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003624
3625 if (ret == NOTDONE)
3626 {
3627 /*
3628 * Must be a variable or function name.
3629 * Can also be a curly-braces kind of name: {expr}.
3630 */
3631 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003632 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003633 if (alias != NULL)
3634 s = alias;
3635
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003636 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003637 ret = FAIL;
3638 else
3639 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003640 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3641
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003642 if (evaluate && in_vim9script() && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003643 {
3644 emsg(_(e_cannot_use_underscore_here));
3645 ret = FAIL;
3646 }
3647 else if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003648 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003649 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003650 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003651 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003652 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003653 else if (flags & EVAL_CONSTANT)
3654 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003655 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003656 {
3657 // get the value of "true", "false" or a variable
3658 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3659 {
3660 rettv->v_type = VAR_BOOL;
3661 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003662 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003663 }
3664 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003665 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003666 {
3667 rettv->v_type = VAR_BOOL;
3668 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003669 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003670 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003671 else if (len == 4 && in_vim9script()
3672 && STRNCMP(s, "null", 4) == 0)
3673 {
3674 rettv->v_type = VAR_SPECIAL;
3675 rettv->vval.v_number = VVAL_NULL;
3676 ret = OK;
3677 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003678 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003679 ret = eval_variable(s, len, rettv, NULL,
3680 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003681 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003682 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003683 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003684 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003685 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003686 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003687 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003688 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003689 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003690 }
3691
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003692 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3693 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003694 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003695 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696
3697 /*
3698 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3699 */
3700 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003701 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003702 return ret;
3703}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003704
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003705/*
3706 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003707 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003708 * Adjusts "end_leaderp" until it is at "start_leader".
3709 */
3710 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003711eval7_leader(
3712 typval_T *rettv,
3713 int numeric_only,
3714 char_u *start_leader,
3715 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003716{
3717 char_u *end_leader = *end_leaderp;
3718 int ret = OK;
3719 int error = FALSE;
3720 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003721 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003722#ifdef FEAT_FLOAT
3723 float_T f = 0.0;
3724
3725 if (rettv->v_type == VAR_FLOAT)
3726 f = rettv->vval.v_float;
3727 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003728#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003729 {
3730 while (VIM_ISWHITE(end_leader[-1]))
3731 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003732 if (in_vim9script() && end_leader[-1] == '!')
3733 val = tv2bool(rettv);
3734 else
3735 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003736 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003737 if (error)
3738 {
3739 clear_tv(rettv);
3740 ret = FAIL;
3741 }
3742 else
3743 {
3744 while (end_leader > start_leader)
3745 {
3746 --end_leader;
3747 if (*end_leader == '!')
3748 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003749 if (numeric_only)
3750 {
3751 ++end_leader;
3752 break;
3753 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003754#ifdef FEAT_FLOAT
3755 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003756 {
3757 if (in_vim9script())
3758 {
3759 rettv->v_type = VAR_BOOL;
3760 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3761 }
3762 else
3763 f = !f;
3764 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003765 else
3766#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003767 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003768 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003769 type = VAR_BOOL;
3770 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003771 }
3772 else if (*end_leader == '-')
3773 {
3774#ifdef FEAT_FLOAT
3775 if (rettv->v_type == VAR_FLOAT)
3776 f = -f;
3777 else
3778#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003779 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003780 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003781 type = VAR_NUMBER;
3782 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003783 }
3784 }
3785#ifdef FEAT_FLOAT
3786 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003788 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003789 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003791 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003792#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003793 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003794 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003795 if (in_vim9script())
3796 rettv->v_type = type;
3797 else
3798 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003799 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003802 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 return ret;
3804}
3805
3806/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003807 * Call the function referred to in "rettv".
3808 */
3809 static int
3810call_func_rettv(
3811 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003812 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003813 typval_T *rettv,
3814 int evaluate,
3815 dict_T *selfdict,
3816 typval_T *basetv)
3817{
3818 partial_T *pt = NULL;
3819 funcexe_T funcexe;
3820 typval_T functv;
3821 char_u *s;
3822 int ret;
3823
3824 // need to copy the funcref so that we can clear rettv
3825 if (evaluate)
3826 {
3827 functv = *rettv;
3828 rettv->v_type = VAR_UNKNOWN;
3829
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003830 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003831 if (functv.v_type == VAR_PARTIAL)
3832 {
3833 pt = functv.vval.v_partial;
3834 s = partial_name(pt);
3835 }
3836 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003837 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003838 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003839 if (s == NULL || *s == NUL)
3840 {
3841 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02003842 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003843 goto theend;
3844 }
3845 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003846 }
3847 else
3848 s = (char_u *)"";
3849
Bram Moolenaara80faa82020-04-12 19:37:17 +02003850 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003851 funcexe.firstline = curwin->w_cursor.lnum;
3852 funcexe.lastline = curwin->w_cursor.lnum;
3853 funcexe.evaluate = evaluate;
3854 funcexe.partial = pt;
3855 funcexe.selfdict = selfdict;
3856 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003857 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003858
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003859theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003860 // Clear the funcref afterwards, so that deleting it while
3861 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003862 if (evaluate)
3863 clear_tv(&functv);
3864
3865 return ret;
3866}
3867
3868/*
3869 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003870 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003871 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3872 */
3873 static int
3874eval_lambda(
3875 char_u **arg,
3876 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003877 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003878 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003879{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003880 int evaluate = evalarg != NULL
3881 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003882 typval_T base = *rettv;
3883 int ret;
3884
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003885 rettv->v_type = VAR_UNKNOWN;
3886
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003887 if (**arg == '{')
3888 {
3889 // ->{lambda}()
3890 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3891 }
3892 else
3893 {
3894 // ->(lambda)()
3895 ++*arg;
3896 ret = eval1(arg, rettv, evalarg);
3897 *arg = skipwhite_and_linebreak(*arg, evalarg);
3898 if (**arg != ')')
3899 {
3900 emsg(_(e_missing_close));
3901 ret = FAIL;
3902 }
3903 ++*arg;
3904 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003905 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003906 return FAIL;
3907 else if (**arg != '(')
3908 {
3909 if (verbose)
3910 {
3911 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003912 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003913 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003914 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003915 }
3916 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003917 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003918 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003919 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003920 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003921
3922 // Clear the funcref afterwards, so that deleting it while
3923 // evaluating the arguments is possible (see test55).
3924 if (evaluate)
3925 clear_tv(&base);
3926
3927 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003928}
3929
3930/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003931 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003932 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02003933 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3934 */
3935 static int
3936eval_method(
3937 char_u **arg,
3938 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003939 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003940 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003941{
3942 char_u *name;
3943 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003944 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003945 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003946 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003947 int evaluate = evalarg != NULL
3948 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003949
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003950 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003951
Bram Moolenaarac92e252019-08-03 21:58:38 +02003952 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003953 len = get_name_len(arg, &alias, evaluate, TRUE);
3954 if (alias != NULL)
3955 name = alias;
3956
3957 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003958 {
3959 if (verbose)
3960 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003961 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003962 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003963 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003964 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003965 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003966 if (**arg != '(')
3967 {
3968 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003969 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003970 ret = FAIL;
3971 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003972 else if (VIM_ISWHITE((*arg)[-1]))
3973 {
3974 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003975 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003976 ret = FAIL;
3977 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003978 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003979 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003980 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003981 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003982
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003983 // Clear the funcref afterwards, so that deleting it while
3984 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003985 if (evaluate)
3986 clear_tv(&base);
3987
Bram Moolenaarac92e252019-08-03 21:58:38 +02003988 return ret;
3989}
3990
3991/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003992 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3993 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003994 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3995 */
3996 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003997eval_index(
3998 char_u **arg,
3999 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004000 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004001 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004002{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004003 int evaluate = evalarg != NULL
4004 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004005 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004006 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004007 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004008 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004009 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004010 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004011
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004012 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4013 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004014
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004015 init_tv(&var1);
4016 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004017 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004018 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004019 /*
4020 * dict.name
4021 */
4022 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004023 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004024 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004025 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004026 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004027 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004028 }
4029 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004030 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004031 /*
4032 * something[idx]
4033 *
4034 * Get the (first) variable from inside the [].
4035 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004036 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004037 if (**arg == ':')
4038 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004039 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004040 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004041 else if (vim9 && **arg == ':')
4042 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004043 semsg(_(e_white_space_required_before_and_after_str_at_str),
4044 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004045 clear_tv(&var1);
4046 return FAIL;
4047 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004048 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004049 {
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004050#ifdef FEAT_FLOAT
4051 // allow for indexing with float
4052 if (vim9 && rettv->v_type == VAR_DICT
4053 && var1.v_type == VAR_FLOAT)
4054 {
4055 var1.vval.v_string = typval_tostring(&var1, TRUE);
4056 var1.v_type = VAR_STRING;
4057 }
4058#endif
4059 if (tv_get_string_chk(&var1) == NULL)
4060 {
4061 // not a number or string
4062 clear_tv(&var1);
4063 return FAIL;
4064 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004065 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004066
4067 /*
4068 * Get the second variable from inside the [:].
4069 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004070 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004071 if (**arg == ':')
4072 {
4073 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004074 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01004075 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004076 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004077 semsg(_(e_white_space_required_before_and_after_str_at_str),
4078 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004079 if (!empty1)
4080 clear_tv(&var1);
4081 return FAIL;
4082 }
4083 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004084 if (**arg == ']')
4085 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004086 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004087 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004088 if (!empty1)
4089 clear_tv(&var1);
4090 return FAIL;
4091 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004092 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004093 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004094 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004095 if (!empty1)
4096 clear_tv(&var1);
4097 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004098 return FAIL;
4099 }
4100 }
4101
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004102 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004103 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004104 if (**arg != ']')
4105 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004106 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004107 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004108 clear_tv(&var1);
4109 if (range)
4110 clear_tv(&var2);
4111 return FAIL;
4112 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004113 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004114 }
4115
4116 if (evaluate)
4117 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004118 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004119 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004120 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004121
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004122 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004123 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004124 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004125 clear_tv(&var2);
4126 return res;
4127 }
4128 return OK;
4129}
4130
4131/*
4132 * Check if "rettv" can have an [index] or [sli:ce]
4133 */
4134 int
4135check_can_index(typval_T *rettv, int evaluate, int verbose)
4136{
4137 switch (rettv->v_type)
4138 {
4139 case VAR_FUNC:
4140 case VAR_PARTIAL:
4141 if (verbose)
4142 emsg(_("E695: Cannot index a Funcref"));
4143 return FAIL;
4144 case VAR_FLOAT:
4145#ifdef FEAT_FLOAT
4146 if (verbose)
4147 emsg(_(e_float_as_string));
4148 return FAIL;
4149#endif
4150 case VAR_BOOL:
4151 case VAR_SPECIAL:
4152 case VAR_JOB:
4153 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004154 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004155 if (verbose)
4156 emsg(_(e_cannot_index_special_variable));
4157 return FAIL;
4158 case VAR_UNKNOWN:
4159 case VAR_ANY:
4160 case VAR_VOID:
4161 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004162 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004163 emsg(_(e_cannot_index_special_variable));
4164 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004165 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004166 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004167
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004168 case VAR_STRING:
4169 case VAR_LIST:
4170 case VAR_DICT:
4171 case VAR_BLOB:
4172 break;
4173 case VAR_NUMBER:
4174 if (in_vim9script())
4175 emsg(_(e_cannot_index_number));
4176 break;
4177 }
4178 return OK;
4179}
4180
4181/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004182 * slice() function
4183 */
4184 void
4185f_slice(typval_T *argvars, typval_T *rettv)
4186{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004187 if (in_vim9script()
4188 && ((argvars[0].v_type != VAR_LIST
4189 && argvars[0].v_type != VAR_BLOB
4190 && argvars[0].v_type != VAR_STRING
4191 && check_for_list_arg(argvars, 0) == FAIL)
4192 || check_for_number_arg(argvars, 1) == FAIL
4193 || check_for_opt_number_arg(argvars, 2) == FAIL))
4194 return;
4195
Bram Moolenaar6601b622021-01-13 21:47:15 +01004196 if (check_can_index(argvars, TRUE, FALSE) == OK)
4197 {
4198 copy_tv(argvars, rettv);
4199 eval_index_inner(rettv, TRUE, argvars + 1,
4200 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4201 TRUE, NULL, 0, FALSE);
4202 }
4203}
4204
4205/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004206 * Apply index or range to "rettv".
4207 * "var1" is the first index, NULL for [:expr].
4208 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004209 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4210 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004211 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4212 */
4213 int
4214eval_index_inner(
4215 typval_T *rettv,
4216 int is_range,
4217 typval_T *var1,
4218 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004219 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004220 char_u *key,
4221 int keylen,
4222 int verbose)
4223{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004224 varnumber_T n1, n2 = 0;
4225 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004226
4227 n1 = 0;
4228 if (var1 != NULL && rettv->v_type != VAR_DICT)
4229 n1 = tv_get_number(var1);
4230
4231 if (is_range)
4232 {
4233 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004234 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004235 if (verbose)
4236 emsg(_(e_cannot_slice_dictionary));
4237 return FAIL;
4238 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004239 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004240 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004241 else
4242 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004243 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004244
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004245 switch (rettv->v_type)
4246 {
4247 case VAR_UNKNOWN:
4248 case VAR_ANY:
4249 case VAR_VOID:
4250 case VAR_FUNC:
4251 case VAR_PARTIAL:
4252 case VAR_FLOAT:
4253 case VAR_BOOL:
4254 case VAR_SPECIAL:
4255 case VAR_JOB:
4256 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004257 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004258 break; // not evaluating, skipping over subscript
4259
4260 case VAR_NUMBER:
4261 case VAR_STRING:
4262 {
4263 char_u *s = tv_get_string(rettv);
4264
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004265 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004266 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004267 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004268 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004269 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004270 else
4271 s = char_from_string(s, n1);
4272 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004273 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004274 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004275 // The resulting variable is a substring. If the indexes
4276 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004277 if (n1 < 0)
4278 {
4279 n1 = len + n1;
4280 if (n1 < 0)
4281 n1 = 0;
4282 }
4283 if (n2 < 0)
4284 n2 = len + n2;
4285 else if (n2 >= len)
4286 n2 = len;
4287 if (n1 >= len || n2 < 0 || n1 > n2)
4288 s = NULL;
4289 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004290 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004291 }
4292 else
4293 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004294 // The resulting variable is a string of a single
4295 // character. If the index is too big or negative the
4296 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004297 if (n1 >= len || n1 < 0)
4298 s = NULL;
4299 else
4300 s = vim_strnsave(s + n1, 1);
4301 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004302 clear_tv(rettv);
4303 rettv->v_type = VAR_STRING;
4304 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004305 }
4306 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004307
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004308 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004309 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4310 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004311 break;
4312
4313 case VAR_LIST:
4314 if (var1 == NULL)
4315 n1 = 0;
4316 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004317 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004318 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004319 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004320 return FAIL;
4321 break;
4322
4323 case VAR_DICT:
4324 {
4325 dictitem_T *item;
4326 typval_T tmp;
4327
4328 if (key == NULL)
4329 {
4330 key = tv_get_string_chk(var1);
4331 if (key == NULL)
4332 return FAIL;
4333 }
4334
4335 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
4336
4337 if (item == NULL && verbose)
4338 semsg(_(e_dictkey), key);
4339 if (item == NULL)
4340 return FAIL;
4341
4342 copy_tv(&item->di_tv, &tmp);
4343 clear_tv(rettv);
4344 *rettv = tmp;
4345 }
4346 break;
4347 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004348 return OK;
4349}
4350
4351/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004352 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004353 */
4354 char_u *
4355partial_name(partial_T *pt)
4356{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004357 if (pt != NULL)
4358 {
4359 if (pt->pt_name != NULL)
4360 return pt->pt_name;
4361 if (pt->pt_func != NULL)
4362 return pt->pt_func->uf_name;
4363 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004364 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004365}
4366
Bram Moolenaarddecc252016-04-06 22:59:37 +02004367 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004368partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004369{
4370 int i;
4371
4372 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004373 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004374 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004375 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004376 if (pt->pt_name != NULL)
4377 {
4378 func_unref(pt->pt_name);
4379 vim_free(pt->pt_name);
4380 }
4381 else
4382 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004383
Bram Moolenaar54656012021-06-09 20:50:46 +02004384 // "out_up" is no longer used, decrement refcount on partial that owns it.
4385 partial_unref(pt->pt_outer.out_up_partial);
4386
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004387 // Decrease the reference count for the context of a closure. If down
4388 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004389 if (pt->pt_funcstack != NULL)
4390 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004391 --pt->pt_funcstack->fs_refcount;
4392 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004393 }
4394
Bram Moolenaarddecc252016-04-06 22:59:37 +02004395 vim_free(pt);
4396}
4397
4398/*
4399 * Unreference a closure: decrement the reference count and free it when it
4400 * becomes zero.
4401 */
4402 void
4403partial_unref(partial_T *pt)
4404{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004405 if (pt != NULL)
4406 {
4407 if (--pt->pt_refcount <= 0)
4408 partial_free(pt);
4409
4410 // If the reference count goes down to one, the funcstack may be the
4411 // only reference and can be freed if no other partials reference it.
4412 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4413 funcstack_check_refcount(pt->pt_funcstack);
4414 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004415}
4416
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004417/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004418 * Return the next (unique) copy ID.
4419 * Used for serializing nested structures.
4420 */
4421 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004422get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004423{
4424 current_copyID += COPYID_INC;
4425 return current_copyID;
4426}
4427
4428/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004429 * Garbage collection for lists and dictionaries.
4430 *
4431 * We use reference counts to be able to free most items right away when they
4432 * are no longer used. But for composite items it's possible that it becomes
4433 * unused while the reference count is > 0: When there is a recursive
4434 * reference. Example:
4435 * :let l = [1, 2, 3]
4436 * :let d = {9: l}
4437 * :let l[1] = d
4438 *
4439 * Since this is quite unusual we handle this with garbage collection: every
4440 * once in a while find out which lists and dicts are not referenced from any
4441 * variable.
4442 *
4443 * Here is a good reference text about garbage collection (refers to Python
4444 * but it applies to all reference-counting mechanisms):
4445 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004446 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004447
4448/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004449 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004450 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004451 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004452 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004453 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004454garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004455{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004456 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004457 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004458 buf_T *buf;
4459 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004460 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004461 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004462
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004463 if (!testing)
4464 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004465 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004466 want_garbage_collect = FALSE;
4467 may_garbage_collect = FALSE;
4468 garbage_collect_at_exit = FALSE;
4469 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004470
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004471 // The execution stack can grow big, limit the size.
4472 if (exestack.ga_maxlen - exestack.ga_len > 500)
4473 {
4474 size_t new_len;
4475 char_u *pp;
4476 int n;
4477
4478 // Keep 150% of the current size, with a minimum of the growth size.
4479 n = exestack.ga_len / 2;
4480 if (n < exestack.ga_growsize)
4481 n = exestack.ga_growsize;
4482
4483 // Don't make it bigger though.
4484 if (exestack.ga_len + n < exestack.ga_maxlen)
4485 {
4486 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4487 pp = vim_realloc(exestack.ga_data, new_len);
4488 if (pp == NULL)
4489 return FAIL;
4490 exestack.ga_maxlen = exestack.ga_len + n;
4491 exestack.ga_data = pp;
4492 }
4493 }
4494
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004495 // We advance by two because we add one for items referenced through
4496 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004497 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004498
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004499 /*
4500 * 1. Go through all accessible variables and mark all lists and dicts
4501 * with copyID.
4502 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004503
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004504 // Don't free variables in the previous_funccal list unless they are only
4505 // referenced through previous_funccal. This must be first, because if
4506 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004507 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004508
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004509 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004510 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004511
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004512 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004513 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004514 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4515 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004516
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004517 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004518 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004519 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4520 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004521 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004522 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4523 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004524#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004525 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004526 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4527 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004528 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004529 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004530 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4531 NULL, NULL);
4532#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004533
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004534 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004535 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004536 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4537 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004538 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004539 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004540
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004541 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004542 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004543
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004544 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004545 abort = abort || set_ref_in_functions(copyID);
4546
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004547 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004548 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004549
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004550 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004551 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004552
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004553 // callbacks in buffers
4554 abort = abort || set_ref_in_buffers(copyID);
4555
Bram Moolenaar1dced572012-04-05 16:54:08 +02004556#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004557 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004558#endif
4559
Bram Moolenaardb913952012-06-29 12:54:53 +02004560#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004561 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004562#endif
4563
4564#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004565 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004566#endif
4567
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004568#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004569 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004570 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004571#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004572#ifdef FEAT_NETBEANS_INTG
4573 abort = abort || set_ref_in_nb_channel(copyID);
4574#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004575
Bram Moolenaare3188e22016-05-31 21:13:04 +02004576#ifdef FEAT_TIMERS
4577 abort = abort || set_ref_in_timer(copyID);
4578#endif
4579
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004580#ifdef FEAT_QUICKFIX
4581 abort = abort || set_ref_in_quickfix(copyID);
4582#endif
4583
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004584#ifdef FEAT_TERMINAL
4585 abort = abort || set_ref_in_term(copyID);
4586#endif
4587
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004588#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004589 abort = abort || set_ref_in_popups(copyID);
4590#endif
4591
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004592 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004593 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004594 /*
4595 * 2. Free lists and dictionaries that are not referenced.
4596 */
4597 did_free = free_unref_items(copyID);
4598
4599 /*
4600 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004601 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004602 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004603 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004604 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004605 else if (p_verbose > 0)
4606 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004607 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004608 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004609
4610 return did_free;
4611}
4612
4613/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004614 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004615 */
4616 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004617free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004618{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004619 int did_free = FALSE;
4620
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004621 // Let all "free" functions know that we are here. This means no
4622 // dictionaries, lists, channels or jobs are to be freed, because we will
4623 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004624 in_free_unref_items = TRUE;
4625
4626 /*
4627 * PASS 1: free the contents of the items. We don't free the items
4628 * themselves yet, so that it is possible to decrement refcount counters
4629 */
4630
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004631 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004632 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004633
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004634 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004635 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004636
4637#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004638 // Go through the list of jobs and free items without the copyID. This
4639 // must happen before doing channels, because jobs refer to channels, but
4640 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004641 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4642
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004643 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004644 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4645#endif
4646
4647 /*
4648 * PASS 2: free the items themselves.
4649 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004650 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004651 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004652
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004653#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004654 // Go through the list of jobs and free items without the copyID. This
4655 // must happen before doing channels, because jobs refer to channels, but
4656 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004657 free_unused_jobs(copyID, COPYID_MASK);
4658
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004659 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004660 free_unused_channels(copyID, COPYID_MASK);
4661#endif
4662
4663 in_free_unref_items = FALSE;
4664
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004665 return did_free;
4666}
4667
4668/*
4669 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004670 * "list_stack" is used to add lists to be marked. Can be NULL.
4671 *
4672 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004673 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004674 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004675set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004676{
4677 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004678 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004679 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004680 hashtab_T *cur_ht;
4681 ht_stack_T *ht_stack = NULL;
4682 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004683
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004684 cur_ht = ht;
4685 for (;;)
4686 {
4687 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004688 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004689 // Mark each item in the hashtab. If the item contains a hashtab
4690 // it is added to ht_stack, if it contains a list it is added to
4691 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004692 todo = (int)cur_ht->ht_used;
4693 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4694 if (!HASHITEM_EMPTY(hi))
4695 {
4696 --todo;
4697 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4698 &ht_stack, list_stack);
4699 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004700 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004701
4702 if (ht_stack == NULL)
4703 break;
4704
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004705 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004706 cur_ht = ht_stack->ht;
4707 tempitem = ht_stack;
4708 ht_stack = ht_stack->prev;
4709 free(tempitem);
4710 }
4711
4712 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004713}
4714
4715/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004716 * Mark a dict and its items with "copyID".
4717 * Returns TRUE if setting references failed somehow.
4718 */
4719 int
4720set_ref_in_dict(dict_T *d, int copyID)
4721{
4722 if (d != NULL && d->dv_copyID != copyID)
4723 {
4724 d->dv_copyID = copyID;
4725 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4726 }
4727 return FALSE;
4728}
4729
4730/*
4731 * Mark a list and its items with "copyID".
4732 * Returns TRUE if setting references failed somehow.
4733 */
4734 int
4735set_ref_in_list(list_T *ll, int copyID)
4736{
4737 if (ll != NULL && ll->lv_copyID != copyID)
4738 {
4739 ll->lv_copyID = copyID;
4740 return set_ref_in_list_items(ll, copyID, NULL);
4741 }
4742 return FALSE;
4743}
4744
4745/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004746 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004747 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4748 *
4749 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004750 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004751 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004752set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004753{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004754 listitem_T *li;
4755 int abort = FALSE;
4756 list_T *cur_l;
4757 list_stack_T *list_stack = NULL;
4758 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004759
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004760 cur_l = l;
4761 for (;;)
4762 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004763 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004764 // Mark each item in the list. If the item contains a hashtab
4765 // it is added to ht_stack, if it contains a list it is added to
4766 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004767 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4768 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4769 ht_stack, &list_stack);
4770 if (list_stack == NULL)
4771 break;
4772
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004773 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004774 cur_l = list_stack->list;
4775 tempitem = list_stack;
4776 list_stack = list_stack->prev;
4777 free(tempitem);
4778 }
4779
4780 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004781}
4782
4783/*
4784 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004785 * "list_stack" is used to add lists to be marked. Can be NULL.
4786 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4787 *
4788 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004789 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004790 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004791set_ref_in_item(
4792 typval_T *tv,
4793 int copyID,
4794 ht_stack_T **ht_stack,
4795 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004796{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004797 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004798
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004799 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004800 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004801 dict_T *dd = tv->vval.v_dict;
4802
Bram Moolenaara03f2332016-02-06 18:09:59 +01004803 if (dd != NULL && dd->dv_copyID != copyID)
4804 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004805 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004806 dd->dv_copyID = copyID;
4807 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004808 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004809 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4810 }
4811 else
4812 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004813 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4814
Bram Moolenaara03f2332016-02-06 18:09:59 +01004815 if (newitem == NULL)
4816 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004817 else
4818 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004819 newitem->ht = &dd->dv_hashtab;
4820 newitem->prev = *ht_stack;
4821 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004822 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004823 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004824 }
4825 }
4826 else if (tv->v_type == VAR_LIST)
4827 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004828 list_T *ll = tv->vval.v_list;
4829
Bram Moolenaara03f2332016-02-06 18:09:59 +01004830 if (ll != NULL && ll->lv_copyID != copyID)
4831 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004832 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004833 ll->lv_copyID = copyID;
4834 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004835 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004836 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004837 }
4838 else
4839 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004840 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4841
Bram Moolenaara03f2332016-02-06 18:09:59 +01004842 if (newitem == NULL)
4843 abort = TRUE;
4844 else
4845 {
4846 newitem->list = ll;
4847 newitem->prev = *list_stack;
4848 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004849 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004850 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004851 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004852 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004853 else if (tv->v_type == VAR_FUNC)
4854 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004855 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004856 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004857 else if (tv->v_type == VAR_PARTIAL)
4858 {
4859 partial_T *pt = tv->vval.v_partial;
4860 int i;
4861
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004862 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004863 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004864 // Didn't see this partial yet.
4865 pt->pt_copyID = copyID;
4866
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004867 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004868
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004869 if (pt->pt_dict != NULL)
4870 {
4871 typval_T dtv;
4872
4873 dtv.v_type = VAR_DICT;
4874 dtv.vval.v_dict = pt->pt_dict;
4875 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4876 }
4877
4878 for (i = 0; i < pt->pt_argc; ++i)
4879 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4880 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004881 if (pt->pt_funcstack != NULL)
4882 {
4883 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4884
4885 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4886 abort = abort || set_ref_in_item(stack + i, copyID,
4887 ht_stack, list_stack);
4888 }
4889
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004890 }
4891 }
4892#ifdef FEAT_JOB_CHANNEL
4893 else if (tv->v_type == VAR_JOB)
4894 {
4895 job_T *job = tv->vval.v_job;
4896 typval_T dtv;
4897
4898 if (job != NULL && job->jv_copyID != copyID)
4899 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004900 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004901 if (job->jv_channel != NULL)
4902 {
4903 dtv.v_type = VAR_CHANNEL;
4904 dtv.vval.v_channel = job->jv_channel;
4905 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4906 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004907 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004908 {
4909 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004910 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004911 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4912 }
4913 }
4914 }
4915 else if (tv->v_type == VAR_CHANNEL)
4916 {
4917 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004918 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004919 typval_T dtv;
4920 jsonq_T *jq;
4921 cbq_T *cq;
4922
4923 if (ch != NULL && ch->ch_copyID != copyID)
4924 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004925 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004926 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004927 {
4928 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4929 jq = jq->jq_next)
4930 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4931 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4932 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004933 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004934 {
4935 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004936 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004937 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4938 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004939 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004940 {
4941 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004942 dtv.vval.v_partial =
4943 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004944 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4945 }
4946 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004947 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004948 {
4949 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004950 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004951 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4952 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004953 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004954 {
4955 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004956 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004957 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4958 }
4959 }
4960 }
4961#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004962 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004963}
4964
Bram Moolenaar8c711452005-01-14 21:53:12 +00004965/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004966 * Return a string with the string representation of a variable.
4967 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004968 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004969 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004970 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004971 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02004972 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004973 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4974 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004975 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004976 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004977 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004978echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004979 typval_T *tv,
4980 char_u **tofree,
4981 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004982 int copyID,
4983 int echo_style,
4984 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004985 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004986{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004987 static int recurse = 0;
4988 char_u *r = NULL;
4989
Bram Moolenaar33570922005-01-25 22:26:29 +00004990 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004991 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004992 if (!did_echo_string_emsg)
4993 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004994 // Only give this message once for a recursive call to avoid
4995 // flooding the user with errors. And stop iterating over lists
4996 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004997 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004998 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004999 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005000 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005001 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005002 }
5003 ++recurse;
5004
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005005 switch (tv->v_type)
5006 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005007 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005008 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005009 {
5010 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005011 r = tv->vval.v_string;
5012 if (r == NULL)
5013 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005014 }
5015 else
5016 {
5017 *tofree = string_quote(tv->vval.v_string, FALSE);
5018 r = *tofree;
5019 }
5020 break;
5021
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005022 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005023 if (echo_style)
5024 {
5025 *tofree = NULL;
5026 r = tv->vval.v_string;
5027 }
5028 else
5029 {
5030 *tofree = string_quote(tv->vval.v_string, TRUE);
5031 r = *tofree;
5032 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005033 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005034
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005035 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005036 {
5037 partial_T *pt = tv->vval.v_partial;
5038 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005039 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005040 garray_T ga;
5041 int i;
5042 char_u *tf;
5043
5044 ga_init2(&ga, 1, 100);
5045 ga_concat(&ga, (char_u *)"function(");
5046 if (fname != NULL)
5047 {
5048 ga_concat(&ga, fname);
5049 vim_free(fname);
5050 }
5051 if (pt != NULL && pt->pt_argc > 0)
5052 {
5053 ga_concat(&ga, (char_u *)", [");
5054 for (i = 0; i < pt->pt_argc; ++i)
5055 {
5056 if (i > 0)
5057 ga_concat(&ga, (char_u *)", ");
5058 ga_concat(&ga,
5059 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5060 vim_free(tf);
5061 }
5062 ga_concat(&ga, (char_u *)"]");
5063 }
5064 if (pt != NULL && pt->pt_dict != NULL)
5065 {
5066 typval_T dtv;
5067
5068 ga_concat(&ga, (char_u *)", ");
5069 dtv.v_type = VAR_DICT;
5070 dtv.vval.v_dict = pt->pt_dict;
5071 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5072 vim_free(tf);
5073 }
5074 ga_concat(&ga, (char_u *)")");
5075
5076 *tofree = ga.ga_data;
5077 r = *tofree;
5078 break;
5079 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005080
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005081 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005082 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005083 break;
5084
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005085 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005086 if (tv->vval.v_list == NULL)
5087 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005088 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005089 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005090 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005091 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005092 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5093 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005094 {
5095 *tofree = NULL;
5096 r = (char_u *)"[...]";
5097 }
5098 else
5099 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005100 int old_copyID = tv->vval.v_list->lv_copyID;
5101
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005102 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005103 *tofree = list2string(tv, copyID, restore_copyID);
5104 if (restore_copyID)
5105 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005106 r = *tofree;
5107 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005108 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005109
Bram Moolenaar8c711452005-01-14 21:53:12 +00005110 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005111 if (tv->vval.v_dict == NULL)
5112 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005113 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005114 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005115 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005116 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005117 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5118 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005119 {
5120 *tofree = NULL;
5121 r = (char_u *)"{...}";
5122 }
5123 else
5124 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005125 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005126
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005127 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005128 *tofree = dict2string(tv, copyID, restore_copyID);
5129 if (restore_copyID)
5130 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005131 r = *tofree;
5132 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005133 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005134
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005135 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005136 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005137 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005138 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005139 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005140 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005141 break;
5142
Bram Moolenaar835dc632016-02-07 14:27:38 +01005143 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005144 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005145#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005146 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005147 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5148 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005149 if (composite_val)
5150 {
5151 *tofree = string_quote(r, FALSE);
5152 r = *tofree;
5153 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005154#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005155 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005156
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005157 case VAR_INSTR:
5158 *tofree = NULL;
5159 r = (char_u *)"instructions";
5160 break;
5161
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005162 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005163#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005164 *tofree = NULL;
5165 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5166 r = numbuf;
5167 break;
5168#endif
5169
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005170 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005171 case VAR_SPECIAL:
5172 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005173 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005174 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005175 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005176
Bram Moolenaar8502c702014-06-17 12:51:16 +02005177 if (--recurse == 0)
5178 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005179 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005180}
5181
5182/*
5183 * Return a string with the string representation of a variable.
5184 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5185 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005186 * Does not put quotes around strings, as ":echo" displays values.
5187 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5188 * May return NULL.
5189 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005190 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005191echo_string(
5192 typval_T *tv,
5193 char_u **tofree,
5194 char_u *numbuf,
5195 int copyID)
5196{
5197 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5198}
5199
5200/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005201 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5202 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005203 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005204 */
5205 int
5206buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5207{
5208 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005209 char_u *t;
5210 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005211
5212 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5213 return -1;
5214
5215 if (lnum > buf->b_ml.ml_line_count)
5216 lnum = buf->b_ml.ml_line_count;
5217
5218 str = ml_get_buf(buf, lnum, FALSE);
5219 if (str == NULL)
5220 return -1;
5221
5222 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005223 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005224
Bram Moolenaar91458462021-01-13 20:08:38 +01005225 // count the number of characters
5226 t = str;
5227 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5228 t += mb_ptr2len(t);
5229
5230 // In insert mode, when the cursor is at the end of a non-empty line,
5231 // byteidx points to the NUL character immediately past the end of the
5232 // string. In this case, add one to the character count.
5233 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5234 count++;
5235
5236 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005237}
5238
5239/*
5240 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005241 * byte index. Works only for loaded buffers. Returns -1 on failure.
5242 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005243 */
5244 int
5245buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5246{
5247 char_u *str;
5248 char_u *t;
5249
5250 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5251 return -1;
5252
5253 if (lnum > buf->b_ml.ml_line_count)
5254 lnum = buf->b_ml.ml_line_count;
5255
5256 str = ml_get_buf(buf, lnum, FALSE);
5257 if (str == NULL)
5258 return -1;
5259
5260 // Convert the character offset to a byte offset
5261 t = str;
5262 while (*t != NUL && --charidx > 0)
5263 t += mb_ptr2len(t);
5264
Bram Moolenaar91458462021-01-13 20:08:38 +01005265 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005266}
5267
5268/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005270 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005272 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005273var2fpos(
5274 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005275 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005276 int *fnum, // set to fnum for '0, 'A, etc.
5277 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005279 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005281 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005283 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005284 if (varp->v_type == VAR_LIST)
5285 {
5286 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005287 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005288 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005289 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005290
5291 l = varp->vval.v_list;
5292 if (l == NULL)
5293 return NULL;
5294
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005295 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005296 pos.lnum = list_find_nr(l, 0L, &error);
5297 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005298 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005299 if (charcol)
5300 len = (long)mb_charlen(ml_get(pos.lnum));
5301 else
5302 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005303
Bram Moolenaarec65d772020-08-20 22:29:12 +02005304 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005305 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005306 li = list_find(l, 1L);
5307 if (li != NULL && li->li_tv.v_type == VAR_STRING
5308 && li->li_tv.vval.v_string != NULL
5309 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005310 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005311 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005312 }
5313 else
5314 {
5315 pos.col = list_find_nr(l, 1L, &error);
5316 if (error)
5317 return NULL;
5318 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005319
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005320 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005321 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005322 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005323 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005324
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005325 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005326 pos.coladd = list_find_nr(l, 2L, &error);
5327 if (error)
5328 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005329
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005330 return &pos;
5331 }
5332
Bram Moolenaarc5809432021-03-27 21:23:30 +01005333 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5334 return NULL;
5335
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005336 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005337 if (name == NULL)
5338 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005339 if (name[0] == '.') // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005340 {
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;
5345 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005346 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005347 {
5348 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005349 pos = VIsual;
5350 else
5351 pos = curwin->w_cursor;
5352 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005353 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005354 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005355 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005356 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005358 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5360 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005361 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005362 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 return pp;
5364 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005365
Bram Moolenaara5525202006-03-02 22:52:09 +00005366 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005367
Bram Moolenaar477933c2007-07-17 14:32:23 +00005368 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005369 {
5370 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005371 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005372 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005373 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005374 // In silent Ex mode topline is zero, but that's not a valid line
5375 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005376 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005377 return &pos;
5378 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005379 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005380 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005381 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005382 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005383 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005384 return &pos;
5385 }
5386 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005387 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005389 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 {
5391 pos.lnum = curbuf->b_ml.ml_line_count;
5392 pos.col = 0;
5393 }
5394 else
5395 {
5396 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005397 if (charcol)
5398 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5399 else
5400 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 }
5402 return &pos;
5403 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005404 if (in_vim9script())
5405 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 return NULL;
5407}
5408
5409/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005410 * Convert list in "arg" into a position and optional file number.
5411 * When "fnump" is NULL there is no file number, only 3 items.
5412 * Note that the column is passed on as-is, the caller may want to decrement
5413 * it to use 1 for the first column.
5414 * Return FAIL when conversion is not possible, doesn't check the position for
5415 * validity.
5416 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005417 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005418list2fpos(
5419 typval_T *arg,
5420 pos_T *posp,
5421 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005422 colnr_T *curswantp,
5423 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005424{
5425 list_T *l = arg->vval.v_list;
5426 long i = 0;
5427 long n;
5428
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005429 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5430 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005431 if (arg->v_type != VAR_LIST
5432 || l == NULL
5433 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005434 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005435 return FAIL;
5436
5437 if (fnump != NULL)
5438 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005439 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005440 if (n < 0)
5441 return FAIL;
5442 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005443 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005444 *fnump = n;
5445 }
5446
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005447 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005448 if (n < 0)
5449 return FAIL;
5450 posp->lnum = n;
5451
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005452 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005453 if (n < 0)
5454 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005455 // If character position is specified, then convert to byte position
5456 if (charcol)
5457 {
5458 buf_T *buf;
5459
5460 // Get the text for the specified line in a loaded buffer
5461 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5462 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5463 return FAIL;
5464
Bram Moolenaar91458462021-01-13 20:08:38 +01005465 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005466 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005467 posp->col = n;
5468
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005469 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005470 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005471 posp->coladd = 0;
5472 else
5473 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005474
Bram Moolenaar493c1782014-05-28 14:34:46 +02005475 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005476 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005477
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005478 return OK;
5479}
5480
5481/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 * Get the length of an environment variable name.
5483 * Advance "arg" to the first character after the name.
5484 * Return 0 for error.
5485 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005486 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005487get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488{
5489 char_u *p;
5490 int len;
5491
5492 for (p = *arg; vim_isIDc(*p); ++p)
5493 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005494 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 return 0;
5496
5497 len = (int)(p - *arg);
5498 *arg = p;
5499 return len;
5500}
5501
5502/*
5503 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005504 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 * Return 0 if something is wrong.
5506 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005507 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005508get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509{
5510 char_u *p;
5511 int len;
5512
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005513 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005515 {
5516 if (*p == ':')
5517 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005518 // "s:" is start of "s:var", but "n:" is not and can be used in
5519 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005520 len = (int)(p - *arg);
5521 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5522 || len > 1)
5523 break;
5524 }
5525 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005526 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 return 0;
5528
5529 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005530 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531
5532 return len;
5533}
5534
5535/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005536 * Get the length of the name of a variable or function.
5537 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005539 * Return -1 if curly braces expansion failed.
5540 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 * If the name contains 'magic' {}'s, expand them and return the
5542 * expanded name in an allocated string via 'alias' - caller must free.
5543 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005544 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005545get_name_len(
5546 char_u **arg,
5547 char_u **alias,
5548 int evaluate,
5549 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550{
5551 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 char_u *p;
5553 char_u *expr_start;
5554 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005556 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557
5558 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5559 && (*arg)[2] == (int)KE_SNR)
5560 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005561 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 *arg += 3;
5563 return get_id_len(arg) + 3;
5564 }
5565 len = eval_fname_script(*arg);
5566 if (len > 0)
5567 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005568 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 *arg += len;
5570 }
5571
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005573 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005575 p = find_name_end(*arg, &expr_start, &expr_end,
5576 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 if (expr_start != NULL)
5578 {
5579 char_u *temp_string;
5580
5581 if (!evaluate)
5582 {
5583 len += (int)(p - *arg);
5584 *arg = skipwhite(p);
5585 return len;
5586 }
5587
5588 /*
5589 * Include any <SID> etc in the expanded string:
5590 * Thus the -len here.
5591 */
5592 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5593 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005594 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 *alias = temp_string;
5596 *arg = skipwhite(p);
5597 return (int)STRLEN(temp_string);
5598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599
5600 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005601 // Only give an error when there is something, otherwise it will be
5602 // reported at a higher level.
5603 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005604 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605
5606 return len;
5607}
5608
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005609/*
5610 * Find the end of a variable or function name, taking care of magic braces.
5611 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5612 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005613 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005614 * Return a pointer to just after the name. Equal to "arg" if there is no
5615 * valid name.
5616 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005617 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005618find_name_end(
5619 char_u *arg,
5620 char_u **expr_start,
5621 char_u **expr_end,
5622 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005624 int mb_nest = 0;
5625 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005627 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005628 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005630 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005632 *expr_start = NULL;
5633 *expr_end = NULL;
5634 }
5635
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005636 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005637 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5638 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005639 return arg;
5640
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005641 for (p = arg; *p != NUL
5642 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005643 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005644 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005645 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005646 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005647 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005648 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005649 if (*p == '\'')
5650 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005651 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005652 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005653 ;
5654 if (*p == NUL)
5655 break;
5656 }
5657 else if (*p == '"')
5658 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005659 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005660 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005661 if (*p == '\\' && p[1] != NUL)
5662 ++p;
5663 if (*p == NUL)
5664 break;
5665 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005666 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5667 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005668 // "s:" is start of "s:var", but "n:" is not and can be used in
5669 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005670 len = (int)(p - arg);
5671 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005672 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005673 break;
5674 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005675
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005676 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005678 if (*p == '[')
5679 ++br_nest;
5680 else if (*p == ']')
5681 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005683
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005684 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005686 if (*p == '{')
5687 {
5688 mb_nest++;
5689 if (expr_start != NULL && *expr_start == NULL)
5690 *expr_start = p;
5691 }
5692 else if (*p == '}')
5693 {
5694 mb_nest--;
5695 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5696 *expr_end = p;
5697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 }
5700
5701 return p;
5702}
5703
5704/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005705 * Expands out the 'magic' {}'s in a variable/function name.
5706 * Note that this can call itself recursively, to deal with
5707 * constructs like foo{bar}{baz}{bam}
5708 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5709 * "in_start" ^
5710 * "expr_start" ^
5711 * "expr_end" ^
5712 * "in_end" ^
5713 *
5714 * Returns a new allocated string, which the caller must free.
5715 * Returns NULL for failure.
5716 */
5717 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005718make_expanded_name(
5719 char_u *in_start,
5720 char_u *expr_start,
5721 char_u *expr_end,
5722 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005723{
5724 char_u c1;
5725 char_u *retval = NULL;
5726 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005727
5728 if (expr_end == NULL || in_end == NULL)
5729 return NULL;
5730 *expr_start = NUL;
5731 *expr_end = NUL;
5732 c1 = *in_end;
5733 *in_end = NUL;
5734
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005735 temp_result = eval_to_string(expr_start + 1, FALSE);
5736 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005737 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005738 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5739 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005740 if (retval != NULL)
5741 {
5742 STRCPY(retval, in_start);
5743 STRCAT(retval, temp_result);
5744 STRCAT(retval, expr_end + 1);
5745 }
5746 }
5747 vim_free(temp_result);
5748
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005749 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005750 *expr_start = '{';
5751 *expr_end = '}';
5752
5753 if (retval != NULL)
5754 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005755 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005756 if (expr_start != NULL)
5757 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005758 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005759 temp_result = make_expanded_name(retval, expr_start,
5760 expr_end, temp_result);
5761 vim_free(retval);
5762 retval = temp_result;
5763 }
5764 }
5765
5766 return retval;
5767}
5768
5769/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005771 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005773 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005774eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005776 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005777}
5778
5779/*
5780 * Return TRUE if character "c" can be used as the first character in a
5781 * variable or function name (excluding '{' and '}').
5782 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005783 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005784eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005785{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005786 return ASCII_ISALPHA(c) || c == '_';
5787}
5788
5789/*
5790 * Return TRUE if character "c" can be used as the first character of a
5791 * dictionary key.
5792 */
5793 int
5794eval_isdictc(int c)
5795{
5796 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797}
5798
5799/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005800 * Handle:
5801 * - expr[expr], expr[expr:expr] subscript
5802 * - ".name" lookup
5803 * - function call with Funcref variable: func(expr)
5804 * - method call: var->method()
5805 *
5806 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005807 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005808 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005809handle_subscript(
5810 char_u **arg,
5811 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005812 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005813 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005814{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005815 int evaluate = evalarg != NULL
5816 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005817 int ret = OK;
5818 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005819 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005820 int getnext;
5821 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005822
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005823 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005824 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005825 // When at the end of the line and ".name" or "->{" or "->X" follows in
5826 // the next line then consume the line break.
5827 p = eval_next_non_blank(*arg, evalarg, &getnext);
5828 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005829 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02005830 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
5831 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
5832 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005833 {
5834 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005835 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005836 check_white = FALSE;
5837 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005838
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01005839 if (rettv->v_type == VAR_ANY)
5840 {
5841 char_u *exp_name;
5842 int cc;
5843 int idx;
5844 ufunc_T *ufunc;
5845 type_T *type;
5846
5847 // Found script from "import * as {name}", script item name must
5848 // follow.
5849 if (**arg != '.')
5850 {
5851 if (verbose)
5852 semsg(_(e_expected_str_but_got_str), "'.'", *arg);
5853 ret = FAIL;
5854 break;
5855 }
5856 ++*arg;
5857 if (IS_WHITE_OR_NUL(**arg))
5858 {
5859 if (verbose)
5860 emsg(_(e_no_white_space_allowed_after_dot));
5861 ret = FAIL;
5862 break;
5863 }
5864
5865 // isolate the name
5866 exp_name = *arg;
5867 while (eval_isnamec(**arg))
5868 ++*arg;
5869 cc = **arg;
5870 **arg = NUL;
5871
5872 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
5873 evalarg->eval_cctx, verbose);
5874 **arg = cc;
5875 *arg = skipwhite(*arg);
5876
5877 if (idx < 0 && ufunc == NULL)
5878 {
5879 ret = FAIL;
5880 break;
5881 }
5882 if (idx >= 0)
5883 {
5884 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
5885 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
5886
5887 copy_tv(sv->sv_tv, rettv);
5888 }
5889 else
5890 {
5891 rettv->v_type = VAR_FUNC;
5892 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
5893 }
5894 }
5895
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005896 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5897 || rettv->v_type == VAR_PARTIAL))
5898 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005899 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005900 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5901 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005902
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005903 // Stop the expression evaluation when immediately aborting on
5904 // error, or when an interrupt occurred or an exception was thrown
5905 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005906 if (aborting())
5907 {
5908 if (ret == OK)
5909 clear_tv(rettv);
5910 ret = FAIL;
5911 }
5912 dict_unref(selfdict);
5913 selfdict = NULL;
5914 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005915 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005916 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005917 if (in_vim9script())
5918 *arg = skipwhite(p + 2);
5919 else
5920 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005921 if (ret == OK)
5922 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005923 if (VIM_ISWHITE(**arg))
5924 {
5925 emsg(_(e_nowhitespace));
5926 ret = FAIL;
5927 }
5928 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005929 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005930 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005931 else
5932 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005933 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005934 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005935 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005936 // "." is ".name" lookup when we found a dict or when evaluating and
5937 // scriptversion is at least 2, where string concatenation is "..".
5938 else if (**arg == '['
5939 || (**arg == '.' && (rettv->v_type == VAR_DICT
5940 || (!evaluate
5941 && (*arg)[1] != '.'
5942 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005943 {
5944 dict_unref(selfdict);
5945 if (rettv->v_type == VAR_DICT)
5946 {
5947 selfdict = rettv->vval.v_dict;
5948 if (selfdict != NULL)
5949 ++selfdict->dv_refcount;
5950 }
5951 else
5952 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005953 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005954 {
5955 clear_tv(rettv);
5956 ret = FAIL;
5957 }
5958 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005959 else
5960 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005961 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005962
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005963 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5964 // Don't do this when "Func" is already a partial that was bound
5965 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005966 if (selfdict != NULL
5967 && (rettv->v_type == VAR_FUNC
5968 || (rettv->v_type == VAR_PARTIAL
5969 && (rettv->vval.v_partial->pt_auto
5970 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005971 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005972
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005973 dict_unref(selfdict);
5974 return ret;
5975}
5976
5977/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005978 * Make a copy of an item.
5979 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005980 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5981 * reference to an already copied list/dict can be used.
5982 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005983 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005984 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005985item_copy(
5986 typval_T *from,
5987 typval_T *to,
5988 int deep,
5989 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005990{
5991 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005992 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005993
Bram Moolenaar33570922005-01-25 22:26:29 +00005994 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005995 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005996 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005997 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005998 }
5999 ++recurse;
6000
6001 switch (from->v_type)
6002 {
6003 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006004 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006005 case VAR_STRING:
6006 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006007 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006008 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006009 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006010 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006011 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006012 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006013 copy_tv(from, to);
6014 break;
6015 case VAR_LIST:
6016 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006017 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006018 if (from->vval.v_list == NULL)
6019 to->vval.v_list = NULL;
6020 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6021 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006022 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006023 to->vval.v_list = from->vval.v_list->lv_copylist;
6024 ++to->vval.v_list->lv_refcount;
6025 }
6026 else
6027 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
6028 if (to->vval.v_list == NULL)
6029 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006030 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006031 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006032 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006033 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006034 case VAR_DICT:
6035 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006036 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006037 if (from->vval.v_dict == NULL)
6038 to->vval.v_dict = NULL;
6039 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6040 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006041 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006042 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6043 ++to->vval.v_dict->dv_refcount;
6044 }
6045 else
6046 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
6047 if (to->vval.v_dict == NULL)
6048 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006049 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006050 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006051 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006052 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006053 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006054 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006055 }
6056 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006057 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006058}
6059
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006060 void
6061echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6062{
6063 char_u *tofree;
6064 char_u numbuf[NUMBUFLEN];
6065 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6066
6067 if (*atstart)
6068 {
6069 *atstart = FALSE;
6070 // Call msg_start() after eval1(), evaluating the expression
6071 // may cause a message to appear.
6072 if (with_space)
6073 {
6074 // Mark the saved text as finishing the line, so that what
6075 // follows is displayed on a new line when scrolling back
6076 // at the more prompt.
6077 msg_sb_eol();
6078 msg_start();
6079 }
6080 }
6081 else if (with_space)
6082 msg_puts_attr(" ", echo_attr);
6083
6084 if (p != NULL)
6085 for ( ; *p != NUL && !got_int; ++p)
6086 {
6087 if (*p == '\n' || *p == '\r' || *p == TAB)
6088 {
6089 if (*p != TAB && *needclr)
6090 {
6091 // remove any text still there from the command
6092 msg_clr_eos();
6093 *needclr = FALSE;
6094 }
6095 msg_putchar_attr(*p, echo_attr);
6096 }
6097 else
6098 {
6099 if (has_mbyte)
6100 {
6101 int i = (*mb_ptr2len)(p);
6102
6103 (void)msg_outtrans_len_attr(p, i, echo_attr);
6104 p += i - 1;
6105 }
6106 else
6107 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6108 }
6109 }
6110 vim_free(tofree);
6111}
6112
Bram Moolenaare9a41262005-01-15 22:18:47 +00006113/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 * ":echo expr1 ..." print each argument separated with a space, add a
6115 * newline at the end.
6116 * ":echon expr1 ..." print each argument plain.
6117 */
6118 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006119ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120{
6121 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006122 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006123 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 int needclr = TRUE;
6125 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006126 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006127 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006128 evalarg_T evalarg;
6129
Bram Moolenaare707c882020-07-01 13:07:37 +02006130 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131
6132 if (eap->skip)
6133 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006134 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006136 // If eval1() causes an error message the text from the command may
6137 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006138 need_clr_eos = needclr;
6139
Bram Moolenaar68db9962021-05-09 23:19:22 +02006140 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006141 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 {
6143 /*
6144 * Report the invalid expression unless the expression evaluation
6145 * has been cancelled due to an aborting error, an interrupt, or an
6146 * exception.
6147 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006148 if (!aborting() && did_emsg == did_emsg_before
6149 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006150 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006151 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 break;
6153 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006154 need_clr_eos = FALSE;
6155
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006157 {
6158 if (rettv.v_type == VAR_VOID)
6159 {
6160 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6161 break;
6162 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006163 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006164 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006165
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006166 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 arg = skipwhite(arg);
6168 }
6169 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006170 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171
6172 if (eap->skip)
6173 --emsg_skip;
6174 else
6175 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006176 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 if (needclr)
6178 msg_clr_eos();
6179 if (eap->cmdidx == CMD_echo)
6180 msg_end();
6181 }
6182}
6183
6184/*
6185 * ":echohl {name}".
6186 */
6187 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006188ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006190 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191}
6192
6193/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006194 * Returns the :echo attribute
6195 */
6196 int
6197get_echo_attr(void)
6198{
6199 return echo_attr;
6200}
6201
6202/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 * ":execute expr1 ..." execute the result of an expression.
6204 * ":echomsg expr1 ..." Print a message
6205 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006206 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 * Each gets spaces around each argument and a newline at the end for
6208 * echo commands
6209 */
6210 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006211ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212{
6213 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006214 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 int ret = OK;
6216 char_u *p;
6217 garray_T ga;
6218 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006219 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220
6221 ga_init2(&ga, 1, 80);
6222
6223 if (eap->skip)
6224 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006225 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006227 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006228 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230
6231 if (!eap->skip)
6232 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006233 char_u buf[NUMBUFLEN];
6234
6235 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006236 {
6237 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6238 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006239 semsg(_(e_using_invalid_value_as_string_str),
6240 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006241 p = NULL;
6242 }
6243 else
6244 p = tv_get_string_buf(&rettv, buf);
6245 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006246 else
6247 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006248 if (p == NULL)
6249 {
6250 clear_tv(&rettv);
6251 ret = FAIL;
6252 break;
6253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 len = (int)STRLEN(p);
6255 if (ga_grow(&ga, len + 2) == FAIL)
6256 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006257 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 ret = FAIL;
6259 break;
6260 }
6261 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 ga.ga_len += len;
6265 }
6266
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006267 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 arg = skipwhite(arg);
6269 }
6270
6271 if (ret != FAIL && ga.ga_data != NULL)
6272 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006273 // use the first line of continuation lines for messages
6274 SOURCING_LNUM = start_lnum;
6275
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006276 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6277 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006278 // Mark the already saved text as finishing the line, so that what
6279 // follows is displayed on a new line when scrolling back at the
6280 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006281 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006282 }
6283
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006285 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006286 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006287 out_flush();
6288 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006289 else if (eap->cmdidx == CMD_echoconsole)
6290 {
6291 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6292 ui_write((char_u *)"\r\n", 2, TRUE);
6293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 else if (eap->cmdidx == CMD_echoerr)
6295 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006296 int save_did_emsg = did_emsg;
6297
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006298 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006299 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 if (!force_abort)
6301 did_emsg = save_did_emsg;
6302 }
6303 else if (eap->cmdidx == CMD_execute)
6304 do_cmdline((char_u *)ga.ga_data,
6305 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6306 }
6307
6308 ga_clear(&ga);
6309
6310 if (eap->skip)
6311 --emsg_skip;
6312
6313 eap->nextcmd = check_nextcmd(arg);
6314}
6315
6316/*
6317 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6318 * "arg" points to the "&" or '+' when called, to "option" when returning.
6319 * Returns NULL when no option name found. Otherwise pointer to the char
6320 * after the option name.
6321 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006322 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006323find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324{
6325 char_u *p = *arg;
6326
6327 ++p;
6328 if (*p == 'g' && p[1] == ':')
6329 {
6330 *opt_flags = OPT_GLOBAL;
6331 p += 2;
6332 }
6333 else if (*p == 'l' && p[1] == ':')
6334 {
6335 *opt_flags = OPT_LOCAL;
6336 p += 2;
6337 }
6338 else
6339 *opt_flags = 0;
6340
6341 if (!ASCII_ISALPHA(*p))
6342 return NULL;
6343 *arg = p;
6344
6345 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006346 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 else
6348 while (ASCII_ISALPHA(*p))
6349 ++p;
6350 return p;
6351}
6352
6353/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006354 * Display script name where an item was last set.
6355 * Should only be invoked when 'verbose' is non-zero.
6356 */
6357 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006358last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006359{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006360 char_u *p;
6361
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006362 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006363 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006364 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006365 if (p != NULL)
6366 {
6367 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006368 msg_puts(_("\n\tLast set from "));
6369 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006370 if (script_ctx.sc_lnum > 0)
6371 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006372 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006373 msg_outnum((long)script_ctx.sc_lnum);
6374 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006375 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006376 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006377 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006378 }
6379}
6380
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006381#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382
6383/*
6384 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006385 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 * "flags" can be "g" to do a global substitute.
6387 * Returns an allocated string, NULL for error.
6388 */
6389 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006390do_string_sub(
6391 char_u *str,
6392 char_u *pat,
6393 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006394 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006395 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396{
6397 int sublen;
6398 regmatch_T regmatch;
6399 int i;
6400 int do_all;
6401 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006402 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 garray_T ga;
6404 char_u *ret;
6405 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006406 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006408 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006410 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411
6412 ga_init2(&ga, 1, 200);
6413
6414 do_all = (flags[0] == 'g');
6415
6416 regmatch.rm_ic = p_ic;
6417 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6418 if (regmatch.regprog != NULL)
6419 {
6420 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006421 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6423 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006424 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006425 if (regmatch.startp[0] == regmatch.endp[0])
6426 {
6427 if (zero_width == regmatch.startp[0])
6428 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006429 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006430 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006431 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6432 (size_t)i);
6433 ga.ga_len += i;
6434 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006435 continue;
6436 }
6437 zero_width = regmatch.startp[0];
6438 }
6439
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 /*
6441 * Get some space for a temporary buffer to do the substitution
6442 * into. It will contain:
6443 * - The text up to where the match is.
6444 * - The substituted text.
6445 * - The text after the match.
6446 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006447 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006448 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6450 {
6451 ga_clear(&ga);
6452 break;
6453 }
6454
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006455 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 i = (int)(regmatch.startp[0] - tail);
6457 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006458 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006459 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 + ga.ga_len + i, TRUE, TRUE, FALSE);
6461 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006462 tail = regmatch.endp[0];
6463 if (*tail == NUL)
6464 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 if (!do_all)
6466 break;
6467 }
6468
6469 if (ga.ga_data != NULL)
6470 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6471
Bram Moolenaar473de612013-06-08 18:19:48 +02006472 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 }
6474
6475 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6476 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006477 if (p_cpo == empty_option)
6478 p_cpo = save_cpo;
6479 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006480 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006481 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006482 // If it's still empty it was changed and restored, need to restore in
6483 // the complicated way.
6484 if (*p_cpo == NUL)
6485 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006486 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488
6489 return ret;
6490}