blob: 3688176ab616848660d80ae2dd5ceed31c643c31 [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;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200149 if (eap != NULL)
Bram Moolenaar37c83712020-06-30 21:18:36 +0200150 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200151 evalarg->eval_cstack = eap->cstack;
152 if (getline_equal(eap->getline, eap->cookie, getsourceline))
153 {
154 evalarg->eval_getline = eap->getline;
155 evalarg->eval_cookie = eap->cookie;
156 }
Bram Moolenaar37c83712020-06-30 21:18:36 +0200157 }
158}
159
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160/*
161 * Top level evaluation function, returning a boolean.
162 * Sets "error" to TRUE if there was an error.
163 * Return TRUE or FALSE.
164 */
165 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100166eval_to_bool(
167 char_u *arg,
168 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200169 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100170 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171{
Bram Moolenaar33570922005-01-25 22:26:29 +0000172 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200173 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200174 evalarg_T evalarg;
175
Bram Moolenaar37c83712020-06-30 21:18:36 +0200176 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177
178 if (skip)
179 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200180 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 else
183 {
184 *error = FALSE;
185 if (!skip)
186 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200187 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200188 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200189 else
190 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000191 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 }
193 }
194 if (skip)
195 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200196 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200198 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199}
200
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100201/*
202 * Call eval1() and give an error message if not done at a lower level.
203 */
204 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200205eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100206{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100207 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100208 int ret;
209 int did_emsg_before = did_emsg;
210 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200211 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100212
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200213 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
214
215 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100216 if (ret == FAIL)
217 {
218 // Report the invalid expression unless the expression evaluation has
219 // been cancelled due to an aborting error, an interrupt, or an
220 // exception, or we already gave a more specific error.
221 // Also check called_emsg for when using assert_fails().
222 if (!aborting() && did_emsg == did_emsg_before
223 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200224 semsg(_(e_invalid_expression_str), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100225 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200226 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100227 return ret;
228}
229
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100230/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200231 * Return whether a typval is a valid expression to pass to eval_expr_typval()
232 * or eval_expr_to_bool(). An empty string returns FALSE;
233 */
234 int
235eval_expr_valid_arg(typval_T *tv)
236{
237 return tv->v_type != VAR_UNKNOWN
238 && (tv->v_type != VAR_STRING
239 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
240}
241
242/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100243 * Evaluate an expression, which can be a function, partial or string.
244 * Pass arguments "argv[argc]".
245 * Return the result in "rettv" and OK or FAIL.
246 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200247 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100248eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
249{
250 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100251 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200252 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100253
254 if (expr->v_type == VAR_FUNC)
255 {
256 s = expr->vval.v_string;
257 if (s == NULL || *s == NUL)
258 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200259 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200260 funcexe.evaluate = TRUE;
261 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100262 return FAIL;
263 }
264 else if (expr->v_type == VAR_PARTIAL)
265 {
266 partial_T *partial = expr->vval.v_partial;
267
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200268 if (partial == NULL)
269 return FAIL;
270
Bram Moolenaar822ba242020-05-24 23:00:18 +0200271 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200272 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100273 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200274 if (call_def_function(partial->pt_func, argc, argv,
275 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100276 return FAIL;
277 }
278 else
279 {
280 s = partial_name(partial);
281 if (s == NULL || *s == NUL)
282 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200283 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100284 funcexe.evaluate = TRUE;
285 funcexe.partial = partial;
286 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
287 return FAIL;
288 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100289 }
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200290 else if (expr->v_type == VAR_INSTR)
291 {
292 return exe_typval_instr(expr, rettv);
293 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100294 else
295 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100296 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100297 if (s == NULL)
298 return FAIL;
299 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200300 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100301 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200302 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100303 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200304 clear_tv(rettv);
Bram Moolenaar108010a2021-06-27 22:03:33 +0200305 semsg(_(e_invalid_expression_str), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100306 return FAIL;
307 }
308 }
309 return OK;
310}
311
312/*
313 * Like eval_to_bool() but using a typval_T instead of a string.
314 * Works for string, funcref and partial.
315 */
316 int
317eval_expr_to_bool(typval_T *expr, int *error)
318{
319 typval_T rettv;
320 int res;
321
322 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
323 {
324 *error = TRUE;
325 return FALSE;
326 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200327 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100328 clear_tv(&rettv);
329 return res;
330}
331
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332/*
333 * Top level evaluation function, returning a string. If "skip" is TRUE,
334 * only parsing to "nextcmd" is done, without reporting errors. Return
335 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
336 */
337 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100338eval_to_string_skip(
339 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200340 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100341 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342{
Bram Moolenaar33570922005-01-25 22:26:29 +0000343 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200345 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346
Bram Moolenaar37c83712020-06-30 21:18:36 +0200347 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 if (skip)
349 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200350 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 retval = NULL;
352 else
353 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100354 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000355 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356 }
357 if (skip)
358 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200359 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360
361 return retval;
362}
363
364/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000365 * Skip over an expression at "*pp".
366 * Return FAIL for an error, OK otherwise.
367 */
368 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200369skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000370{
Bram Moolenaar33570922005-01-25 22:26:29 +0000371 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000372
373 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200374 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000375}
376
377/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200378 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200379 * If in Vim9 script and line breaks are encountered, the lines are
380 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200381 * "arg" is advanced to just after the expression.
382 * "start" is set to the start of the expression, "end" to just after the end.
383 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200384 * Return FAIL for an error, OK otherwise.
385 */
386 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200387skip_expr_concatenate(
388 char_u **arg,
389 char_u **start,
390 char_u **end,
391 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200392{
393 typval_T rettv;
394 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200395 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200396 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200397 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200398 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200399 int evaluate = evalarg == NULL
400 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200401
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200402 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200403 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200404 {
405 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200406 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200407 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200408 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200409 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200410 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200411 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200412
413 // Don't evaluate the expression.
414 if (evalarg != NULL)
415 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200416 *arg = skipwhite(*arg);
417 res = eval1(arg, &rettv, evalarg);
418 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200419 if (evalarg != NULL)
420 evalarg->eval_flags = save_flags;
421
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200422 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200423 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200424 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200425 if (evalarg->eval_ga.ga_len == 1)
426 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200427 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200428 ga_clear(gap);
429 gap->ga_itemsize = 0;
430 }
431 else
432 {
433 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200434 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200435
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200436 // Line breaks encountered, concatenate all the lines.
437 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200438 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200439
440 // free the lines only when using getsourceline()
441 if (evalarg->eval_cookie != NULL)
442 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200443 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200444 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200445 // Do not free the last line, "arg" points into it, free it
446 // later.
447 vim_free(evalarg->eval_tofree);
448 evalarg->eval_tofree =
449 ((char_u **)gap->ga_data)[gap->ga_len - 1];
450 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200451 ga_clear_strings(gap);
452 }
453 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200454 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200455 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200456
457 // free lines that were explicitly marked for freeing
458 ga_clear_strings(freegap);
459 }
460
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200461 gap->ga_itemsize = 0;
462 if (p == NULL)
463 return FAIL;
464 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200465 vim_free(evalarg->eval_tofree_lambda);
466 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200467 // Compute "end" relative to the end.
468 *end = *start + STRLEN(*start) - endoff;
469 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200470 }
471
472 return res;
473}
474
475/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100476 * Convert "tv" to a string.
477 * When "convert" is TRUE convert a List into a sequence of lines and convert
478 * a Float to a String.
479 * Returns an allocated string (NULL when out of memory).
480 */
481 char_u *
482typval2string(typval_T *tv, int convert)
483{
484 garray_T ga;
485 char_u *retval;
486#ifdef FEAT_FLOAT
487 char_u numbuf[NUMBUFLEN];
488#endif
489
490 if (convert && tv->v_type == VAR_LIST)
491 {
492 ga_init2(&ga, (int)sizeof(char), 80);
493 if (tv->vval.v_list != NULL)
494 {
495 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
496 if (tv->vval.v_list->lv_len > 0)
497 ga_append(&ga, NL);
498 }
499 ga_append(&ga, NUL);
500 retval = (char_u *)ga.ga_data;
501 }
502#ifdef FEAT_FLOAT
503 else if (convert && tv->v_type == VAR_FLOAT)
504 {
505 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
506 retval = vim_strsave(numbuf);
507 }
508#endif
509 else
510 retval = vim_strsave(tv_get_string(tv));
511 return retval;
512}
513
514/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200515 * Top level evaluation function, returning a string. Does not handle line
516 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000517 * When "convert" is TRUE convert a List into a sequence of lines and convert
518 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 * Return pointer to allocated memory, or NULL for failure.
520 */
521 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100522eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100523 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100524 int convert,
525 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526{
Bram Moolenaar33570922005-01-25 22:26:29 +0000527 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100529 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100531 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
532 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 retval = NULL;
534 else
535 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100536 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000537 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100539 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540
541 return retval;
542}
543
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100544 char_u *
545eval_to_string(
546 char_u *arg,
547 int convert)
548{
549 return eval_to_string_eap(arg, convert, NULL);
550}
551
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000553 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200554 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200555 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 */
557 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100558eval_to_string_safe(
559 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100560 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561{
562 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200563 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200564 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200566 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200567 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000568 if (use_sandbox)
569 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200570 ++textwinlock;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200571 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000572 if (use_sandbox)
573 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200574 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200575 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200576 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 return retval;
578}
579
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580/*
581 * Top level evaluation function, returning a number.
582 * Evaluates "expr" silently.
583 * Returns -1 for an error.
584 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200585 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100586eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587{
Bram Moolenaar33570922005-01-25 22:26:29 +0000588 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200589 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000590 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591
592 ++emsg_off;
593
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200594 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 retval = -1;
596 else
597 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100598 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000599 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 }
601 --emsg_off;
602
603 return retval;
604}
605
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000606/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000607 * Top level evaluation function.
608 * Returns an allocated typval_T with the result.
609 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000610 */
611 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200612eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000613{
614 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200615 evalarg_T evalarg;
616
617 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000618
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200619 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200620 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100621 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000622
Bram Moolenaar37c83712020-06-30 21:18:36 +0200623 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000624 return tv;
625}
626
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100628 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200629 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
630 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000631 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200633 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100634call_vim_function(
635 char_u *func,
636 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200637 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200638 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000640 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200641 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100643 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200644 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200645 funcexe.firstline = curwin->w_cursor.lnum;
646 funcexe.lastline = curwin->w_cursor.lnum;
647 funcexe.evaluate = TRUE;
648 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000649 if (ret == FAIL)
650 clear_tv(rettv);
651
652 return ret;
653}
654
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100655/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100656 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100657 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200658 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
659 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100660 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200661 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100662call_func_retnr(
663 char_u *func,
664 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200665 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100666{
667 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200668 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100669
Bram Moolenaarded27a12018-08-01 19:06:03 +0200670 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100671 return -1;
672
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100673 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100674 clear_tv(&rettv);
675 return retval;
676}
677
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000678/*
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100679 * Call Vim script function like call_func_retnr() and drop the result.
680 * Returns FAIL when calling the function fails.
681 */
682 int
683call_func_noret(
684 char_u *func,
685 int argc,
686 typval_T *argv)
687{
688 typval_T rettv;
689
690 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
691 return FAIL;
692 clear_tv(&rettv);
693 return OK;
694}
695
696/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100697 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100698 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000699 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000700 */
701 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100702call_func_retstr(
703 char_u *func,
704 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200705 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000706{
707 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000708 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000709
Bram Moolenaarded27a12018-08-01 19:06:03 +0200710 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000711 return NULL;
712
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100713 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000714 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 return retval;
716}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000717
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000718/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100719 * Call Vim script function "func" and return the result as a List.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100720 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000721 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000722 */
723 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100724call_func_retlist(
725 char_u *func,
726 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200727 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000728{
729 typval_T rettv;
730
Bram Moolenaarded27a12018-08-01 19:06:03 +0200731 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000732 return NULL;
733
734 if (rettv.v_type != VAR_LIST)
735 {
736 clear_tv(&rettv);
737 return NULL;
738 }
739
740 return rettv.vval.v_list;
741}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743#ifdef FEAT_FOLDING
744/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100745 * Evaluate "arg", which is 'foldexpr'.
746 * Note: caller must set "curwin" to match "arg".
747 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
748 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 */
750 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100751eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752{
Bram Moolenaar33570922005-01-25 22:26:29 +0000753 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200754 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000756 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
757 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758
759 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000760 if (use_sandbox)
761 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200762 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200764 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 retval = 0;
766 else
767 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100768 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000769 if (tv.v_type == VAR_NUMBER)
770 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000771 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 retval = 0;
773 else
774 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100775 // If the result is a string, check if there is a non-digit before
776 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000777 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 if (!VIM_ISDIGIT(*s) && *s != '-')
779 *cp = *s++;
780 retval = atol((char *)s);
781 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000782 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 }
784 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000785 if (use_sandbox)
786 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200787 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200788 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200790 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791}
792#endif
793
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000795 * Get an lval: variable, Dict item or List item that can be assigned a value
796 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
797 * "name.key", "name.key[expr]" etc.
798 * Indexing only works if "name" is an existing List or Dictionary.
799 * "name" points to the start of the name.
800 * If "rettv" is not NULL it points to the value to be assigned.
801 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
802 * wrong; must end in space or cmd separator.
803 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100804 * flags:
805 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100806 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100807 * GLV_NO_AUTOLOAD: do not use script autoloading
808 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000809 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000810 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000811 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000812 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200813 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100814get_lval(
815 char_u *name,
816 typval_T *rettv,
817 lval_T *lp,
818 int unlet,
819 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100820 int flags, // GLV_ values
821 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000822{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000823 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000824 char_u *expr_start, *expr_end;
825 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000826 dictitem_T *v;
827 typval_T var1;
828 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000829 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000830 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000831 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000832 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100833 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100834 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100835 int writing;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000836
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100837 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200838 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000839
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100840 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000841 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100842 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000843 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200844 lp->ll_name_end = find_name_end(name, NULL, NULL,
845 FNE_INCL_BR | fne_flags);
846 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000847 }
848
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100849 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000850 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100851 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000852 if (expr_start != NULL)
853 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100854 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100855 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000856 && *p != '[' && *p != '.')
857 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200858 semsg(_(e_trailing_arg), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000859 return NULL;
860 }
861
862 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
863 if (lp->ll_exp_name == NULL)
864 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100865 // Report an invalid expression in braces, unless the
866 // expression evaluation has been cancelled due to an
867 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000868 if (!aborting() && !quiet)
869 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000870 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100871 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000872 return NULL;
873 }
874 }
875 lp->ll_name = lp->ll_exp_name;
876 }
877 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100878 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000879 lp->ll_name = name;
880
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200881 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100882 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200883 // "a: type" is declaring variable "a" with a type, not "a:".
884 if (p == name + 2 && p[-1] == ':')
885 {
886 --p;
887 lp->ll_name_end = p;
888 }
889 if (*p == ':')
890 {
891 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
892 char_u *tp = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100893
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200894 // parse the type after the name
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100895 lp->ll_type = parse_type(&tp, &si->sn_type_list, !quiet);
896 if (lp->ll_type == NULL && !quiet)
897 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200898 lp->ll_name_end = tp;
899 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100900 }
901 }
902
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100903 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000904 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
905 return p;
906
907 cc = *p;
908 *p = NUL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100909 // When we would write to the variable pass &ht and prevent autoload.
910 writing = !(flags & GLV_READ_ONLY);
911 v = find_var(lp->ll_name, writing ? &ht : NULL,
912 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000913 if (v == NULL && !quiet)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200914 semsg(_(e_undefined_variable_str), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000915 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000916 if (v == NULL)
917 return NULL;
918
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +0100919 if (in_vim9script() && (flags & GLV_NO_DECL) == 0)
920 {
921 if (!quiet)
922 semsg(_(e_variable_already_declared), lp->ll_name);
923 return NULL;
924 }
925
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000926 /*
927 * Loop until no more [idx] or .key is following.
928 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000929 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100930 var1.v_type = VAR_UNKNOWN;
931 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +0200932 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000933 {
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +0200934 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
935 {
936 if (!quiet)
937 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
938 return NULL;
939 }
Bram Moolenaare65081d2021-06-27 15:04:05 +0200940 if (lp->ll_tv->v_type != VAR_LIST
941 && lp->ll_tv->v_type != VAR_DICT
942 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000943 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000944 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100945 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000946 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000947 }
Bram Moolenaare65081d2021-06-27 15:04:05 +0200948
949 // a NULL list/blob works like an empty list/blob, allocate one now.
950 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
951 rettv_list_alloc(lp->ll_tv);
952 else if (lp->ll_tv->v_type == VAR_BLOB
953 && lp->ll_tv->vval.v_blob == NULL)
954 rettv_blob_alloc(lp->ll_tv);
955
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000956 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000957 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000958 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100959 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000960 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000961 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000962
Bram Moolenaar10c65862020-10-08 21:16:42 +0200963 if (in_vim9script() && lp->ll_valtype == NULL
964 && lp->ll_tv == &v->di_tv
965 && ht != NULL && ht == get_script_local_ht())
966 {
Bram Moolenaarc967d572021-07-08 21:38:50 +0200967 svar_T *sv = find_typval_in_script(lp->ll_tv);
Bram Moolenaar10c65862020-10-08 21:16:42 +0200968
969 // Vim9 script local variable: get the type
970 if (sv != NULL)
971 lp->ll_valtype = sv->sv_type;
972 }
973
Bram Moolenaar8c711452005-01-14 21:53:12 +0000974 len = -1;
975 if (*p == '.')
976 {
977 key = p + 1;
978 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
979 ;
980 if (len == 0)
981 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000982 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100983 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000984 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000985 }
986 p = key + len;
987 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000988 else
989 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100990 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000991 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000992 if (*p == ':')
993 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000994 else
995 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000996 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200997 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000998 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100999 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001000 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001001 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001002 clear_tv(&var1);
1003 return NULL;
1004 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001005 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001006 }
1007
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001008 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001009 if (*p == ':')
1010 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001011 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001012 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001013 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001014 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001015 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001016 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001017 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001018 if (rettv != NULL
1019 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001020 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001021 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001022 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001023 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001024 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001025 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001026 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001027 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001028 }
1029 p = skipwhite(p + 1);
1030 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001031 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001032 else
1033 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001034 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001035 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001036 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001037 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001038 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001039 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001040 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001041 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001042 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001043 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001044 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001045 clear_tv(&var2);
1046 return NULL;
1047 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001048 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001049 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001050 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001051 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001052 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001053
Bram Moolenaar8c711452005-01-14 21:53:12 +00001054 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001055 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001056 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001057 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001058 clear_tv(&var1);
1059 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001060 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001061 }
1062
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001063 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001064 ++p;
1065 }
1066
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001067 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001068 {
1069 if (len == -1)
1070 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001071 // "[key]": get key from "var1"
1072 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001073 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001074 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001075 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001076 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001077 }
1078 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001079 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001080
1081 // a NULL dict is equivalent with an empty dict
1082 if (lp->ll_tv->vval.v_dict == NULL)
1083 {
1084 lp->ll_tv->vval.v_dict = dict_alloc();
1085 if (lp->ll_tv->vval.v_dict == NULL)
1086 {
1087 clear_tv(&var1);
1088 return NULL;
1089 }
1090 ++lp->ll_tv->vval.v_dict->dv_refcount;
1091 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001092 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001093
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001094 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001095
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001096 // When assigning to a scope dictionary check that a function and
1097 // variable name is valid (only variable name unless it is l: or
1098 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001099 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001100 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001101 int prevval;
1102 int wrong;
1103
1104 if (len != -1)
1105 {
1106 prevval = key[len];
1107 key[len] = NUL;
1108 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001109 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001110 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001111 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1112 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001113 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar03290b82020-12-19 16:30:44 +01001114 || !valid_varname(key, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001115 if (len != -1)
1116 key[len] = prevval;
1117 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001118 {
1119 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001120 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001121 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001122 }
1123
Bram Moolenaar10c65862020-10-08 21:16:42 +02001124 if (lp->ll_valtype != NULL)
1125 // use the type of the member
1126 lp->ll_valtype = lp->ll_valtype->tt_member;
1127
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001128 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001129 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001130 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001131 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001132 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001133 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001134 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001135 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001136 return NULL;
1137 }
1138
Bram Moolenaar31b81602019-02-10 22:14:27 +01001139 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001140 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001141 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001142 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001143 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001144 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001145 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001146 }
1147 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001148 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001149 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001150 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001151 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001152 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001153 p = NULL;
1154 break;
1155 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001156 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001157 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001158 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1159 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001160 {
1161 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001162 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001163 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001164
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001165 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001166 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001167 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001168 else if (lp->ll_tv->v_type == VAR_BLOB)
1169 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001170 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1171
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001172 /*
1173 * Get the number and item for the only or first index of the List.
1174 */
1175 if (empty1)
1176 lp->ll_n1 = 0;
1177 else
1178 // is number or string
1179 lp->ll_n1 = (long)tv_get_number(&var1);
1180 clear_tv(&var1);
1181
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001182 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001183 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001184 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001185 return NULL;
1186 }
1187 if (lp->ll_range && !lp->ll_empty2)
1188 {
1189 lp->ll_n2 = (long)tv_get_number(&var2);
1190 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001191 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1192 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001193 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001194 }
1195 lp->ll_blob = lp->ll_tv->vval.v_blob;
1196 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001197 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001198 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001199 else
1200 {
1201 /*
1202 * Get the number and item for the only or first index of the List.
1203 */
1204 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001205 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001206 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001207 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001208 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001209 clear_tv(&var1);
1210
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001211 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001212 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001213 lp->ll_li = list_find_index(lp->ll_list, &lp->ll_n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001214 if (lp->ll_li == NULL)
1215 {
Bram Moolenaare65081d2021-06-27 15:04:05 +02001216 // Vim9: Allow for adding an item at the end.
1217 if (in_vim9script() && lp->ll_n1 == lp->ll_list->lv_len
1218 && lp->ll_list->lv_lock == 0)
1219 {
1220 list_append_number(lp->ll_list, 0);
1221 lp->ll_li = list_find_index(lp->ll_list, &lp->ll_n1);
1222 }
1223 if (lp->ll_li == NULL)
1224 {
1225 clear_tv(&var2);
1226 if (!quiet)
1227 semsg(_(e_listidx), lp->ll_n1);
1228 return NULL;
1229 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001230 }
1231
Bram Moolenaar10c65862020-10-08 21:16:42 +02001232 if (lp->ll_valtype != NULL)
1233 // use the type of the member
1234 lp->ll_valtype = lp->ll_valtype->tt_member;
1235
Bram Moolenaar8c711452005-01-14 21:53:12 +00001236 /*
1237 * May need to find the item or absolute index for the second
1238 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001239 * When no index given: "lp->ll_empty2" is TRUE.
1240 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001241 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001242 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001243 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001244 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001245 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001246 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001247 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001248 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001249 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001250 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001251 {
1252 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001253 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001254 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001255 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001256 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001257 }
1258
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001259 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001260 if (lp->ll_n1 < 0)
1261 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1262 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001263 {
1264 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001265 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001266 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001267 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001268 }
1269
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001270 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001271 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001272 }
1273
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001274 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001275 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001276 return p;
1277}
1278
1279/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001280 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001281 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001282 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001283clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001284{
1285 vim_free(lp->ll_exp_name);
1286 vim_free(lp->ll_newkey);
1287}
1288
1289/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001290 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001291 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001292 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1293 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001294 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001295 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001296set_var_lval(
1297 lval_T *lp,
1298 char_u *endp,
1299 typval_T *rettv,
1300 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001301 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1302 char_u *op,
1303 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001304{
1305 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001306 listitem_T *ri;
1307 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001308
1309 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001310 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001311 cc = *endp;
1312 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001313 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1314 return;
1315
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001316 if (lp->ll_blob != NULL)
1317 {
1318 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001319
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001320 if (op != NULL && *op != '=')
1321 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001322 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001323 return;
1324 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001325 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001326 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001327
1328 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1329 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001330 if (lp->ll_empty2)
1331 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1332
Bram Moolenaar68452172021-04-12 21:21:02 +02001333 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1334 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001335 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001336 }
1337 else
1338 {
1339 val = (int)tv_get_number_chk(rettv, &error);
1340 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001341 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001342 }
1343 }
1344 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001345 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001346 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001347
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001348 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1349 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001350 {
1351 emsg(_(e_cannot_mod));
1352 *endp = cc;
1353 return;
1354 }
1355
Bram Moolenaarff697e62019-02-12 22:28:33 +01001356 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001357 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001358 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001359 &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001360 {
1361 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001362 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1363 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001364 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar24e93162021-07-18 20:40:33 +02001365 set_var_const(lp->ll_name, NULL, &tv, FALSE,
1366 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001367 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001368 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001369 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001370 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001371 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001372 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1373 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001374 return;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001375 set_var_const(lp->ll_name, lp->ll_type, rettv, copy,
1376 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001377 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001378 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001379 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001380 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001381 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001382 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001383 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001384 else if (lp->ll_range)
1385 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001386 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001387 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001388
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001389 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1390 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001391 {
1392 emsg(_("E996: Cannot lock a range"));
1393 return;
1394 }
1395
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001396 /*
1397 * Check whether any of the list items is locked
1398 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001399 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001400 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001401 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001402 return;
1403 ri = ri->li_next;
1404 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1405 break;
1406 ll_li = ll_li->li_next;
1407 ++ll_n1;
1408 }
1409
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001410 /*
1411 * Assign the List values to the list items.
1412 */
1413 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001414 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001415 if (op != NULL && *op != '=')
1416 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1417 else
1418 {
1419 clear_tv(&lp->ll_li->li_tv);
1420 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1421 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001422 ri = ri->li_next;
1423 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1424 break;
1425 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001426 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001427 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001428 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001429 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001430 ri = NULL;
1431 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001432 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001433 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001434 lp->ll_li = lp->ll_li->li_next;
1435 ++lp->ll_n1;
1436 }
1437 if (ri != NULL)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001438 emsg(_(e_list_value_has_more_items_than_targets));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001439 else if (lp->ll_empty2
1440 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001441 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001442 emsg(_(e_list_value_does_not_have_enough_items));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001443 }
1444 else
1445 {
1446 /*
1447 * Assign to a List or Dictionary item.
1448 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001449 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1450 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001451 {
1452 emsg(_("E996: Cannot lock a list or dict"));
1453 return;
1454 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001455
1456 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001457 && check_typval_arg_type(lp->ll_valtype, rettv,
1458 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001459 return;
1460
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001461 if (lp->ll_newkey != NULL)
1462 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001463 if (op != NULL && *op != '=')
1464 {
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02001465 semsg(_(e_dictkey), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001466 return;
1467 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001468 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1469 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001470 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001471
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001472 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001473 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001474 if (di == NULL)
1475 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001476 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1477 {
1478 vim_free(di);
1479 return;
1480 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001481 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001482 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001483 else if (op != NULL && *op != '=')
1484 {
1485 tv_op(lp->ll_tv, rettv, op);
1486 return;
1487 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001488 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001489 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001490
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001491 /*
1492 * Assign the value to the variable or list item.
1493 */
1494 if (copy)
1495 copy_tv(rettv, lp->ll_tv);
1496 else
1497 {
1498 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001499 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001500 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001501 }
1502 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001503}
1504
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001505/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001506 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1507 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001508 * Returns OK or FAIL.
1509 */
1510 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001511tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001512{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001513 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001514 char_u numbuf[NUMBUFLEN];
1515 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001516 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001517
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001518 // Can't do anything with a Funcref or Dict on the right.
1519 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001520 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001521 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1522 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001523 {
1524 switch (tv1->v_type)
1525 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001526 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001527 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001528 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001529 case VAR_DICT:
1530 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001531 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001532 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001533 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001534 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001535 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001536 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001537 break;
1538
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001539 case VAR_BLOB:
1540 if (*op != '+' || tv2->v_type != VAR_BLOB)
1541 break;
1542 // BLOB += BLOB
1543 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1544 {
1545 blob_T *b1 = tv1->vval.v_blob;
1546 blob_T *b2 = tv2->vval.v_blob;
1547 int i, len = blob_len(b2);
1548 for (i = 0; i < len; i++)
1549 ga_append(&b1->bv_ga, blob_get(b2, i));
1550 }
1551 return OK;
1552
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001553 case VAR_LIST:
1554 if (*op != '+' || tv2->v_type != VAR_LIST)
1555 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001556 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001557 if (tv2->vval.v_list != NULL)
1558 {
1559 if (tv1->vval.v_list == NULL)
1560 {
1561 tv1->vval.v_list = tv2->vval.v_list;
1562 ++tv1->vval.v_list->lv_refcount;
1563 }
1564 else
1565 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1566 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001567 return OK;
1568
1569 case VAR_NUMBER:
1570 case VAR_STRING:
1571 if (tv2->v_type == VAR_LIST)
1572 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001573 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001574 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001575 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001576 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001577#ifdef FEAT_FLOAT
1578 if (tv2->v_type == VAR_FLOAT)
1579 {
1580 float_T f = n;
1581
Bram Moolenaarff697e62019-02-12 22:28:33 +01001582 if (*op == '%')
1583 break;
1584 switch (*op)
1585 {
1586 case '+': f += tv2->vval.v_float; break;
1587 case '-': f -= tv2->vval.v_float; break;
1588 case '*': f *= tv2->vval.v_float; break;
1589 case '/': f /= tv2->vval.v_float; break;
1590 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001591 clear_tv(tv1);
1592 tv1->v_type = VAR_FLOAT;
1593 tv1->vval.v_float = f;
1594 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001595 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001596#endif
1597 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001598 switch (*op)
1599 {
1600 case '+': n += tv_get_number(tv2); break;
1601 case '-': n -= tv_get_number(tv2); break;
1602 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001603 case '/': n = num_divide(n, tv_get_number(tv2),
1604 &failed); break;
1605 case '%': n = num_modulus(n, tv_get_number(tv2),
1606 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001607 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001608 clear_tv(tv1);
1609 tv1->v_type = VAR_NUMBER;
1610 tv1->vval.v_number = n;
1611 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001612 }
1613 else
1614 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001615 if (tv2->v_type == VAR_FLOAT)
1616 break;
1617
Bram Moolenaarff697e62019-02-12 22:28:33 +01001618 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001619 s = tv_get_string(tv1);
1620 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001621 clear_tv(tv1);
1622 tv1->v_type = VAR_STRING;
1623 tv1->vval.v_string = s;
1624 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001625 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001626
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001627 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001628#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001629 {
1630 float_T f;
1631
Bram Moolenaarff697e62019-02-12 22:28:33 +01001632 if (*op == '%' || *op == '.'
1633 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001634 && tv2->v_type != VAR_NUMBER
1635 && tv2->v_type != VAR_STRING))
1636 break;
1637 if (tv2->v_type == VAR_FLOAT)
1638 f = tv2->vval.v_float;
1639 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001640 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001641 switch (*op)
1642 {
1643 case '+': tv1->vval.v_float += f; break;
1644 case '-': tv1->vval.v_float -= f; break;
1645 case '*': tv1->vval.v_float *= f; break;
1646 case '/': tv1->vval.v_float /= f; break;
1647 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001648 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001649#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001650 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001651 }
1652 }
1653
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001654 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001655 return FAIL;
1656}
1657
1658/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001659 * Evaluate the expression used in a ":for var in expr" command.
1660 * "arg" points to "var".
1661 * Set "*errp" to TRUE for an error, FALSE otherwise;
1662 * Return a pointer that holds the info. Null when there is an error.
1663 */
1664 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001665eval_for_line(
1666 char_u *arg,
1667 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001668 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001669 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001670{
Bram Moolenaar33570922005-01-25 22:26:29 +00001671 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001672 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001673 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001674 typval_T tv;
1675 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001676 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001677
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001678 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001679
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001680 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001681 if (fi == NULL)
1682 return NULL;
1683
Bram Moolenaar404557e2021-07-05 21:41:48 +02001684 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1685 &fi->fi_semicolon, FALSE);
1686 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001687 return fi;
1688
Bram Moolenaar404557e2021-07-05 21:41:48 +02001689 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001690 if (expr[0] != 'i' || expr[1] != 'n'
1691 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001692 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001693 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1694 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1695 else
1696 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001697 return fi;
1698 }
1699
1700 if (skip)
1701 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001702 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1703 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001704 {
1705 *errp = FALSE;
1706 if (!skip)
1707 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001708 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001709 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001710 l = tv.vval.v_list;
1711 if (l == NULL)
1712 {
1713 // a null list is like an empty list: do nothing
1714 clear_tv(&tv);
1715 }
1716 else
1717 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001718 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001719 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001720
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001721 // No need to increment the refcount, it's already set for
1722 // the list being used in "tv".
1723 fi->fi_list = l;
1724 list_add_watch(l, &fi->fi_lw);
1725 fi->fi_lw.lw_item = l->lv_first;
1726 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001727 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001728 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001729 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001730 fi->fi_bi = 0;
1731 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001732 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001733 typval_T btv;
1734
1735 // Make a copy, so that the iteration still works when the
1736 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001737 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001738 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001739 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001740 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001741 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001742 else if (tv.v_type == VAR_STRING)
1743 {
1744 fi->fi_byte_idx = 0;
1745 fi->fi_string = tv.vval.v_string;
1746 tv.vval.v_string = NULL;
1747 if (fi->fi_string == NULL)
1748 fi->fi_string = vim_strsave((char_u *)"");
1749 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001750 else
1751 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001752 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001753 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001754 }
1755 }
1756 }
1757 if (skip)
1758 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001759 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001760
1761 return fi;
1762}
1763
1764/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001765 * Used when looping over a :for line, skip the "in expr" part.
1766 */
1767 void
1768skip_for_lines(void *fi_void, evalarg_T *evalarg)
1769{
1770 forinfo_T *fi = (forinfo_T *)fi_void;
1771 int i;
1772
1773 for (i = 0; i < fi->fi_break_count; ++i)
1774 eval_next_line(evalarg);
1775}
1776
1777/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001778 * Use the first item in a ":for" list. Advance to the next.
1779 * Assign the values to the variable (list). "arg" points to the first one.
1780 * Return TRUE when a valid item was found, FALSE when at end of list or
1781 * something wrong.
1782 */
1783 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001784next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001785{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001786 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001787 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001788 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001789 ? (ASSIGN_FINAL
1790 // first round: error if variable exists
1791 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
1792 | ASSIGN_NO_MEMBER_TYPE)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001793 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001794 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001795 int skip_assign = in_vim9script() && arg[0] == '_'
1796 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001797
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001798 if (fi->fi_blob != NULL)
1799 {
1800 typval_T tv;
1801
1802 if (fi->fi_bi >= blob_len(fi->fi_blob))
1803 return FALSE;
1804 tv.v_type = VAR_NUMBER;
1805 tv.v_lock = VAR_FIXED;
1806 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1807 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001808 if (skip_assign)
1809 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001810 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001811 fi->fi_varcount, flag, NULL) == OK;
1812 }
1813
1814 if (fi->fi_string != NULL)
1815 {
1816 typval_T tv;
1817 int len;
1818
1819 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1820 if (len == 0)
1821 return FALSE;
1822 tv.v_type = VAR_STRING;
1823 tv.v_lock = VAR_FIXED;
1824 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1825 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001826 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001827 if (skip_assign)
1828 result = TRUE;
1829 else
1830 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001831 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001832 vim_free(tv.vval.v_string);
1833 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001834 }
1835
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001836 item = fi->fi_lw.lw_item;
1837 if (item == NULL)
1838 result = FALSE;
1839 else
1840 {
1841 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001842 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001843 if (skip_assign)
1844 result = TRUE;
1845 else
1846 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001847 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001848 }
1849 return result;
1850}
1851
1852/*
1853 * Free the structure used to store info used by ":for".
1854 */
1855 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001856free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001857{
Bram Moolenaar33570922005-01-25 22:26:29 +00001858 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001859
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001860 if (fi == NULL)
1861 return;
1862 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001863 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001864 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001865 list_unref(fi->fi_list);
1866 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001867 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001868 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001869 else
1870 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001871 vim_free(fi);
1872}
1873
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001875set_context_for_expression(
1876 expand_T *xp,
1877 char_u *arg,
1878 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001880 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001882 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001884 if (cmdidx == CMD_let || cmdidx == CMD_var
1885 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001886 {
1887 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001888 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001889 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001890 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001891 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001892 {
1893 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001894 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001895 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001896 break;
1897 }
1898 return;
1899 }
1900 }
1901 else
1902 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1903 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 while ((xp->xp_pattern = vim_strpbrk(arg,
1905 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1906 {
1907 c = *xp->xp_pattern;
1908 if (c == '&')
1909 {
1910 c = xp->xp_pattern[1];
1911 if (c == '&')
1912 {
1913 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001914 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 }
1916 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001917 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001919 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1920 xp->xp_pattern += 2;
1921
1922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 }
1924 else if (c == '$')
1925 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001926 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 xp->xp_context = EXPAND_ENV_VARS;
1928 }
1929 else if (c == '=')
1930 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001931 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 xp->xp_context = EXPAND_EXPRESSION;
1933 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001934 else if (c == '#'
1935 && xp->xp_context == EXPAND_EXPRESSION)
1936 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001937 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001938 break;
1939 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001940 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 && xp->xp_context == EXPAND_FUNCTIONS
1942 && vim_strchr(xp->xp_pattern, '(') == NULL)
1943 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001944 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 break;
1946 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001947 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001949 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 {
1951 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1952 if (c == '\\' && xp->xp_pattern[1] != NUL)
1953 ++xp->xp_pattern;
1954 xp->xp_context = EXPAND_NOTHING;
1955 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001956 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001958 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1960 /* skip */ ;
1961 xp->xp_context = EXPAND_NOTHING;
1962 }
1963 else if (c == '|')
1964 {
1965 if (xp->xp_pattern[1] == '|')
1966 {
1967 ++xp->xp_pattern;
1968 xp->xp_context = EXPAND_EXPRESSION;
1969 }
1970 else
1971 xp->xp_context = EXPAND_COMMANDS;
1972 }
1973 else
1974 xp->xp_context = EXPAND_EXPRESSION;
1975 }
1976 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001977 // Doesn't look like something valid, expand as an expression
1978 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001979 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 arg = xp->xp_pattern;
1981 if (*arg != NUL)
1982 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1983 /* skip */ ;
1984 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01001985
1986 // ":exe one two" completes "two"
1987 if ((cmdidx == CMD_execute
1988 || cmdidx == CMD_echo
1989 || cmdidx == CMD_echon
1990 || cmdidx == CMD_echomsg)
1991 && xp->xp_context == EXPAND_EXPRESSION)
1992 {
1993 for (;;)
1994 {
1995 char_u *n = skiptowhite(arg);
1996
1997 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
1998 break;
1999 arg = skipwhite(n);
2000 }
2001 }
2002
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 xp->xp_pattern = arg;
2004}
2005
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002007 * Return TRUE if "pat" matches "text".
2008 * Does not use 'cpo' and always uses 'magic'.
2009 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002010 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002011pattern_match(char_u *pat, char_u *text, int ic)
2012{
2013 int matches = FALSE;
2014 char_u *save_cpo;
2015 regmatch_T regmatch;
2016
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002017 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002018 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002019 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002020 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2021 if (regmatch.regprog != NULL)
2022 {
2023 regmatch.rm_ic = ic;
2024 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2025 vim_regfree(regmatch.regprog);
2026 }
2027 p_cpo = save_cpo;
2028 return matches;
2029}
2030
2031/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002032 * Handle a name followed by "(". Both for just "name(arg)" and for
2033 * "expr->name(arg)".
2034 * Returns OK or FAIL.
2035 */
2036 static int
2037eval_func(
2038 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002039 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002040 char_u *name,
2041 int name_len,
2042 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002043 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002044 typval_T *basetv) // "expr" for "expr->name(arg)"
2045{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002046 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002047 char_u *s = name;
2048 int len = name_len;
2049 partial_T *partial;
2050 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002051 type_T *type = NULL;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002052
2053 if (!evaluate)
2054 check_vars(s, len);
2055
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002056 // If "s" is the name of a variable of type VAR_FUNC
2057 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002058 s = deref_func_name(s, &len, &partial,
2059 in_vim9script() ? &type : NULL, !evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002060
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002061 // Need to make a copy, in case evaluating the arguments makes
2062 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002063 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002064 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002065 ret = FAIL;
2066 else
2067 {
2068 funcexe_T funcexe;
2069
2070 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002071 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002072 funcexe.firstline = curwin->w_cursor.lnum;
2073 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002074 funcexe.evaluate = evaluate;
2075 funcexe.partial = partial;
2076 funcexe.basetv = basetv;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002077 funcexe.check_type = type;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002078 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002079 }
2080 vim_free(s);
2081
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002082 // If evaluate is FALSE rettv->v_type was not set in
2083 // get_func_tv, but it's needed in handle_subscript() to parse
2084 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002085 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2086 {
2087 rettv->vval.v_string = NULL;
2088 rettv->v_type = VAR_FUNC;
2089 }
2090
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002091 // Stop the expression evaluation when immediately
2092 // aborting on error, or when an interrupt occurred or
2093 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002094 if (evaluate && aborting())
2095 {
2096 if (ret == OK)
2097 clear_tv(rettv);
2098 ret = FAIL;
2099 }
2100 return ret;
2101}
2102
2103/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002104 * Get the next line source line without advancing. But do skip over comment
2105 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002106 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002107 */
2108 static char_u *
2109getline_peek_skip_comments(evalarg_T *evalarg)
2110{
2111 for (;;)
2112 {
2113 char_u *next = getline_peek(evalarg->eval_getline,
2114 evalarg->eval_cookie);
2115 char_u *p;
2116
2117 if (next == NULL)
2118 break;
2119 p = skipwhite(next);
2120 if (*p != NUL && !vim9_comment_start(p))
2121 return next;
2122 (void)eval_next_line(evalarg);
2123 }
2124 return NULL;
2125}
2126
2127/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002128 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2129 * comment) and there is a next line, return the next line (skipping blanks)
2130 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002131 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2132 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002133 * "arg" must point somewhere inside a line, not at the start.
2134 */
Yegappan Lakshmanana2438132021-07-10 21:29:18 +02002135 static char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002136eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2137{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002138 char_u *p = skipwhite(arg);
2139
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002140 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002141 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002142 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002143 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002144 && (*p == NUL || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002145 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002146 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002147
2148 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002149 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002150 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002151 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002152
Bram Moolenaar9d489562020-07-30 20:08:50 +02002153 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002154 {
2155 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002156 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002157 }
2158 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002159 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002160}
2161
2162/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002163 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002164 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002165 */
Yegappan Lakshmanana2438132021-07-10 21:29:18 +02002166 static char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002167eval_next_line(evalarg_T *evalarg)
2168{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002169 garray_T *gap = &evalarg->eval_ga;
2170 char_u *line;
2171
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002172 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002173 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2174 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002175 else
2176 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002177 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002178 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2179 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002180 char_u *p = skipwhite(line);
2181
2182 // Going to concatenate the lines after parsing. For an empty or
2183 // comment line use an empty string.
2184 if (*p == NUL || vim9_comment_start(p))
2185 {
2186 vim_free(line);
2187 line = vim_strsave((char_u *)"");
2188 }
2189
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002190 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2191 ++gap->ga_len;
2192 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002193 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002194 {
2195 vim_free(evalarg->eval_tofree);
2196 evalarg->eval_tofree = line;
2197 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002198
2199 // Advanced to the next line, "arg" no longer points into the previous
2200 // line.
2201 VIM_CLEAR(evalarg->eval_tofree_cmdline);
2202
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002203 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002204}
2205
Bram Moolenaare6b53242020-07-01 17:28:33 +02002206/*
2207 * Call eval_next_non_blank() and get the next line if needed.
2208 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002209 char_u *
2210skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2211{
2212 int getnext;
2213 char_u *p = skipwhite(arg);
2214
Bram Moolenaar962d7212020-07-04 14:15:00 +02002215 if (evalarg == NULL)
2216 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002217 eval_next_non_blank(p, evalarg, &getnext);
2218 if (getnext)
2219 return eval_next_line(evalarg);
2220 return p;
2221}
2222
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002223/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002224 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002225 */
2226 void
2227clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2228{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002229 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002230 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002231 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002232 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002233 if (eap != NULL)
2234 {
2235 // We may need to keep the original command line, e.g. for
2236 // ":let" it has the variable names. But we may also need the
2237 // new one, "nextcmd" points into it. Keep both.
2238 vim_free(eap->cmdline_tofree);
2239 eap->cmdline_tofree = *eap->cmdlinep;
2240 *eap->cmdlinep = evalarg->eval_tofree;
2241 }
2242 else
2243 vim_free(evalarg->eval_tofree);
2244 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002245 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002246
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002247 VIM_CLEAR(evalarg->eval_tofree_cmdline);
2248 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002249 }
2250}
2251
2252/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2256 */
2257
2258/*
2259 * Handle zero level expression.
2260 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002261 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002262 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002263 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 * Return OK or FAIL.
2265 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002266 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002267eval0(
2268 char_u *arg,
2269 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002270 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002271 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272{
2273 int ret;
2274 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002275 int did_emsg_before = did_emsg;
2276 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002277 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002278 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279
2280 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002281 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002282 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002283
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002284 if (ret != FAIL)
2285 end_error = !ends_excmd2(arg, p);
2286 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 {
2288 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002289 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 /*
2291 * Report the invalid expression unless the expression evaluation has
2292 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002293 * exception, or we already gave a more specific error.
2294 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002296 if (!aborting()
2297 && did_emsg == did_emsg_before
2298 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002299 && (flags & EVAL_CONSTANT) == 0
2300 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002301 {
2302 if (end_error)
2303 semsg(_(e_trailing_arg), p);
2304 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002305 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002306 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002307
2308 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002309 // a next command to avoid more errors, unless "|" is following, which
2310 // could only be a command separator.
2311 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2312 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002313 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002315
2316 if (eap != NULL)
2317 eap->nextcmd = check_nextcmd(p);
2318
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 return ret;
2320}
2321
2322/*
2323 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002324 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002325 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 *
2327 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002328 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002330 * Note: "rettv.v_lock" is not set.
2331 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 * Return OK or FAIL.
2333 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002334 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002335eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002337 char_u *p;
2338 int getnext;
2339
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002340 CLEAR_POINTER(rettv);
2341
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 /*
2343 * Get the first variable.
2344 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002345 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 return FAIL;
2347
Bram Moolenaar793648f2020-06-26 21:28:25 +02002348 p = eval_next_non_blank(*arg, evalarg, &getnext);
2349 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002351 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002352 int result;
2353 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002354 evalarg_T *evalarg_used = evalarg;
2355 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002356 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002357 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002358 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002359
2360 if (evalarg == NULL)
2361 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002362 CLEAR_FIELD(local_evalarg);
2363 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002364 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002365 orig_flags = evalarg_used->eval_flags;
2366 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002367
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002368 if (getnext)
2369 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002370 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002371 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002372 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002373 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002374 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002375 clear_tv(rettv);
2376 return FAIL;
2377 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002378 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002379 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002380
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002382 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002384 int error = FALSE;
2385
Bram Moolenaar13106602020-10-04 16:06:05 +02002386 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002387 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002388 else if (vim9script)
2389 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002390 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002392 if (error || !op_falsy || !result)
2393 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002394 if (error)
2395 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 }
2397
2398 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002399 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002401 if (op_falsy)
2402 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002403 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002404 {
mityu4ac198c2021-05-28 17:52:40 +02002405 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002406 clear_tv(rettv);
2407 return FAIL;
2408 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002409 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002410 evalarg_used->eval_flags = (op_falsy ? !result : result)
2411 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2412 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002413 {
2414 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002416 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002417 if (!op_falsy || !result)
2418 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002420 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002422 /*
2423 * Check for the ":".
2424 */
2425 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2426 if (*p != ':')
2427 {
2428 emsg(_(e_missing_colon));
2429 if (evaluate && result)
2430 clear_tv(rettv);
2431 evalarg_used->eval_flags = orig_flags;
2432 return FAIL;
2433 }
2434 if (getnext)
2435 *arg = eval_next_line(evalarg_used);
2436 else
2437 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002438 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002439 {
2440 error_white_both(p, 1);
2441 clear_tv(rettv);
2442 evalarg_used->eval_flags = orig_flags;
2443 return FAIL;
2444 }
2445 *arg = p;
2446 }
2447
2448 /*
2449 * Get the third variable. Recursive!
2450 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002451 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002452 {
mityu4ac198c2021-05-28 17:52:40 +02002453 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002454 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002455 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002456 return FAIL;
2457 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002458 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2459 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002460 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002461 if (eval1(arg, &var2, evalarg_used) == FAIL)
2462 {
2463 if (evaluate && result)
2464 clear_tv(rettv);
2465 evalarg_used->eval_flags = orig_flags;
2466 return FAIL;
2467 }
2468 if (evaluate && !result)
2469 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002471
2472 if (evalarg == NULL)
2473 clear_evalarg(&local_evalarg, NULL);
2474 else
2475 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 }
2477
2478 return OK;
2479}
2480
2481/*
2482 * Handle first level expression:
2483 * expr2 || expr2 || expr2 logical OR
2484 *
2485 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002486 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 *
2488 * Return OK or FAIL.
2489 */
2490 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002491eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002493 char_u *p;
2494 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495
2496 /*
2497 * Get the first variable.
2498 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002499 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 return FAIL;
2501
2502 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002503 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002505 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002506 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002508 evalarg_T *evalarg_used = evalarg;
2509 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002510 int evaluate;
2511 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002512 long result = FALSE;
2513 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002514 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002515 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002516
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002517 if (evalarg == NULL)
2518 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002519 CLEAR_FIELD(local_evalarg);
2520 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002521 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002522 orig_flags = evalarg_used->eval_flags;
2523 evaluate = orig_flags & EVAL_EVALUATE;
2524 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002525 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002526 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002527 result = tv_get_bool_chk(rettv, &error);
2528 else if (tv_get_number_chk(rettv, &error) != 0)
2529 result = TRUE;
2530 clear_tv(rettv);
2531 if (error)
2532 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 }
2534
2535 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002536 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002538 while (p[0] == '|' && p[1] == '|')
2539 {
2540 if (getnext)
2541 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002542 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002543 {
2544 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2545 {
2546 error_white_both(p, 2);
2547 clear_tv(rettv);
2548 return FAIL;
2549 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002550 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002551 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002552
2553 /*
2554 * Get the second variable.
2555 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002556 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2557 {
mityu4ac198c2021-05-28 17:52:40 +02002558 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002559 clear_tv(rettv);
2560 return FAIL;
2561 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002562 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2563 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002564 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002565 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002566 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002567
2568 /*
2569 * Compute the result.
2570 */
2571 if (evaluate && !result)
2572 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002573 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002574 result = tv_get_bool_chk(&var2, &error);
2575 else if (tv_get_number_chk(&var2, &error) != 0)
2576 result = TRUE;
2577 clear_tv(&var2);
2578 if (error)
2579 return FAIL;
2580 }
2581 if (evaluate)
2582 {
2583 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002584 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002585 rettv->v_type = VAR_BOOL;
2586 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002587 }
2588 else
2589 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002590 rettv->v_type = VAR_NUMBER;
2591 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002592 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002593 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002594
2595 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002597
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002598 if (evalarg == NULL)
2599 clear_evalarg(&local_evalarg, NULL);
2600 else
2601 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 }
2603
2604 return OK;
2605}
2606
2607/*
2608 * Handle second level expression:
2609 * expr3 && expr3 && expr3 logical AND
2610 *
2611 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002612 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 *
2614 * Return OK or FAIL.
2615 */
2616 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002617eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002619 char_u *p;
2620 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621
2622 /*
2623 * Get the first variable.
2624 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002625 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 return FAIL;
2627
2628 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002629 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002631 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002632 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002634 evalarg_T *evalarg_used = evalarg;
2635 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002636 int orig_flags;
2637 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002638 long result = TRUE;
2639 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002640 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002641 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002642
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002643 if (evalarg == NULL)
2644 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002645 CLEAR_FIELD(local_evalarg);
2646 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002647 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002648 orig_flags = evalarg_used->eval_flags;
2649 evaluate = orig_flags & EVAL_EVALUATE;
2650 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002651 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002652 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002653 result = tv_get_bool_chk(rettv, &error);
2654 else if (tv_get_number_chk(rettv, &error) == 0)
2655 result = FALSE;
2656 clear_tv(rettv);
2657 if (error)
2658 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 }
2660
2661 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002662 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002664 while (p[0] == '&' && p[1] == '&')
2665 {
2666 if (getnext)
2667 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002668 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002669 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002670 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002671 {
2672 error_white_both(p, 2);
2673 clear_tv(rettv);
2674 return FAIL;
2675 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002676 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002677 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002678
2679 /*
2680 * Get the second variable.
2681 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002682 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2683 {
mityu4ac198c2021-05-28 17:52:40 +02002684 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002685 clear_tv(rettv);
2686 return FAIL;
2687 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002688 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2689 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002690 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002691 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002692 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002693 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002694
2695 /*
2696 * Compute the result.
2697 */
2698 if (evaluate && result)
2699 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002700 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002701 result = tv_get_bool_chk(&var2, &error);
2702 else if (tv_get_number_chk(&var2, &error) == 0)
2703 result = FALSE;
2704 clear_tv(&var2);
2705 if (error)
2706 return FAIL;
2707 }
2708 if (evaluate)
2709 {
2710 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002711 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002712 rettv->v_type = VAR_BOOL;
2713 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002714 }
2715 else
2716 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002717 rettv->v_type = VAR_NUMBER;
2718 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002719 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002720 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002721
2722 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002724
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002725 if (evalarg == NULL)
2726 clear_evalarg(&local_evalarg, NULL);
2727 else
2728 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 }
2730
2731 return OK;
2732}
2733
2734/*
2735 * Handle third level expression:
2736 * var1 == var2
2737 * var1 =~ var2
2738 * var1 != var2
2739 * var1 !~ var2
2740 * var1 > var2
2741 * var1 >= var2
2742 * var1 < var2
2743 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002744 * var1 is var2
2745 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 *
2747 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002748 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 *
2750 * Return OK or FAIL.
2751 */
2752 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002753eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002756 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002757 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002759 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760
2761 /*
2762 * Get the first variable.
2763 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002764 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 return FAIL;
2766
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002767 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002768 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769
2770 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002771 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002773 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002775 typval_T var2;
2776 int ic;
2777 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002778 int evaluate = evalarg == NULL
2779 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002780
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002781 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002782 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002783 *arg = eval_next_line(evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002784 p = *arg;
2785 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002786 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2787 {
mityu4ac198c2021-05-28 17:52:40 +02002788 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002789 clear_tv(rettv);
2790 return FAIL;
2791 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002792
Bram Moolenaar696ba232020-07-29 21:20:41 +02002793 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2794 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002795 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002796 clear_tv(rettv);
2797 return FAIL;
2798 }
2799
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002800 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 if (p[len] == '?')
2802 {
2803 ic = TRUE;
2804 ++len;
2805 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002806 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 else if (p[len] == '#')
2808 {
2809 ic = FALSE;
2810 ++len;
2811 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002812 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002814 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815
2816 /*
2817 * Get the second variable.
2818 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002819 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2820 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002821 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002822 clear_tv(rettv);
2823 return FAIL;
2824 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002825 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002826 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002828 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 return FAIL;
2830 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002831 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002832 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002833 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002834
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002835 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002836 {
2837 ret = FAIL;
2838 clear_tv(rettv);
2839 }
2840 else
2841 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002842 clear_tv(&var2);
2843 return ret;
2844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 }
2846
2847 return OK;
2848}
2849
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002850/*
2851 * Make a copy of blob "tv1" and append blob "tv2".
2852 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002853 void
2854eval_addblob(typval_T *tv1, typval_T *tv2)
2855{
2856 blob_T *b1 = tv1->vval.v_blob;
2857 blob_T *b2 = tv2->vval.v_blob;
2858 blob_T *b = blob_alloc();
2859 int i;
2860
2861 if (b != NULL)
2862 {
2863 for (i = 0; i < blob_len(b1); i++)
2864 ga_append(&b->bv_ga, blob_get(b1, i));
2865 for (i = 0; i < blob_len(b2); i++)
2866 ga_append(&b->bv_ga, blob_get(b2, i));
2867
2868 clear_tv(tv1);
2869 rettv_blob_set(tv1, b);
2870 }
2871}
2872
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002873/*
2874 * Make a copy of list "tv1" and append list "tv2".
2875 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002876 int
2877eval_addlist(typval_T *tv1, typval_T *tv2)
2878{
2879 typval_T var3;
2880
2881 // concatenate Lists
2882 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2883 {
2884 clear_tv(tv1);
2885 clear_tv(tv2);
2886 return FAIL;
2887 }
2888 clear_tv(tv1);
2889 *tv1 = var3;
2890 return OK;
2891}
2892
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893/*
2894 * Handle fourth level expression:
2895 * + number addition
2896 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002897 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002898 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 *
2900 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002901 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 *
2903 * Return OK or FAIL.
2904 */
2905 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002906eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 /*
2909 * Get the first variable.
2910 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002911 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 return FAIL;
2913
2914 /*
2915 * Repeat computing, until no '+', '-' or '.' is following.
2916 */
2917 for (;;)
2918 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002919 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002920 int getnext;
2921 char_u *p;
2922 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002923 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002924 int concat;
2925 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002926 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002927
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002928 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002929 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002930 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002931 p = eval_next_non_blank(*arg, evalarg, &getnext);
2932 op = *p;
2933 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002934 if ((op != '+' && op != '-' && !concat) || p[1] == '='
2935 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002937 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
2938 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002939
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002940 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002941 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002942 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002943 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002944 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002945 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002946 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002947 {
mityu4ac198c2021-05-28 17:52:40 +02002948 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002949 clear_tv(rettv);
2950 return FAIL;
2951 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002952 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002953 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002954 if ((op != '+' || (rettv->v_type != VAR_LIST
2955 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002956#ifdef FEAT_FLOAT
2957 && (op == '.' || rettv->v_type != VAR_FLOAT)
2958#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002959 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002960 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002961 int error = FALSE;
2962
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002963 // For "list + ...", an illegal use of the first operand as
2964 // a number cannot be determined before evaluating the 2nd
2965 // operand: if this is also a list, all is ok.
2966 // For "something . ...", "something - ..." or "non-list + ...",
2967 // we know that the first operand needs to be a string or number
2968 // without evaluating the 2nd operand. So check before to avoid
2969 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002970 if (op != '.')
2971 tv_get_number_chk(rettv, &error);
2972 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002973 {
2974 clear_tv(rettv);
2975 return FAIL;
2976 }
2977 }
2978
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 /*
2980 * Get the second variable.
2981 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002982 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002983 {
mityu89dcb4d2021-05-28 13:50:17 +02002984 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002985 clear_tv(rettv);
2986 return FAIL;
2987 }
2988 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002989 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002991 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 return FAIL;
2993 }
2994
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002995 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 {
2997 /*
2998 * Compute the result.
2999 */
3000 if (op == '.')
3001 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003002 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3003 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003004 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003005
Bram Moolenaar2e086612020-08-25 22:37:48 +02003006 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003007 || var2.v_type == VAR_CHANNEL
3008 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003009 semsg(_(e_using_invalid_value_as_string_str),
3010 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003011#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02003012 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003013 {
3014 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3015 var2.vval.v_float);
3016 s2 = buf2;
3017 }
3018#endif
3019 else
3020 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003021 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003022 {
3023 clear_tv(rettv);
3024 clear_tv(&var2);
3025 return FAIL;
3026 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003027 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003028 clear_tv(rettv);
3029 rettv->v_type = VAR_STRING;
3030 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003032 else if (op == '+' && rettv->v_type == VAR_BLOB
3033 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003034 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003035 else if (op == '+' && rettv->v_type == VAR_LIST
3036 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003037 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003038 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003039 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 else
3042 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003043 int error = FALSE;
3044 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003045#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003046 float_T f1 = 0, f2 = 0;
3047
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003048 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003049 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003050 f1 = rettv->vval.v_float;
3051 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003052 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003053 else
3054#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003055 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003056 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003057 if (error)
3058 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003059 // This can only happen for "list + non-list" or
3060 // "blob + non-blob". For "non-list + ..." or
3061 // "something - ...", we returned before evaluating the
3062 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003063 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003064 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003065 return FAIL;
3066 }
3067#ifdef FEAT_FLOAT
3068 if (var2.v_type == VAR_FLOAT)
3069 f1 = n1;
3070#endif
3071 }
3072#ifdef FEAT_FLOAT
3073 if (var2.v_type == VAR_FLOAT)
3074 {
3075 f2 = var2.vval.v_float;
3076 n2 = 0;
3077 }
3078 else
3079#endif
3080 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003081 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003082 if (error)
3083 {
3084 clear_tv(rettv);
3085 clear_tv(&var2);
3086 return FAIL;
3087 }
3088#ifdef FEAT_FLOAT
3089 if (rettv->v_type == VAR_FLOAT)
3090 f2 = n2;
3091#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003092 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003093 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003094
3095#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003096 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003097 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3098 {
3099 if (op == '+')
3100 f1 = f1 + f2;
3101 else
3102 f1 = f1 - f2;
3103 rettv->v_type = VAR_FLOAT;
3104 rettv->vval.v_float = f1;
3105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003107#endif
3108 {
3109 if (op == '+')
3110 n1 = n1 + n2;
3111 else
3112 n1 = n1 - n2;
3113 rettv->v_type = VAR_NUMBER;
3114 rettv->vval.v_number = n1;
3115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003117 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 }
3119 }
3120 return OK;
3121}
3122
3123/*
3124 * Handle fifth level expression:
3125 * * number multiplication
3126 * / number division
3127 * % number modulo
3128 *
3129 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003130 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 *
3132 * Return OK or FAIL.
3133 */
3134 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003135eval6(
3136 char_u **arg,
3137 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003138 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003139 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003141#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003142 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003143#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144
3145 /*
3146 * Get the first variable.
3147 */
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003148 if (eval7t(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 return FAIL;
3150
3151 /*
3152 * Repeat computing, until no '*', '/' or '%' is following.
3153 */
3154 for (;;)
3155 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003156 int evaluate;
3157 int getnext;
3158 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003159 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003160 int op;
3161 varnumber_T n1, n2;
3162#ifdef FEAT_FLOAT
3163 float_T f1, f2;
3164#endif
3165 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003166
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003167 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003168 p = eval_next_non_blank(*arg, evalarg, &getnext);
3169 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003170 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003172
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003173 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003174 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003175 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003176 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003177 {
3178 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3179 {
mityu4ac198c2021-05-28 17:52:40 +02003180 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003181 clear_tv(rettv);
3182 return FAIL;
3183 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003184 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003187#ifdef FEAT_FLOAT
3188 f1 = 0;
3189 f2 = 0;
3190#endif
3191 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003192 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003194#ifdef FEAT_FLOAT
3195 if (rettv->v_type == VAR_FLOAT)
3196 {
3197 f1 = rettv->vval.v_float;
3198 use_float = TRUE;
3199 n1 = 0;
3200 }
3201 else
3202#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003203 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003204 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003205 if (error)
3206 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 }
3208 else
3209 n1 = 0;
3210
3211 /*
3212 * Get the second variable.
3213 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003214 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3215 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003216 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003217 clear_tv(rettv);
3218 return FAIL;
3219 }
3220 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003221 if (eval7t(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 return FAIL;
3223
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003224 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003226#ifdef FEAT_FLOAT
3227 if (var2.v_type == VAR_FLOAT)
3228 {
3229 if (!use_float)
3230 {
3231 f1 = n1;
3232 use_float = TRUE;
3233 }
3234 f2 = var2.vval.v_float;
3235 n2 = 0;
3236 }
3237 else
3238#endif
3239 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003240 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003241 clear_tv(&var2);
3242 if (error)
3243 return FAIL;
3244#ifdef FEAT_FLOAT
3245 if (use_float)
3246 f2 = n2;
3247#endif
3248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249
3250 /*
3251 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003252 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003254#ifdef FEAT_FLOAT
3255 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003257 if (op == '*')
3258 f1 = f1 * f2;
3259 else if (op == '/')
3260 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003261# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003262 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003263 if (f2 == 0.0)
3264 {
3265 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003266 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003267 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003268 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003269 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003270 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003271 }
3272 else
3273 f1 = f1 / f2;
3274# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003275 // We rely on the floating point library to handle divide
3276 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003277 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003278# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003281 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003282 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003283 return FAIL;
3284 }
3285 rettv->v_type = VAR_FLOAT;
3286 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 }
3288 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003289#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003291 int failed = FALSE;
3292
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003293 if (op == '*')
3294 n1 = n1 * n2;
3295 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003296 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003298 n1 = num_modulus(n1, n2, &failed);
3299 if (failed)
3300 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003301
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003302 rettv->v_type = VAR_NUMBER;
3303 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 }
3306 }
3307
3308 return OK;
3309}
3310
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003311/*
3312 * Handle a type cast before a base level expression.
3313 * "arg" must point to the first non-white of the expression.
3314 * "arg" is advanced to just after the recognized expression.
3315 * Return OK or FAIL.
3316 */
3317 static int
3318eval7t(
3319 char_u **arg,
3320 typval_T *rettv,
3321 evalarg_T *evalarg,
3322 int want_string) // after "." operator
3323{
3324 type_T *want_type = NULL;
3325 garray_T type_list; // list of pointers to allocated types
3326 int res;
3327 int evaluate = evalarg == NULL ? 0
3328 : (evalarg->eval_flags & EVAL_EVALUATE);
3329
3330 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003331 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3332 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003333 {
3334 ++*arg;
3335 ga_init2(&type_list, sizeof(type_T *), 10);
3336 want_type = parse_type(arg, &type_list, TRUE);
3337 if (want_type == NULL && (evaluate || **arg != '>'))
3338 {
3339 clear_type_list(&type_list);
3340 return FAIL;
3341 }
3342
3343 if (**arg != '>')
3344 {
3345 if (*skipwhite(*arg) == '>')
3346 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3347 else
3348 emsg(_(e_missing_gt));
3349 clear_type_list(&type_list);
3350 return FAIL;
3351 }
3352 ++*arg;
3353 *arg = skipwhite_and_linebreak(*arg, evalarg);
3354 }
3355
3356 res = eval7(arg, rettv, evalarg, want_string);
3357
3358 if (want_type != NULL && evaluate)
3359 {
3360 if (res == OK)
3361 {
3362 type_T *actual = typval2type(rettv, get_copyID(), &type_list, TRUE);
3363
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003364 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003365 {
3366 if (want_type == &t_bool && actual != &t_bool
3367 && (actual->tt_flags & TTFLAG_BOOL_OK))
3368 {
3369 int n = tv2bool(rettv);
3370
3371 // can use "0" and "1" for boolean in some places
3372 clear_tv(rettv);
3373 rettv->v_type = VAR_BOOL;
3374 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3375 }
3376 else
3377 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003378 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003379
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003380 where.wt_variable = TRUE;
3381 res = check_type(want_type, actual, TRUE, where);
3382 }
3383 }
3384 }
3385 clear_type_list(&type_list);
3386 }
3387
3388 return res;
3389}
3390
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003391 int
3392eval_leader(char_u **arg, int vim9)
3393{
3394 char_u *s = *arg;
3395 char_u *p = *arg;
3396
3397 while (*p == '!' || *p == '-' || *p == '+')
3398 {
3399 char_u *n = skipwhite(p + 1);
3400
3401 // ++, --, -+ and +- are not accepted in Vim9 script
3402 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3403 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003404 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003405 return FAIL;
3406 }
3407 p = n;
3408 }
3409 *arg = p;
3410 return OK;
3411}
3412
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413/*
3414 * Handle sixth level expression:
3415 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003416 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003417 * "string" string constant
3418 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 * &option-name option value
3420 * @r register contents
3421 * identifier variable value
3422 * function() function call
3423 * $VAR environment variable
3424 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003425 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003426 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003427 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003428 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 *
3430 * Also handle:
3431 * ! in front logical NOT
3432 * - in front unary minus
3433 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003434 * trailing [] subscript in String or List
3435 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003436 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 *
3438 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003439 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 *
3441 * Return OK or FAIL.
3442 */
3443 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003444eval7(
3445 char_u **arg,
3446 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003447 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003448 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003450 int evaluate = evalarg != NULL
3451 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 int len;
3453 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 char_u *start_leader, *end_leader;
3455 int ret = OK;
3456 char_u *alias;
3457
3458 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003459 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003460 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003462 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463
3464 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003465 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 */
3467 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003468 if (eval_leader(arg, in_vim9script()) == FAIL)
3469 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 end_leader = *arg;
3471
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003472 if (**arg == '.' && (!isdigit(*(*arg + 1))
3473#ifdef FEAT_FLOAT
3474 || current_sctx.sc_version < 2
3475#endif
3476 ))
3477 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003478 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003479 ++*arg;
3480 return FAIL;
3481 }
3482
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 switch (**arg)
3484 {
3485 /*
3486 * Number constant.
3487 */
3488 case '0':
3489 case '1':
3490 case '2':
3491 case '3':
3492 case '4':
3493 case '5':
3494 case '6':
3495 case '7':
3496 case '8':
3497 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003498 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003499
3500 // Apply prefixed "-" and "+" now. Matters especially when
3501 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003502 if (ret == OK && evaluate && end_leader > start_leader
3503 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003504 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 break;
3506
3507 /*
3508 * String constant: "string".
3509 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003510 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 break;
3512
3513 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003514 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003516 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003517 break;
3518
3519 /*
3520 * List: [expr, expr]
3521 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003522 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 break;
3524
3525 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003526 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003527 */
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003528 case '#': if (in_vim9script())
3529 {
3530 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3531 }
3532 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003533 {
3534 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003535 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003536 }
3537 else
3538 ret = NOTDONE;
3539 break;
3540
3541 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003542 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003543 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003544 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003545 case '{': if (in_vim9script())
3546 ret = NOTDONE;
3547 else
3548 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003549 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003550 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003551 break;
3552
3553 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003554 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003556 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 break;
3558
3559 /*
3560 * Environment variable: $VAR.
3561 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003562 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 break;
3564
3565 /*
3566 * Register contents: @r.
3567 */
3568 case '@': ++*arg;
3569 if (evaluate)
3570 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003571 if (in_vim9script() && IS_WHITE_OR_NUL(**arg))
3572 semsg(_(e_syntax_error_at_str), *arg);
3573 else if (in_vim9script() && !valid_yank_reg(**arg, FALSE))
3574 emsg_invreg(**arg);
3575 else
3576 {
3577 rettv->v_type = VAR_STRING;
3578 rettv->vval.v_string = get_reg_contents(**arg,
3579 GREG_EXPR_SRC);
3580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 }
3582 if (**arg != NUL)
3583 ++*arg;
3584 break;
3585
3586 /*
3587 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003588 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003590 case '(': ret = NOTDONE;
3591 if (in_vim9script())
Bram Moolenaar06409502021-02-17 17:00:27 +01003592 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003593 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003594 if (ret == OK && evaluate)
3595 {
3596 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3597
Bram Moolenaara9931532021-06-12 15:58:16 +02003598 // Compile it here to get the return type. The return
3599 // type is optional, when it's missing use t_unknown.
3600 // This is recognized in compile_return().
3601 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3602 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003603 if (compile_def_function(ufunc,
Bram Moolenaare99d4222021-06-13 14:01:26 +02003604 FALSE, COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003605 {
3606 clear_tv(rettv);
3607 ret = FAIL;
3608 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003609 }
3610 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003611 if (ret == NOTDONE)
3612 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003613 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003614 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003615
Bram Moolenaar9215f012020-06-27 21:18:00 +02003616 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003617 if (**arg == ')')
3618 ++*arg;
3619 else if (ret == OK)
3620 {
3621 emsg(_(e_missing_close));
3622 clear_tv(rettv);
3623 ret = FAIL;
3624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 }
3626 break;
3627
Bram Moolenaar8c711452005-01-14 21:53:12 +00003628 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 break;
3630 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003631
3632 if (ret == NOTDONE)
3633 {
3634 /*
3635 * Must be a variable or function name.
3636 * Can also be a curly-braces kind of name: {expr}.
3637 */
3638 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003639 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003640 if (alias != NULL)
3641 s = alias;
3642
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003643 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003644 ret = FAIL;
3645 else
3646 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003647 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3648
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003649 if (evaluate && in_vim9script() && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003650 {
3651 emsg(_(e_cannot_use_underscore_here));
3652 ret = FAIL;
3653 }
3654 else if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003655 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003656 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003657 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003658 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003659 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003660 else if (flags & EVAL_CONSTANT)
3661 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003662 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003663 {
3664 // get the value of "true", "false" or a variable
3665 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3666 {
3667 rettv->v_type = VAR_BOOL;
3668 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003669 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003670 }
3671 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003672 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003673 {
3674 rettv->v_type = VAR_BOOL;
3675 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003676 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003677 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003678 else if (len == 4 && in_vim9script()
3679 && STRNCMP(s, "null", 4) == 0)
3680 {
3681 rettv->v_type = VAR_SPECIAL;
3682 rettv->vval.v_number = VVAL_NULL;
3683 ret = OK;
3684 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003685 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003686 ret = eval_variable(s, len, rettv, NULL,
3687 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003688 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003689 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003690 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003691 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003692 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003693 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003694 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003695 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003696 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003697 }
3698
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003699 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3700 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003701 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003702 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703
3704 /*
3705 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3706 */
3707 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003708 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003709 return ret;
3710}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003711
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003712/*
3713 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003714 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003715 * Adjusts "end_leaderp" until it is at "start_leader".
3716 */
3717 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003718eval7_leader(
3719 typval_T *rettv,
3720 int numeric_only,
3721 char_u *start_leader,
3722 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003723{
3724 char_u *end_leader = *end_leaderp;
3725 int ret = OK;
3726 int error = FALSE;
3727 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003728 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003729#ifdef FEAT_FLOAT
3730 float_T f = 0.0;
3731
3732 if (rettv->v_type == VAR_FLOAT)
3733 f = rettv->vval.v_float;
3734 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003735#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003736 {
3737 while (VIM_ISWHITE(end_leader[-1]))
3738 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003739 if (in_vim9script() && end_leader[-1] == '!')
3740 val = tv2bool(rettv);
3741 else
3742 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003743 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003744 if (error)
3745 {
3746 clear_tv(rettv);
3747 ret = FAIL;
3748 }
3749 else
3750 {
3751 while (end_leader > start_leader)
3752 {
3753 --end_leader;
3754 if (*end_leader == '!')
3755 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003756 if (numeric_only)
3757 {
3758 ++end_leader;
3759 break;
3760 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003761#ifdef FEAT_FLOAT
3762 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003763 {
3764 if (in_vim9script())
3765 {
3766 rettv->v_type = VAR_BOOL;
3767 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3768 }
3769 else
3770 f = !f;
3771 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003772 else
3773#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003774 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003775 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003776 type = VAR_BOOL;
3777 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003778 }
3779 else if (*end_leader == '-')
3780 {
3781#ifdef FEAT_FLOAT
3782 if (rettv->v_type == VAR_FLOAT)
3783 f = -f;
3784 else
3785#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003786 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003787 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003788 type = VAR_NUMBER;
3789 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003790 }
3791 }
3792#ifdef FEAT_FLOAT
3793 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003795 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003796 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003798 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003799#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003800 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003801 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003802 if (in_vim9script())
3803 rettv->v_type = type;
3804 else
3805 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003806 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003809 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 return ret;
3811}
3812
3813/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003814 * Call the function referred to in "rettv".
3815 */
3816 static int
3817call_func_rettv(
3818 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003819 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003820 typval_T *rettv,
3821 int evaluate,
3822 dict_T *selfdict,
3823 typval_T *basetv)
3824{
3825 partial_T *pt = NULL;
3826 funcexe_T funcexe;
3827 typval_T functv;
3828 char_u *s;
3829 int ret;
3830
3831 // need to copy the funcref so that we can clear rettv
3832 if (evaluate)
3833 {
3834 functv = *rettv;
3835 rettv->v_type = VAR_UNKNOWN;
3836
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003837 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003838 if (functv.v_type == VAR_PARTIAL)
3839 {
3840 pt = functv.vval.v_partial;
3841 s = partial_name(pt);
3842 }
3843 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003844 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003845 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003846 if (s == NULL || *s == NUL)
3847 {
3848 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02003849 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003850 goto theend;
3851 }
3852 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003853 }
3854 else
3855 s = (char_u *)"";
3856
Bram Moolenaara80faa82020-04-12 19:37:17 +02003857 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003858 funcexe.firstline = curwin->w_cursor.lnum;
3859 funcexe.lastline = curwin->w_cursor.lnum;
3860 funcexe.evaluate = evaluate;
3861 funcexe.partial = pt;
3862 funcexe.selfdict = selfdict;
3863 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003864 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003865
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003866theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003867 // Clear the funcref afterwards, so that deleting it while
3868 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003869 if (evaluate)
3870 clear_tv(&functv);
3871
3872 return ret;
3873}
3874
3875/*
3876 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003877 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003878 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3879 */
3880 static int
3881eval_lambda(
3882 char_u **arg,
3883 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003884 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003885 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003886{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003887 int evaluate = evalarg != NULL
3888 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003889 typval_T base = *rettv;
3890 int ret;
3891
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003892 rettv->v_type = VAR_UNKNOWN;
3893
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003894 if (**arg == '{')
3895 {
3896 // ->{lambda}()
3897 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3898 }
3899 else
3900 {
3901 // ->(lambda)()
3902 ++*arg;
3903 ret = eval1(arg, rettv, evalarg);
3904 *arg = skipwhite_and_linebreak(*arg, evalarg);
3905 if (**arg != ')')
3906 {
3907 emsg(_(e_missing_close));
3908 ret = FAIL;
3909 }
3910 ++*arg;
3911 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003912 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003913 return FAIL;
3914 else if (**arg != '(')
3915 {
3916 if (verbose)
3917 {
3918 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003919 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003920 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003921 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003922 }
3923 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003924 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003925 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003926 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003927 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003928
3929 // Clear the funcref afterwards, so that deleting it while
3930 // evaluating the arguments is possible (see test55).
3931 if (evaluate)
3932 clear_tv(&base);
3933
3934 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003935}
3936
3937/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003938 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003939 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02003940 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3941 */
3942 static int
3943eval_method(
3944 char_u **arg,
3945 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003946 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003947 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003948{
3949 char_u *name;
3950 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003951 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003952 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003953 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003954 int evaluate = evalarg != NULL
3955 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003956
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003957 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003958
Bram Moolenaarac92e252019-08-03 21:58:38 +02003959 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003960 len = get_name_len(arg, &alias, evaluate, TRUE);
3961 if (alias != NULL)
3962 name = alias;
3963
3964 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003965 {
3966 if (verbose)
3967 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003968 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003969 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003970 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003971 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003972 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003973 if (**arg != '(')
3974 {
3975 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003976 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003977 ret = FAIL;
3978 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003979 else if (VIM_ISWHITE((*arg)[-1]))
3980 {
3981 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003982 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003983 ret = FAIL;
3984 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003985 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003986 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003987 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003988 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003989
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003990 // Clear the funcref afterwards, so that deleting it while
3991 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003992 if (evaluate)
3993 clear_tv(&base);
3994
Bram Moolenaarac92e252019-08-03 21:58:38 +02003995 return ret;
3996}
3997
3998/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003999 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4000 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004001 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4002 */
4003 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004004eval_index(
4005 char_u **arg,
4006 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004007 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004008 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004009{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004010 int evaluate = evalarg != NULL
4011 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004012 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004013 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004014 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004015 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004016 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004017 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004018
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004019 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4020 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004021
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004022 init_tv(&var1);
4023 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004024 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004025 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004026 /*
4027 * dict.name
4028 */
4029 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004030 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004031 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004032 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004033 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004034 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004035 }
4036 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004037 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004038 /*
4039 * something[idx]
4040 *
4041 * Get the (first) variable from inside the [].
4042 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004043 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004044 if (**arg == ':')
4045 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004046 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004047 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004048 else if (vim9 && **arg == ':')
4049 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004050 semsg(_(e_white_space_required_before_and_after_str_at_str),
4051 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004052 clear_tv(&var1);
4053 return FAIL;
4054 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004055 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004056 {
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004057#ifdef FEAT_FLOAT
4058 // allow for indexing with float
4059 if (vim9 && rettv->v_type == VAR_DICT
4060 && var1.v_type == VAR_FLOAT)
4061 {
4062 var1.vval.v_string = typval_tostring(&var1, TRUE);
4063 var1.v_type = VAR_STRING;
4064 }
4065#endif
4066 if (tv_get_string_chk(&var1) == NULL)
4067 {
4068 // not a number or string
4069 clear_tv(&var1);
4070 return FAIL;
4071 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004072 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004073
4074 /*
4075 * Get the second variable from inside the [:].
4076 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004077 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004078 if (**arg == ':')
4079 {
4080 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004081 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01004082 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004083 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004084 semsg(_(e_white_space_required_before_and_after_str_at_str),
4085 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004086 if (!empty1)
4087 clear_tv(&var1);
4088 return FAIL;
4089 }
4090 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004091 if (**arg == ']')
4092 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004093 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004094 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004095 if (!empty1)
4096 clear_tv(&var1);
4097 return FAIL;
4098 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004099 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004100 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004101 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004102 if (!empty1)
4103 clear_tv(&var1);
4104 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004105 return FAIL;
4106 }
4107 }
4108
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004109 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004110 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004111 if (**arg != ']')
4112 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004113 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004114 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004115 clear_tv(&var1);
4116 if (range)
4117 clear_tv(&var2);
4118 return FAIL;
4119 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004120 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004121 }
4122
4123 if (evaluate)
4124 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004125 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004126 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004127 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004128
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004129 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004130 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004131 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004132 clear_tv(&var2);
4133 return res;
4134 }
4135 return OK;
4136}
4137
4138/*
4139 * Check if "rettv" can have an [index] or [sli:ce]
4140 */
4141 int
4142check_can_index(typval_T *rettv, int evaluate, int verbose)
4143{
4144 switch (rettv->v_type)
4145 {
4146 case VAR_FUNC:
4147 case VAR_PARTIAL:
4148 if (verbose)
4149 emsg(_("E695: Cannot index a Funcref"));
4150 return FAIL;
4151 case VAR_FLOAT:
4152#ifdef FEAT_FLOAT
4153 if (verbose)
4154 emsg(_(e_float_as_string));
4155 return FAIL;
4156#endif
4157 case VAR_BOOL:
4158 case VAR_SPECIAL:
4159 case VAR_JOB:
4160 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004161 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004162 if (verbose)
4163 emsg(_(e_cannot_index_special_variable));
4164 return FAIL;
4165 case VAR_UNKNOWN:
4166 case VAR_ANY:
4167 case VAR_VOID:
4168 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004169 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004170 emsg(_(e_cannot_index_special_variable));
4171 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004172 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004173 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004174
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004175 case VAR_STRING:
4176 case VAR_LIST:
4177 case VAR_DICT:
4178 case VAR_BLOB:
4179 break;
4180 case VAR_NUMBER:
4181 if (in_vim9script())
4182 emsg(_(e_cannot_index_number));
4183 break;
4184 }
4185 return OK;
4186}
4187
4188/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004189 * slice() function
4190 */
4191 void
4192f_slice(typval_T *argvars, typval_T *rettv)
4193{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004194 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004195 && ((argvars[0].v_type != VAR_STRING
4196 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004197 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004198 && check_for_list_arg(argvars, 0) == FAIL)
4199 || check_for_number_arg(argvars, 1) == FAIL
4200 || check_for_opt_number_arg(argvars, 2) == FAIL))
4201 return;
4202
Bram Moolenaar6601b622021-01-13 21:47:15 +01004203 if (check_can_index(argvars, TRUE, FALSE) == OK)
4204 {
4205 copy_tv(argvars, rettv);
4206 eval_index_inner(rettv, TRUE, argvars + 1,
4207 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4208 TRUE, NULL, 0, FALSE);
4209 }
4210}
4211
4212/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004213 * Apply index or range to "rettv".
4214 * "var1" is the first index, NULL for [:expr].
4215 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004216 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4217 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004218 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4219 */
4220 int
4221eval_index_inner(
4222 typval_T *rettv,
4223 int is_range,
4224 typval_T *var1,
4225 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004226 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004227 char_u *key,
4228 int keylen,
4229 int verbose)
4230{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004231 varnumber_T n1, n2 = 0;
4232 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004233
4234 n1 = 0;
4235 if (var1 != NULL && rettv->v_type != VAR_DICT)
4236 n1 = tv_get_number(var1);
4237
4238 if (is_range)
4239 {
4240 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004241 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004242 if (verbose)
4243 emsg(_(e_cannot_slice_dictionary));
4244 return FAIL;
4245 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004246 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004247 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004248 else
4249 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004250 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004251
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004252 switch (rettv->v_type)
4253 {
4254 case VAR_UNKNOWN:
4255 case VAR_ANY:
4256 case VAR_VOID:
4257 case VAR_FUNC:
4258 case VAR_PARTIAL:
4259 case VAR_FLOAT:
4260 case VAR_BOOL:
4261 case VAR_SPECIAL:
4262 case VAR_JOB:
4263 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004264 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004265 break; // not evaluating, skipping over subscript
4266
4267 case VAR_NUMBER:
4268 case VAR_STRING:
4269 {
4270 char_u *s = tv_get_string(rettv);
4271
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004272 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004273 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004274 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004275 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004276 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004277 else
4278 s = char_from_string(s, n1);
4279 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004280 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004281 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004282 // The resulting variable is a substring. If the indexes
4283 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004284 if (n1 < 0)
4285 {
4286 n1 = len + n1;
4287 if (n1 < 0)
4288 n1 = 0;
4289 }
4290 if (n2 < 0)
4291 n2 = len + n2;
4292 else if (n2 >= len)
4293 n2 = len;
4294 if (n1 >= len || n2 < 0 || n1 > n2)
4295 s = NULL;
4296 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004297 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004298 }
4299 else
4300 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004301 // The resulting variable is a string of a single
4302 // character. If the index is too big or negative the
4303 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004304 if (n1 >= len || n1 < 0)
4305 s = NULL;
4306 else
4307 s = vim_strnsave(s + n1, 1);
4308 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004309 clear_tv(rettv);
4310 rettv->v_type = VAR_STRING;
4311 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004312 }
4313 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004314
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004315 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004316 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4317 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004318 break;
4319
4320 case VAR_LIST:
4321 if (var1 == NULL)
4322 n1 = 0;
4323 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004324 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004325 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004326 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004327 return FAIL;
4328 break;
4329
4330 case VAR_DICT:
4331 {
4332 dictitem_T *item;
4333 typval_T tmp;
4334
4335 if (key == NULL)
4336 {
4337 key = tv_get_string_chk(var1);
4338 if (key == NULL)
4339 return FAIL;
4340 }
4341
4342 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
4343
4344 if (item == NULL && verbose)
4345 semsg(_(e_dictkey), key);
4346 if (item == NULL)
4347 return FAIL;
4348
4349 copy_tv(&item->di_tv, &tmp);
4350 clear_tv(rettv);
4351 *rettv = tmp;
4352 }
4353 break;
4354 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004355 return OK;
4356}
4357
4358/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004359 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004360 */
4361 char_u *
4362partial_name(partial_T *pt)
4363{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004364 if (pt != NULL)
4365 {
4366 if (pt->pt_name != NULL)
4367 return pt->pt_name;
4368 if (pt->pt_func != NULL)
4369 return pt->pt_func->uf_name;
4370 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004371 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004372}
4373
Bram Moolenaarddecc252016-04-06 22:59:37 +02004374 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004375partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004376{
4377 int i;
4378
4379 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004380 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004381 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004382 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004383 if (pt->pt_name != NULL)
4384 {
4385 func_unref(pt->pt_name);
4386 vim_free(pt->pt_name);
4387 }
4388 else
4389 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004390
Bram Moolenaar54656012021-06-09 20:50:46 +02004391 // "out_up" is no longer used, decrement refcount on partial that owns it.
4392 partial_unref(pt->pt_outer.out_up_partial);
4393
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004394 // Decrease the reference count for the context of a closure. If down
4395 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004396 if (pt->pt_funcstack != NULL)
4397 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004398 --pt->pt_funcstack->fs_refcount;
4399 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004400 }
4401
Bram Moolenaarddecc252016-04-06 22:59:37 +02004402 vim_free(pt);
4403}
4404
4405/*
4406 * Unreference a closure: decrement the reference count and free it when it
4407 * becomes zero.
4408 */
4409 void
4410partial_unref(partial_T *pt)
4411{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004412 if (pt != NULL)
4413 {
4414 if (--pt->pt_refcount <= 0)
4415 partial_free(pt);
4416
4417 // If the reference count goes down to one, the funcstack may be the
4418 // only reference and can be freed if no other partials reference it.
4419 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4420 funcstack_check_refcount(pt->pt_funcstack);
4421 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004422}
4423
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004424/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004425 * Return the next (unique) copy ID.
4426 * Used for serializing nested structures.
4427 */
4428 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004429get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004430{
4431 current_copyID += COPYID_INC;
4432 return current_copyID;
4433}
4434
4435/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004436 * Garbage collection for lists and dictionaries.
4437 *
4438 * We use reference counts to be able to free most items right away when they
4439 * are no longer used. But for composite items it's possible that it becomes
4440 * unused while the reference count is > 0: When there is a recursive
4441 * reference. Example:
4442 * :let l = [1, 2, 3]
4443 * :let d = {9: l}
4444 * :let l[1] = d
4445 *
4446 * Since this is quite unusual we handle this with garbage collection: every
4447 * once in a while find out which lists and dicts are not referenced from any
4448 * variable.
4449 *
4450 * Here is a good reference text about garbage collection (refers to Python
4451 * but it applies to all reference-counting mechanisms):
4452 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004453 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004454
4455/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004456 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004457 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004458 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004459 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004460 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004461garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004462{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004463 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004464 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004465 buf_T *buf;
4466 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004467 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004468 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004469
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004470 if (!testing)
4471 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004472 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004473 want_garbage_collect = FALSE;
4474 may_garbage_collect = FALSE;
4475 garbage_collect_at_exit = FALSE;
4476 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004477
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004478 // The execution stack can grow big, limit the size.
4479 if (exestack.ga_maxlen - exestack.ga_len > 500)
4480 {
4481 size_t new_len;
4482 char_u *pp;
4483 int n;
4484
4485 // Keep 150% of the current size, with a minimum of the growth size.
4486 n = exestack.ga_len / 2;
4487 if (n < exestack.ga_growsize)
4488 n = exestack.ga_growsize;
4489
4490 // Don't make it bigger though.
4491 if (exestack.ga_len + n < exestack.ga_maxlen)
4492 {
4493 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4494 pp = vim_realloc(exestack.ga_data, new_len);
4495 if (pp == NULL)
4496 return FAIL;
4497 exestack.ga_maxlen = exestack.ga_len + n;
4498 exestack.ga_data = pp;
4499 }
4500 }
4501
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004502 // We advance by two because we add one for items referenced through
4503 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004504 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004505
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004506 /*
4507 * 1. Go through all accessible variables and mark all lists and dicts
4508 * with copyID.
4509 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004510
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004511 // Don't free variables in the previous_funccal list unless they are only
4512 // referenced through previous_funccal. This must be first, because if
4513 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004514 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004515
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004516 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004517 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004518
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004519 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004520 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004521 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4522 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004523
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004524 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004525 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004526 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4527 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004528 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004529 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4530 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004531#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004532 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004533 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4534 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004535 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004536 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004537 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4538 NULL, NULL);
4539#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004540
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004541 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004542 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004543 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4544 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004545 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004546 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004547
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004548 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004549 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004550
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004551 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004552 abort = abort || set_ref_in_functions(copyID);
4553
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004554 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004555 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004556
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004557 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004558 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004559
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004560 // callbacks in buffers
4561 abort = abort || set_ref_in_buffers(copyID);
4562
Bram Moolenaar1dced572012-04-05 16:54:08 +02004563#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004564 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004565#endif
4566
Bram Moolenaardb913952012-06-29 12:54:53 +02004567#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004568 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004569#endif
4570
4571#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004572 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004573#endif
4574
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004575#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004576 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004577 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004578#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004579#ifdef FEAT_NETBEANS_INTG
4580 abort = abort || set_ref_in_nb_channel(copyID);
4581#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004582
Bram Moolenaare3188e22016-05-31 21:13:04 +02004583#ifdef FEAT_TIMERS
4584 abort = abort || set_ref_in_timer(copyID);
4585#endif
4586
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004587#ifdef FEAT_QUICKFIX
4588 abort = abort || set_ref_in_quickfix(copyID);
4589#endif
4590
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004591#ifdef FEAT_TERMINAL
4592 abort = abort || set_ref_in_term(copyID);
4593#endif
4594
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004595#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004596 abort = abort || set_ref_in_popups(copyID);
4597#endif
4598
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004599 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004600 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004601 /*
4602 * 2. Free lists and dictionaries that are not referenced.
4603 */
4604 did_free = free_unref_items(copyID);
4605
4606 /*
4607 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004608 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004609 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004610 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004611 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004612 else if (p_verbose > 0)
4613 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004614 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004615 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004616
4617 return did_free;
4618}
4619
4620/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004621 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004622 */
4623 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004624free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004625{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004626 int did_free = FALSE;
4627
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004628 // Let all "free" functions know that we are here. This means no
4629 // dictionaries, lists, channels or jobs are to be freed, because we will
4630 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004631 in_free_unref_items = TRUE;
4632
4633 /*
4634 * PASS 1: free the contents of the items. We don't free the items
4635 * themselves yet, so that it is possible to decrement refcount counters
4636 */
4637
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004638 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004639 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004640
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004641 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004642 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004643
4644#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004645 // Go through the list of jobs and free items without the copyID. This
4646 // must happen before doing channels, because jobs refer to channels, but
4647 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004648 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4649
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004650 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004651 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4652#endif
4653
4654 /*
4655 * PASS 2: free the items themselves.
4656 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004657 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004658 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004659
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004660#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004661 // Go through the list of jobs and free items without the copyID. This
4662 // must happen before doing channels, because jobs refer to channels, but
4663 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004664 free_unused_jobs(copyID, COPYID_MASK);
4665
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004666 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004667 free_unused_channels(copyID, COPYID_MASK);
4668#endif
4669
4670 in_free_unref_items = FALSE;
4671
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004672 return did_free;
4673}
4674
4675/*
4676 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004677 * "list_stack" is used to add lists to be marked. Can be NULL.
4678 *
4679 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004680 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004681 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004682set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004683{
4684 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004685 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004686 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004687 hashtab_T *cur_ht;
4688 ht_stack_T *ht_stack = NULL;
4689 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004690
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004691 cur_ht = ht;
4692 for (;;)
4693 {
4694 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004695 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004696 // Mark each item in the hashtab. If the item contains a hashtab
4697 // it is added to ht_stack, if it contains a list it is added to
4698 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004699 todo = (int)cur_ht->ht_used;
4700 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4701 if (!HASHITEM_EMPTY(hi))
4702 {
4703 --todo;
4704 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4705 &ht_stack, list_stack);
4706 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004707 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004708
4709 if (ht_stack == NULL)
4710 break;
4711
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004712 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004713 cur_ht = ht_stack->ht;
4714 tempitem = ht_stack;
4715 ht_stack = ht_stack->prev;
4716 free(tempitem);
4717 }
4718
4719 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004720}
4721
4722/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004723 * Mark a dict and its items with "copyID".
4724 * Returns TRUE if setting references failed somehow.
4725 */
4726 int
4727set_ref_in_dict(dict_T *d, int copyID)
4728{
4729 if (d != NULL && d->dv_copyID != copyID)
4730 {
4731 d->dv_copyID = copyID;
4732 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4733 }
4734 return FALSE;
4735}
4736
4737/*
4738 * Mark a list and its items with "copyID".
4739 * Returns TRUE if setting references failed somehow.
4740 */
4741 int
4742set_ref_in_list(list_T *ll, int copyID)
4743{
4744 if (ll != NULL && ll->lv_copyID != copyID)
4745 {
4746 ll->lv_copyID = copyID;
4747 return set_ref_in_list_items(ll, copyID, NULL);
4748 }
4749 return FALSE;
4750}
4751
4752/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004753 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004754 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4755 *
4756 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004757 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004758 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004759set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004760{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004761 listitem_T *li;
4762 int abort = FALSE;
4763 list_T *cur_l;
4764 list_stack_T *list_stack = NULL;
4765 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004766
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004767 cur_l = l;
4768 for (;;)
4769 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004770 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004771 // Mark each item in the list. If the item contains a hashtab
4772 // it is added to ht_stack, if it contains a list it is added to
4773 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004774 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4775 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4776 ht_stack, &list_stack);
4777 if (list_stack == NULL)
4778 break;
4779
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004780 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004781 cur_l = list_stack->list;
4782 tempitem = list_stack;
4783 list_stack = list_stack->prev;
4784 free(tempitem);
4785 }
4786
4787 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004788}
4789
4790/*
4791 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004792 * "list_stack" is used to add lists to be marked. Can be NULL.
4793 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4794 *
4795 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004796 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004797 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004798set_ref_in_item(
4799 typval_T *tv,
4800 int copyID,
4801 ht_stack_T **ht_stack,
4802 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004803{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004804 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004805
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004806 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004807 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004808 dict_T *dd = tv->vval.v_dict;
4809
Bram Moolenaara03f2332016-02-06 18:09:59 +01004810 if (dd != NULL && dd->dv_copyID != copyID)
4811 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004812 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004813 dd->dv_copyID = copyID;
4814 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004815 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004816 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4817 }
4818 else
4819 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004820 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4821
Bram Moolenaara03f2332016-02-06 18:09:59 +01004822 if (newitem == NULL)
4823 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004824 else
4825 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004826 newitem->ht = &dd->dv_hashtab;
4827 newitem->prev = *ht_stack;
4828 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004829 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004830 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004831 }
4832 }
4833 else if (tv->v_type == VAR_LIST)
4834 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004835 list_T *ll = tv->vval.v_list;
4836
Bram Moolenaara03f2332016-02-06 18:09:59 +01004837 if (ll != NULL && ll->lv_copyID != copyID)
4838 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004839 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004840 ll->lv_copyID = copyID;
4841 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004842 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004843 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004844 }
4845 else
4846 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004847 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4848
Bram Moolenaara03f2332016-02-06 18:09:59 +01004849 if (newitem == NULL)
4850 abort = TRUE;
4851 else
4852 {
4853 newitem->list = ll;
4854 newitem->prev = *list_stack;
4855 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004856 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004857 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004858 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004859 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004860 else if (tv->v_type == VAR_FUNC)
4861 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004862 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004863 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004864 else if (tv->v_type == VAR_PARTIAL)
4865 {
4866 partial_T *pt = tv->vval.v_partial;
4867 int i;
4868
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004869 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004870 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004871 // Didn't see this partial yet.
4872 pt->pt_copyID = copyID;
4873
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004874 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004875
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004876 if (pt->pt_dict != NULL)
4877 {
4878 typval_T dtv;
4879
4880 dtv.v_type = VAR_DICT;
4881 dtv.vval.v_dict = pt->pt_dict;
4882 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4883 }
4884
4885 for (i = 0; i < pt->pt_argc; ++i)
4886 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4887 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004888 if (pt->pt_funcstack != NULL)
4889 {
4890 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4891
4892 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4893 abort = abort || set_ref_in_item(stack + i, copyID,
4894 ht_stack, list_stack);
4895 }
4896
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004897 }
4898 }
4899#ifdef FEAT_JOB_CHANNEL
4900 else if (tv->v_type == VAR_JOB)
4901 {
4902 job_T *job = tv->vval.v_job;
4903 typval_T dtv;
4904
4905 if (job != NULL && job->jv_copyID != copyID)
4906 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004907 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004908 if (job->jv_channel != NULL)
4909 {
4910 dtv.v_type = VAR_CHANNEL;
4911 dtv.vval.v_channel = job->jv_channel;
4912 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4913 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004914 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004915 {
4916 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004917 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004918 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4919 }
4920 }
4921 }
4922 else if (tv->v_type == VAR_CHANNEL)
4923 {
4924 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004925 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004926 typval_T dtv;
4927 jsonq_T *jq;
4928 cbq_T *cq;
4929
4930 if (ch != NULL && ch->ch_copyID != copyID)
4931 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004932 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004933 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004934 {
4935 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4936 jq = jq->jq_next)
4937 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4938 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4939 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004940 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004941 {
4942 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004943 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004944 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4945 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004946 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004947 {
4948 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004949 dtv.vval.v_partial =
4950 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004951 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4952 }
4953 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004954 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004955 {
4956 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004957 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004958 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4959 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004960 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004961 {
4962 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004963 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004964 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4965 }
4966 }
4967 }
4968#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004969 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004970}
4971
Bram Moolenaar8c711452005-01-14 21:53:12 +00004972/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004973 * Return a string with the string representation of a variable.
4974 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004975 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004976 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004977 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004978 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02004979 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004980 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4981 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004982 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004983 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004984 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004985echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004986 typval_T *tv,
4987 char_u **tofree,
4988 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004989 int copyID,
4990 int echo_style,
4991 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004992 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004993{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004994 static int recurse = 0;
4995 char_u *r = NULL;
4996
Bram Moolenaar33570922005-01-25 22:26:29 +00004997 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004998 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004999 if (!did_echo_string_emsg)
5000 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005001 // Only give this message once for a recursive call to avoid
5002 // flooding the user with errors. And stop iterating over lists
5003 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005004 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005005 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005006 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005007 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005008 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005009 }
5010 ++recurse;
5011
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005012 switch (tv->v_type)
5013 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005014 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005015 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005016 {
5017 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005018 r = tv->vval.v_string;
5019 if (r == NULL)
5020 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005021 }
5022 else
5023 {
5024 *tofree = string_quote(tv->vval.v_string, FALSE);
5025 r = *tofree;
5026 }
5027 break;
5028
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005029 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005030 if (echo_style)
5031 {
5032 *tofree = NULL;
5033 r = tv->vval.v_string;
5034 }
5035 else
5036 {
5037 *tofree = string_quote(tv->vval.v_string, TRUE);
5038 r = *tofree;
5039 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005040 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005041
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005042 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005043 {
5044 partial_T *pt = tv->vval.v_partial;
5045 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005046 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005047 garray_T ga;
5048 int i;
5049 char_u *tf;
5050
5051 ga_init2(&ga, 1, 100);
5052 ga_concat(&ga, (char_u *)"function(");
5053 if (fname != NULL)
5054 {
5055 ga_concat(&ga, fname);
5056 vim_free(fname);
5057 }
5058 if (pt != NULL && pt->pt_argc > 0)
5059 {
5060 ga_concat(&ga, (char_u *)", [");
5061 for (i = 0; i < pt->pt_argc; ++i)
5062 {
5063 if (i > 0)
5064 ga_concat(&ga, (char_u *)", ");
5065 ga_concat(&ga,
5066 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5067 vim_free(tf);
5068 }
5069 ga_concat(&ga, (char_u *)"]");
5070 }
5071 if (pt != NULL && pt->pt_dict != NULL)
5072 {
5073 typval_T dtv;
5074
5075 ga_concat(&ga, (char_u *)", ");
5076 dtv.v_type = VAR_DICT;
5077 dtv.vval.v_dict = pt->pt_dict;
5078 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5079 vim_free(tf);
5080 }
5081 ga_concat(&ga, (char_u *)")");
5082
5083 *tofree = ga.ga_data;
5084 r = *tofree;
5085 break;
5086 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005087
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005088 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005089 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005090 break;
5091
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005092 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005093 if (tv->vval.v_list == NULL)
5094 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005095 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005096 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005097 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005098 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005099 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5100 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005101 {
5102 *tofree = NULL;
5103 r = (char_u *)"[...]";
5104 }
5105 else
5106 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005107 int old_copyID = tv->vval.v_list->lv_copyID;
5108
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005109 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005110 *tofree = list2string(tv, copyID, restore_copyID);
5111 if (restore_copyID)
5112 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005113 r = *tofree;
5114 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005115 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005116
Bram Moolenaar8c711452005-01-14 21:53:12 +00005117 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005118 if (tv->vval.v_dict == NULL)
5119 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005120 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005121 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005122 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005123 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005124 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5125 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005126 {
5127 *tofree = NULL;
5128 r = (char_u *)"{...}";
5129 }
5130 else
5131 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005132 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005133
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005134 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005135 *tofree = dict2string(tv, copyID, restore_copyID);
5136 if (restore_copyID)
5137 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005138 r = *tofree;
5139 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005140 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005141
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005142 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005143 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005144 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005145 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005146 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005147 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005148 break;
5149
Bram Moolenaar835dc632016-02-07 14:27:38 +01005150 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005151 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005152#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005153 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005154 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5155 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005156 if (composite_val)
5157 {
5158 *tofree = string_quote(r, FALSE);
5159 r = *tofree;
5160 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005161#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005162 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005163
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005164 case VAR_INSTR:
5165 *tofree = NULL;
5166 r = (char_u *)"instructions";
5167 break;
5168
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005169 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005170#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005171 *tofree = NULL;
5172 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5173 r = numbuf;
5174 break;
5175#endif
5176
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005177 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005178 case VAR_SPECIAL:
5179 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005180 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005181 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005182 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005183
Bram Moolenaar8502c702014-06-17 12:51:16 +02005184 if (--recurse == 0)
5185 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005186 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005187}
5188
5189/*
5190 * Return a string with the string representation of a variable.
5191 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5192 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005193 * Does not put quotes around strings, as ":echo" displays values.
5194 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5195 * May return NULL.
5196 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005197 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005198echo_string(
5199 typval_T *tv,
5200 char_u **tofree,
5201 char_u *numbuf,
5202 int copyID)
5203{
5204 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5205}
5206
5207/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005208 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5209 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005210 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005211 */
5212 int
5213buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5214{
5215 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005216 char_u *t;
5217 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005218
5219 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5220 return -1;
5221
5222 if (lnum > buf->b_ml.ml_line_count)
5223 lnum = buf->b_ml.ml_line_count;
5224
5225 str = ml_get_buf(buf, lnum, FALSE);
5226 if (str == NULL)
5227 return -1;
5228
5229 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005230 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005231
Bram Moolenaar91458462021-01-13 20:08:38 +01005232 // count the number of characters
5233 t = str;
5234 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5235 t += mb_ptr2len(t);
5236
5237 // In insert mode, when the cursor is at the end of a non-empty line,
5238 // byteidx points to the NUL character immediately past the end of the
5239 // string. In this case, add one to the character count.
5240 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5241 count++;
5242
5243 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005244}
5245
5246/*
5247 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005248 * byte index. Works only for loaded buffers. Returns -1 on failure.
5249 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005250 */
5251 int
5252buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5253{
5254 char_u *str;
5255 char_u *t;
5256
5257 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5258 return -1;
5259
5260 if (lnum > buf->b_ml.ml_line_count)
5261 lnum = buf->b_ml.ml_line_count;
5262
5263 str = ml_get_buf(buf, lnum, FALSE);
5264 if (str == NULL)
5265 return -1;
5266
5267 // Convert the character offset to a byte offset
5268 t = str;
5269 while (*t != NUL && --charidx > 0)
5270 t += mb_ptr2len(t);
5271
Bram Moolenaar91458462021-01-13 20:08:38 +01005272 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005273}
5274
5275/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005277 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005279 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005280var2fpos(
5281 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005282 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005283 int *fnum, // set to fnum for '0, 'A, etc.
5284 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005286 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005288 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005290 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005291 if (varp->v_type == VAR_LIST)
5292 {
5293 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005294 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005295 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005296 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005297
5298 l = varp->vval.v_list;
5299 if (l == NULL)
5300 return NULL;
5301
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005302 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005303 pos.lnum = list_find_nr(l, 0L, &error);
5304 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005305 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005306 if (charcol)
5307 len = (long)mb_charlen(ml_get(pos.lnum));
5308 else
5309 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005310
Bram Moolenaarec65d772020-08-20 22:29:12 +02005311 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005312 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005313 li = list_find(l, 1L);
5314 if (li != NULL && li->li_tv.v_type == VAR_STRING
5315 && li->li_tv.vval.v_string != NULL
5316 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005317 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005318 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005319 }
5320 else
5321 {
5322 pos.col = list_find_nr(l, 1L, &error);
5323 if (error)
5324 return NULL;
5325 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005326
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005327 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005328 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005329 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005330 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005331
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005332 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005333 pos.coladd = list_find_nr(l, 2L, &error);
5334 if (error)
5335 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005336
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005337 return &pos;
5338 }
5339
Bram Moolenaarc5809432021-03-27 21:23:30 +01005340 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5341 return NULL;
5342
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005343 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005344 if (name == NULL)
5345 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005346 if (name[0] == '.') // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005347 {
5348 pos = curwin->w_cursor;
5349 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005350 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005351 return &pos;
5352 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005353 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005354 {
5355 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005356 pos = VIsual;
5357 else
5358 pos = curwin->w_cursor;
5359 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005360 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005361 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005362 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005363 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005365 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5367 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005368 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005369 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 return pp;
5371 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005372
Bram Moolenaara5525202006-03-02 22:52:09 +00005373 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005374
Bram Moolenaar477933c2007-07-17 14:32:23 +00005375 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005376 {
5377 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005378 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005379 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005380 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005381 // In silent Ex mode topline is zero, but that's not a valid line
5382 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005383 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005384 return &pos;
5385 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005386 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005387 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005388 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005389 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005390 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005391 return &pos;
5392 }
5393 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005394 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005396 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 {
5398 pos.lnum = curbuf->b_ml.ml_line_count;
5399 pos.col = 0;
5400 }
5401 else
5402 {
5403 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005404 if (charcol)
5405 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5406 else
5407 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 }
5409 return &pos;
5410 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005411 if (in_vim9script())
5412 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 return NULL;
5414}
5415
5416/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005417 * Convert list in "arg" into a position and optional file number.
5418 * When "fnump" is NULL there is no file number, only 3 items.
5419 * Note that the column is passed on as-is, the caller may want to decrement
5420 * it to use 1 for the first column.
5421 * Return FAIL when conversion is not possible, doesn't check the position for
5422 * validity.
5423 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005424 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005425list2fpos(
5426 typval_T *arg,
5427 pos_T *posp,
5428 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005429 colnr_T *curswantp,
5430 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005431{
5432 list_T *l = arg->vval.v_list;
5433 long i = 0;
5434 long n;
5435
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005436 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5437 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005438 if (arg->v_type != VAR_LIST
5439 || l == NULL
5440 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005441 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005442 return FAIL;
5443
5444 if (fnump != NULL)
5445 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005446 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005447 if (n < 0)
5448 return FAIL;
5449 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005450 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005451 *fnump = n;
5452 }
5453
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005454 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005455 if (n < 0)
5456 return FAIL;
5457 posp->lnum = n;
5458
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005459 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005460 if (n < 0)
5461 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005462 // If character position is specified, then convert to byte position
5463 if (charcol)
5464 {
5465 buf_T *buf;
5466
5467 // Get the text for the specified line in a loaded buffer
5468 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5469 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5470 return FAIL;
5471
Bram Moolenaar91458462021-01-13 20:08:38 +01005472 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005473 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005474 posp->col = n;
5475
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005476 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005477 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005478 posp->coladd = 0;
5479 else
5480 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005481
Bram Moolenaar493c1782014-05-28 14:34:46 +02005482 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005483 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005484
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005485 return OK;
5486}
5487
5488/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 * Get the length of an environment variable name.
5490 * Advance "arg" to the first character after the name.
5491 * Return 0 for error.
5492 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005493 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005494get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495{
5496 char_u *p;
5497 int len;
5498
5499 for (p = *arg; vim_isIDc(*p); ++p)
5500 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005501 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 return 0;
5503
5504 len = (int)(p - *arg);
5505 *arg = p;
5506 return len;
5507}
5508
5509/*
5510 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005511 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 * Return 0 if something is wrong.
5513 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005514 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005515get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516{
5517 char_u *p;
5518 int len;
5519
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005520 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005522 {
5523 if (*p == ':')
5524 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005525 // "s:" is start of "s:var", but "n:" is not and can be used in
5526 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005527 len = (int)(p - *arg);
5528 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5529 || len > 1)
5530 break;
5531 }
5532 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005533 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 return 0;
5535
5536 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005537 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538
5539 return len;
5540}
5541
5542/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005543 * Get the length of the name of a variable or function.
5544 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005546 * Return -1 if curly braces expansion failed.
5547 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 * If the name contains 'magic' {}'s, expand them and return the
5549 * expanded name in an allocated string via 'alias' - caller must free.
5550 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005551 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005552get_name_len(
5553 char_u **arg,
5554 char_u **alias,
5555 int evaluate,
5556 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557{
5558 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 char_u *p;
5560 char_u *expr_start;
5561 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005563 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564
5565 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5566 && (*arg)[2] == (int)KE_SNR)
5567 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005568 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 *arg += 3;
5570 return get_id_len(arg) + 3;
5571 }
5572 len = eval_fname_script(*arg);
5573 if (len > 0)
5574 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005575 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 *arg += len;
5577 }
5578
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005580 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005582 p = find_name_end(*arg, &expr_start, &expr_end,
5583 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 if (expr_start != NULL)
5585 {
5586 char_u *temp_string;
5587
5588 if (!evaluate)
5589 {
5590 len += (int)(p - *arg);
5591 *arg = skipwhite(p);
5592 return len;
5593 }
5594
5595 /*
5596 * Include any <SID> etc in the expanded string:
5597 * Thus the -len here.
5598 */
5599 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5600 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005601 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 *alias = temp_string;
5603 *arg = skipwhite(p);
5604 return (int)STRLEN(temp_string);
5605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606
5607 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005608 // Only give an error when there is something, otherwise it will be
5609 // reported at a higher level.
5610 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005611 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612
5613 return len;
5614}
5615
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005616/*
5617 * Find the end of a variable or function name, taking care of magic braces.
5618 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5619 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005620 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005621 * Return a pointer to just after the name. Equal to "arg" if there is no
5622 * valid name.
5623 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005624 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005625find_name_end(
5626 char_u *arg,
5627 char_u **expr_start,
5628 char_u **expr_end,
5629 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005631 int mb_nest = 0;
5632 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005634 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005635 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005637 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005639 *expr_start = NULL;
5640 *expr_end = NULL;
5641 }
5642
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005643 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005644 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5645 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005646 return arg;
5647
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005648 for (p = arg; *p != NUL
5649 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005650 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005651 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005652 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005653 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005654 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005655 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005656 if (*p == '\'')
5657 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005658 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005659 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005660 ;
5661 if (*p == NUL)
5662 break;
5663 }
5664 else if (*p == '"')
5665 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005666 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005667 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005668 if (*p == '\\' && p[1] != NUL)
5669 ++p;
5670 if (*p == NUL)
5671 break;
5672 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005673 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5674 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005675 // "s:" is start of "s:var", but "n:" is not and can be used in
5676 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005677 len = (int)(p - arg);
5678 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005679 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005680 break;
5681 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005682
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005683 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005685 if (*p == '[')
5686 ++br_nest;
5687 else if (*p == ']')
5688 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005690
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005691 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005693 if (*p == '{')
5694 {
5695 mb_nest++;
5696 if (expr_start != NULL && *expr_start == NULL)
5697 *expr_start = p;
5698 }
5699 else if (*p == '}')
5700 {
5701 mb_nest--;
5702 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5703 *expr_end = p;
5704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 }
5707
5708 return p;
5709}
5710
5711/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005712 * Expands out the 'magic' {}'s in a variable/function name.
5713 * Note that this can call itself recursively, to deal with
5714 * constructs like foo{bar}{baz}{bam}
5715 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5716 * "in_start" ^
5717 * "expr_start" ^
5718 * "expr_end" ^
5719 * "in_end" ^
5720 *
5721 * Returns a new allocated string, which the caller must free.
5722 * Returns NULL for failure.
5723 */
5724 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005725make_expanded_name(
5726 char_u *in_start,
5727 char_u *expr_start,
5728 char_u *expr_end,
5729 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005730{
5731 char_u c1;
5732 char_u *retval = NULL;
5733 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005734
5735 if (expr_end == NULL || in_end == NULL)
5736 return NULL;
5737 *expr_start = NUL;
5738 *expr_end = NUL;
5739 c1 = *in_end;
5740 *in_end = NUL;
5741
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005742 temp_result = eval_to_string(expr_start + 1, FALSE);
5743 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005744 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005745 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5746 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005747 if (retval != NULL)
5748 {
5749 STRCPY(retval, in_start);
5750 STRCAT(retval, temp_result);
5751 STRCAT(retval, expr_end + 1);
5752 }
5753 }
5754 vim_free(temp_result);
5755
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005756 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005757 *expr_start = '{';
5758 *expr_end = '}';
5759
5760 if (retval != NULL)
5761 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005762 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005763 if (expr_start != NULL)
5764 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005765 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005766 temp_result = make_expanded_name(retval, expr_start,
5767 expr_end, temp_result);
5768 vim_free(retval);
5769 retval = temp_result;
5770 }
5771 }
5772
5773 return retval;
5774}
5775
5776/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005778 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005780 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005781eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005783 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005784}
5785
5786/*
5787 * Return TRUE if character "c" can be used as the first character in a
5788 * variable or function name (excluding '{' and '}').
5789 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005790 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005791eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005792{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005793 return ASCII_ISALPHA(c) || c == '_';
5794}
5795
5796/*
5797 * Return TRUE if character "c" can be used as the first character of a
5798 * dictionary key.
5799 */
5800 int
5801eval_isdictc(int c)
5802{
5803 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804}
5805
5806/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005807 * Handle:
5808 * - expr[expr], expr[expr:expr] subscript
5809 * - ".name" lookup
5810 * - function call with Funcref variable: func(expr)
5811 * - method call: var->method()
5812 *
5813 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005814 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005815 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005816handle_subscript(
5817 char_u **arg,
5818 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005819 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005820 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005821{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005822 int evaluate = evalarg != NULL
5823 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005824 int ret = OK;
5825 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005826 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005827 int getnext;
5828 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005829
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005830 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005831 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005832 // When at the end of the line and ".name" or "->{" or "->X" follows in
5833 // the next line then consume the line break.
5834 p = eval_next_non_blank(*arg, evalarg, &getnext);
5835 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005836 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02005837 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
5838 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
5839 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005840 {
5841 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005842 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005843 check_white = FALSE;
5844 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005845
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01005846 if (rettv->v_type == VAR_ANY)
5847 {
5848 char_u *exp_name;
5849 int cc;
5850 int idx;
5851 ufunc_T *ufunc;
5852 type_T *type;
5853
5854 // Found script from "import * as {name}", script item name must
5855 // follow.
5856 if (**arg != '.')
5857 {
5858 if (verbose)
5859 semsg(_(e_expected_str_but_got_str), "'.'", *arg);
5860 ret = FAIL;
5861 break;
5862 }
5863 ++*arg;
5864 if (IS_WHITE_OR_NUL(**arg))
5865 {
5866 if (verbose)
5867 emsg(_(e_no_white_space_allowed_after_dot));
5868 ret = FAIL;
5869 break;
5870 }
5871
5872 // isolate the name
5873 exp_name = *arg;
5874 while (eval_isnamec(**arg))
5875 ++*arg;
5876 cc = **arg;
5877 **arg = NUL;
5878
5879 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
5880 evalarg->eval_cctx, verbose);
5881 **arg = cc;
5882 *arg = skipwhite(*arg);
5883
5884 if (idx < 0 && ufunc == NULL)
5885 {
5886 ret = FAIL;
5887 break;
5888 }
5889 if (idx >= 0)
5890 {
5891 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
5892 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
5893
5894 copy_tv(sv->sv_tv, rettv);
5895 }
5896 else
5897 {
5898 rettv->v_type = VAR_FUNC;
5899 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
5900 }
5901 }
5902
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005903 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5904 || rettv->v_type == VAR_PARTIAL))
5905 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005906 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005907 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5908 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005909
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005910 // Stop the expression evaluation when immediately aborting on
5911 // error, or when an interrupt occurred or an exception was thrown
5912 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005913 if (aborting())
5914 {
5915 if (ret == OK)
5916 clear_tv(rettv);
5917 ret = FAIL;
5918 }
5919 dict_unref(selfdict);
5920 selfdict = NULL;
5921 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005922 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005923 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005924 if (in_vim9script())
5925 *arg = skipwhite(p + 2);
5926 else
5927 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005928 if (ret == OK)
5929 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005930 if (VIM_ISWHITE(**arg))
5931 {
5932 emsg(_(e_nowhitespace));
5933 ret = FAIL;
5934 }
5935 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005936 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005937 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005938 else
5939 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005940 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005941 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005942 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005943 // "." is ".name" lookup when we found a dict or when evaluating and
5944 // scriptversion is at least 2, where string concatenation is "..".
5945 else if (**arg == '['
5946 || (**arg == '.' && (rettv->v_type == VAR_DICT
5947 || (!evaluate
5948 && (*arg)[1] != '.'
5949 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005950 {
5951 dict_unref(selfdict);
5952 if (rettv->v_type == VAR_DICT)
5953 {
5954 selfdict = rettv->vval.v_dict;
5955 if (selfdict != NULL)
5956 ++selfdict->dv_refcount;
5957 }
5958 else
5959 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005960 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005961 {
5962 clear_tv(rettv);
5963 ret = FAIL;
5964 }
5965 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005966 else
5967 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005968 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005969
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005970 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5971 // Don't do this when "Func" is already a partial that was bound
5972 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005973 if (selfdict != NULL
5974 && (rettv->v_type == VAR_FUNC
5975 || (rettv->v_type == VAR_PARTIAL
5976 && (rettv->vval.v_partial->pt_auto
5977 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005978 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005979
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005980 dict_unref(selfdict);
5981 return ret;
5982}
5983
5984/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005985 * Make a copy of an item.
5986 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005987 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5988 * reference to an already copied list/dict can be used.
5989 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005990 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005991 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005992item_copy(
5993 typval_T *from,
5994 typval_T *to,
5995 int deep,
5996 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005997{
5998 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005999 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006000
Bram Moolenaar33570922005-01-25 22:26:29 +00006001 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006002 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006003 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006004 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006005 }
6006 ++recurse;
6007
6008 switch (from->v_type)
6009 {
6010 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006011 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006012 case VAR_STRING:
6013 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006014 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006015 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006016 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006017 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006018 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006019 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006020 copy_tv(from, to);
6021 break;
6022 case VAR_LIST:
6023 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006024 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006025 if (from->vval.v_list == NULL)
6026 to->vval.v_list = NULL;
6027 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6028 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006029 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006030 to->vval.v_list = from->vval.v_list->lv_copylist;
6031 ++to->vval.v_list->lv_refcount;
6032 }
6033 else
6034 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
6035 if (to->vval.v_list == NULL)
6036 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006037 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006038 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006039 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006040 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006041 case VAR_DICT:
6042 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006043 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006044 if (from->vval.v_dict == NULL)
6045 to->vval.v_dict = NULL;
6046 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6047 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006048 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006049 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6050 ++to->vval.v_dict->dv_refcount;
6051 }
6052 else
6053 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
6054 if (to->vval.v_dict == NULL)
6055 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006056 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006057 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006058 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006059 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006060 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006061 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006062 }
6063 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006064 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006065}
6066
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006067 void
6068echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6069{
6070 char_u *tofree;
6071 char_u numbuf[NUMBUFLEN];
6072 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6073
6074 if (*atstart)
6075 {
6076 *atstart = FALSE;
6077 // Call msg_start() after eval1(), evaluating the expression
6078 // may cause a message to appear.
6079 if (with_space)
6080 {
6081 // Mark the saved text as finishing the line, so that what
6082 // follows is displayed on a new line when scrolling back
6083 // at the more prompt.
6084 msg_sb_eol();
6085 msg_start();
6086 }
6087 }
6088 else if (with_space)
6089 msg_puts_attr(" ", echo_attr);
6090
6091 if (p != NULL)
6092 for ( ; *p != NUL && !got_int; ++p)
6093 {
6094 if (*p == '\n' || *p == '\r' || *p == TAB)
6095 {
6096 if (*p != TAB && *needclr)
6097 {
6098 // remove any text still there from the command
6099 msg_clr_eos();
6100 *needclr = FALSE;
6101 }
6102 msg_putchar_attr(*p, echo_attr);
6103 }
6104 else
6105 {
6106 if (has_mbyte)
6107 {
6108 int i = (*mb_ptr2len)(p);
6109
6110 (void)msg_outtrans_len_attr(p, i, echo_attr);
6111 p += i - 1;
6112 }
6113 else
6114 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6115 }
6116 }
6117 vim_free(tofree);
6118}
6119
Bram Moolenaare9a41262005-01-15 22:18:47 +00006120/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 * ":echo expr1 ..." print each argument separated with a space, add a
6122 * newline at the end.
6123 * ":echon expr1 ..." print each argument plain.
6124 */
6125 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006126ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127{
6128 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006129 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006130 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 int needclr = TRUE;
6132 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006133 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006134 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006135 evalarg_T evalarg;
6136
Bram Moolenaare707c882020-07-01 13:07:37 +02006137 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138
6139 if (eap->skip)
6140 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006141 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006143 // If eval1() causes an error message the text from the command may
6144 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006145 need_clr_eos = needclr;
6146
Bram Moolenaar68db9962021-05-09 23:19:22 +02006147 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006148 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 {
6150 /*
6151 * Report the invalid expression unless the expression evaluation
6152 * has been cancelled due to an aborting error, an interrupt, or an
6153 * exception.
6154 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006155 if (!aborting() && did_emsg == did_emsg_before
6156 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006157 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006158 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 break;
6160 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006161 need_clr_eos = FALSE;
6162
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006164 {
6165 if (rettv.v_type == VAR_VOID)
6166 {
6167 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6168 break;
6169 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006170 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006171 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006172
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006173 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 arg = skipwhite(arg);
6175 }
6176 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006177 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178
6179 if (eap->skip)
6180 --emsg_skip;
6181 else
6182 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006183 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 if (needclr)
6185 msg_clr_eos();
6186 if (eap->cmdidx == CMD_echo)
6187 msg_end();
6188 }
6189}
6190
6191/*
6192 * ":echohl {name}".
6193 */
6194 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006195ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006197 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198}
6199
6200/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006201 * Returns the :echo attribute
6202 */
6203 int
6204get_echo_attr(void)
6205{
6206 return echo_attr;
6207}
6208
6209/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 * ":execute expr1 ..." execute the result of an expression.
6211 * ":echomsg expr1 ..." Print a message
6212 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006213 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 * Each gets spaces around each argument and a newline at the end for
6215 * echo commands
6216 */
6217 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006218ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219{
6220 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006221 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 int ret = OK;
6223 char_u *p;
6224 garray_T ga;
6225 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006226 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227
6228 ga_init2(&ga, 1, 80);
6229
6230 if (eap->skip)
6231 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006232 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006234 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006235 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237
6238 if (!eap->skip)
6239 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006240 char_u buf[NUMBUFLEN];
6241
6242 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006243 {
6244 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6245 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006246 semsg(_(e_using_invalid_value_as_string_str),
6247 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006248 p = NULL;
6249 }
6250 else
6251 p = tv_get_string_buf(&rettv, buf);
6252 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006253 else
6254 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006255 if (p == NULL)
6256 {
6257 clear_tv(&rettv);
6258 ret = FAIL;
6259 break;
6260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 len = (int)STRLEN(p);
6262 if (ga_grow(&ga, len + 2) == FAIL)
6263 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006264 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 ret = FAIL;
6266 break;
6267 }
6268 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 ga.ga_len += len;
6272 }
6273
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006274 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 arg = skipwhite(arg);
6276 }
6277
6278 if (ret != FAIL && ga.ga_data != NULL)
6279 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006280 // use the first line of continuation lines for messages
6281 SOURCING_LNUM = start_lnum;
6282
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006283 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6284 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006285 // Mark the already saved text as finishing the line, so that what
6286 // follows is displayed on a new line when scrolling back at the
6287 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006288 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006289 }
6290
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006292 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006293 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006294 out_flush();
6295 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006296 else if (eap->cmdidx == CMD_echoconsole)
6297 {
6298 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6299 ui_write((char_u *)"\r\n", 2, TRUE);
6300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 else if (eap->cmdidx == CMD_echoerr)
6302 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006303 int save_did_emsg = did_emsg;
6304
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006305 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006306 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 if (!force_abort)
6308 did_emsg = save_did_emsg;
6309 }
6310 else if (eap->cmdidx == CMD_execute)
6311 do_cmdline((char_u *)ga.ga_data,
6312 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6313 }
6314
6315 ga_clear(&ga);
6316
6317 if (eap->skip)
6318 --emsg_skip;
6319
6320 eap->nextcmd = check_nextcmd(arg);
6321}
6322
6323/*
6324 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6325 * "arg" points to the "&" or '+' when called, to "option" when returning.
6326 * Returns NULL when no option name found. Otherwise pointer to the char
6327 * after the option name.
6328 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006329 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006330find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331{
6332 char_u *p = *arg;
6333
6334 ++p;
6335 if (*p == 'g' && p[1] == ':')
6336 {
6337 *opt_flags = OPT_GLOBAL;
6338 p += 2;
6339 }
6340 else if (*p == 'l' && p[1] == ':')
6341 {
6342 *opt_flags = OPT_LOCAL;
6343 p += 2;
6344 }
6345 else
6346 *opt_flags = 0;
6347
6348 if (!ASCII_ISALPHA(*p))
6349 return NULL;
6350 *arg = p;
6351
6352 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006353 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 else
6355 while (ASCII_ISALPHA(*p))
6356 ++p;
6357 return p;
6358}
6359
6360/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006361 * Display script name where an item was last set.
6362 * Should only be invoked when 'verbose' is non-zero.
6363 */
6364 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006365last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006366{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006367 char_u *p;
6368
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006369 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006370 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006371 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006372 if (p != NULL)
6373 {
6374 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006375 msg_puts(_("\n\tLast set from "));
6376 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006377 if (script_ctx.sc_lnum > 0)
6378 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006379 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006380 msg_outnum((long)script_ctx.sc_lnum);
6381 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006382 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006383 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006384 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006385 }
6386}
6387
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006388#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389
6390/*
6391 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006392 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 * "flags" can be "g" to do a global substitute.
6394 * Returns an allocated string, NULL for error.
6395 */
6396 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006397do_string_sub(
6398 char_u *str,
6399 char_u *pat,
6400 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006401 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006402 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403{
6404 int sublen;
6405 regmatch_T regmatch;
6406 int i;
6407 int do_all;
6408 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006409 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 garray_T ga;
6411 char_u *ret;
6412 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006413 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006415 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006417 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418
6419 ga_init2(&ga, 1, 200);
6420
6421 do_all = (flags[0] == 'g');
6422
6423 regmatch.rm_ic = p_ic;
6424 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6425 if (regmatch.regprog != NULL)
6426 {
6427 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006428 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6430 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006431 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006432 if (regmatch.startp[0] == regmatch.endp[0])
6433 {
6434 if (zero_width == regmatch.startp[0])
6435 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006436 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006437 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006438 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6439 (size_t)i);
6440 ga.ga_len += i;
6441 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006442 continue;
6443 }
6444 zero_width = regmatch.startp[0];
6445 }
6446
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 /*
6448 * Get some space for a temporary buffer to do the substitution
6449 * into. It will contain:
6450 * - The text up to where the match is.
6451 * - The substituted text.
6452 * - The text after the match.
6453 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006454 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006455 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6457 {
6458 ga_clear(&ga);
6459 break;
6460 }
6461
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006462 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 i = (int)(regmatch.startp[0] - tail);
6464 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006465 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006466 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 + ga.ga_len + i, TRUE, TRUE, FALSE);
6468 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006469 tail = regmatch.endp[0];
6470 if (*tail == NUL)
6471 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 if (!do_all)
6473 break;
6474 }
6475
6476 if (ga.ga_data != NULL)
6477 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6478
Bram Moolenaar473de612013-06-08 18:19:48 +02006479 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 }
6481
6482 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6483 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006484 if (p_cpo == empty_option)
6485 p_cpo = save_cpo;
6486 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006487 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006488 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006489 // If it's still empty it was changed and restored, need to restore in
6490 // the complicated way.
6491 if (*p_cpo == NUL)
6492 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006493 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495
6496 return ret;
6497}