blob: bf65082e275496cb69f182e0a4cd46c5d3a4c301 [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 Moolenaar5409f5d2020-06-24 18:37:35 +020048static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
49static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
50static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
51static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
52static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +020053static int eval7t(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020054static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +020055static int eval7_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000056
Bram Moolenaar48e697e2016-01-23 22:17:30 +010057static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020058static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020059
Bram Moolenaare21c1582019-03-02 11:57:09 +010060/*
61 * Return "n1" divided by "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010062 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010063 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020064 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010065num_divide(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010066{
67 varnumber_T result;
68
Bram Moolenaar99880f92021-01-20 21:23:14 +010069 if (n2 == 0)
Bram Moolenaare21c1582019-03-02 11:57:09 +010070 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010071 if (in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010072 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010073 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010074 if (failed != NULL)
75 *failed = TRUE;
76 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010077 if (n1 == 0)
78 result = VARNUM_MIN; // similar to NaN
79 else if (n1 < 0)
80 result = -VARNUM_MAX;
81 else
82 result = VARNUM_MAX;
83 }
84 else
85 result = n1 / n2;
86
87 return result;
88}
89
90/*
91 * Return "n1" modulus "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010092 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010093 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020094 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010095num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010096{
Bram Moolenaar99880f92021-01-20 21:23:14 +010097 if (n2 == 0 && in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010098 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010099 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +0100100 if (failed != NULL)
101 *failed = TRUE;
102 }
Bram Moolenaare21c1582019-03-02 11:57:09 +0100103 return (n2 == 0) ? 0 : (n1 % n2);
104}
105
Bram Moolenaar33570922005-01-25 22:26:29 +0000106/*
107 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000108 */
109 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100110eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000111{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200112 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200113 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000114
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200115#ifdef EBCDIC
116 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100117 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200118 */
119 sortFunctions();
120#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000121}
122
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000123#if defined(EXITFREE) || defined(PROTO)
124 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100125eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000126{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200127 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100128 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200129 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000130
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200131 // autoloaded script names
132 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000133
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200134 // unreferenced lists and dicts
135 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200136
137 // functions not garbage collected
138 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000139}
140#endif
141
Bram Moolenaare6b53242020-07-01 17:28:33 +0200142 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200143fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
144{
Bram Moolenaar844fb642021-10-23 13:32:30 +0100145 init_evalarg(evalarg);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200146 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200147 if (eap != NULL)
Bram Moolenaar37c83712020-06-30 21:18:36 +0200148 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200149 evalarg->eval_cstack = eap->cstack;
150 if (getline_equal(eap->getline, eap->cookie, getsourceline))
151 {
152 evalarg->eval_getline = eap->getline;
153 evalarg->eval_cookie = eap->cookie;
154 }
Bram Moolenaar37c83712020-06-30 21:18:36 +0200155 }
156}
157
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158/*
159 * Top level evaluation function, returning a boolean.
160 * Sets "error" to TRUE if there was an error.
161 * Return TRUE or FALSE.
162 */
163 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100164eval_to_bool(
165 char_u *arg,
166 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200167 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100168 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169{
Bram Moolenaar33570922005-01-25 22:26:29 +0000170 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200171 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200172 evalarg_T evalarg;
173
Bram Moolenaar37c83712020-06-30 21:18:36 +0200174 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175
176 if (skip)
177 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200178 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 else
181 {
182 *error = FALSE;
183 if (!skip)
184 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200185 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200186 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200187 else
188 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000189 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 }
191 }
192 if (skip)
193 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200194 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200196 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197}
198
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100199/*
200 * Call eval1() and give an error message if not done at a lower level.
201 */
202 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200203eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100204{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100205 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100206 int ret;
207 int did_emsg_before = did_emsg;
208 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200209 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100210
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200211 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
212
213 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100214 if (ret == FAIL)
215 {
216 // Report the invalid expression unless the expression evaluation has
217 // been cancelled due to an aborting error, an interrupt, or an
218 // exception, or we already gave a more specific error.
219 // Also check called_emsg for when using assert_fails().
220 if (!aborting() && did_emsg == did_emsg_before
221 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200222 semsg(_(e_invalid_expression_str), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100223 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200224 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100225 return ret;
226}
227
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100228/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200229 * Return whether a typval is a valid expression to pass to eval_expr_typval()
230 * or eval_expr_to_bool(). An empty string returns FALSE;
231 */
232 int
233eval_expr_valid_arg(typval_T *tv)
234{
235 return tv->v_type != VAR_UNKNOWN
236 && (tv->v_type != VAR_STRING
237 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
238}
239
240/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100241 * Evaluate an expression, which can be a function, partial or string.
242 * Pass arguments "argv[argc]".
243 * Return the result in "rettv" and OK or FAIL.
244 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200245 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100246eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
247{
248 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100249 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200250 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100251
252 if (expr->v_type == VAR_FUNC)
253 {
254 s = expr->vval.v_string;
255 if (s == NULL || *s == NUL)
256 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200257 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000258 funcexe.fe_evaluate = TRUE;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200259 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100260 return FAIL;
261 }
262 else if (expr->v_type == VAR_PARTIAL)
263 {
264 partial_T *partial = expr->vval.v_partial;
265
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200266 if (partial == NULL)
267 return FAIL;
268
Bram Moolenaar822ba242020-05-24 23:00:18 +0200269 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200270 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200272 if (call_def_function(partial->pt_func, argc, argv,
273 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100274 return FAIL;
275 }
276 else
277 {
278 s = partial_name(partial);
279 if (s == NULL || *s == NUL)
280 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200281 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000282 funcexe.fe_evaluate = TRUE;
283 funcexe.fe_partial = partial;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100284 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
285 return FAIL;
286 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100287 }
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200288 else if (expr->v_type == VAR_INSTR)
289 {
290 return exe_typval_instr(expr, rettv);
291 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100292 else
293 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100294 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100295 if (s == NULL)
296 return FAIL;
297 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200298 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100299 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200300 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100301 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200302 clear_tv(rettv);
Bram Moolenaar108010a2021-06-27 22:03:33 +0200303 semsg(_(e_invalid_expression_str), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100304 return FAIL;
305 }
306 }
307 return OK;
308}
309
310/*
311 * Like eval_to_bool() but using a typval_T instead of a string.
312 * Works for string, funcref and partial.
313 */
314 int
315eval_expr_to_bool(typval_T *expr, int *error)
316{
317 typval_T rettv;
318 int res;
319
320 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
321 {
322 *error = TRUE;
323 return FALSE;
324 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200325 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100326 clear_tv(&rettv);
327 return res;
328}
329
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330/*
331 * Top level evaluation function, returning a string. If "skip" is TRUE,
332 * only parsing to "nextcmd" is done, without reporting errors. Return
333 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
334 */
335 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100336eval_to_string_skip(
337 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200338 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100339 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340{
Bram Moolenaar33570922005-01-25 22:26:29 +0000341 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200343 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344
Bram Moolenaar37c83712020-06-30 21:18:36 +0200345 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 if (skip)
347 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200348 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 retval = NULL;
350 else
351 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100352 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000353 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 }
355 if (skip)
356 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200357 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358
359 return retval;
360}
361
362/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000363 * Skip over an expression at "*pp".
364 * Return FAIL for an error, OK otherwise.
365 */
366 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200367skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000368{
Bram Moolenaar33570922005-01-25 22:26:29 +0000369 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000370
371 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200372 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000373}
374
375/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200376 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200377 * If in Vim9 script and line breaks are encountered, the lines are
378 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200379 * "arg" is advanced to just after the expression.
380 * "start" is set to the start of the expression, "end" to just after the end.
381 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200382 * Return FAIL for an error, OK otherwise.
383 */
384 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200385skip_expr_concatenate(
386 char_u **arg,
387 char_u **start,
388 char_u **end,
389 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200390{
391 typval_T rettv;
392 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200393 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200394 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200395 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200396 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200397 int evaluate = evalarg == NULL
398 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200399
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200400 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200401 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200402 {
403 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200404 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200405 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200406 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200407 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200408 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200409 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200410
411 // Don't evaluate the expression.
412 if (evalarg != NULL)
413 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200414 *arg = skipwhite(*arg);
415 res = eval1(arg, &rettv, evalarg);
416 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200417 if (evalarg != NULL)
418 evalarg->eval_flags = save_flags;
419
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200420 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200421 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200422 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200423 if (evalarg->eval_ga.ga_len == 1)
424 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200425 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200426 ga_clear(gap);
427 gap->ga_itemsize = 0;
428 }
429 else
430 {
431 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200432 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200433
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200434 // Line breaks encountered, concatenate all the lines.
435 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200436 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200437
438 // free the lines only when using getsourceline()
439 if (evalarg->eval_cookie != NULL)
440 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200441 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200442 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200443 // Do not free the last line, "arg" points into it, free it
444 // later.
445 vim_free(evalarg->eval_tofree);
446 evalarg->eval_tofree =
447 ((char_u **)gap->ga_data)[gap->ga_len - 1];
448 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200449 ga_clear_strings(gap);
450 }
451 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200452 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200453 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200454
455 // free lines that were explicitly marked for freeing
456 ga_clear_strings(freegap);
457 }
458
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200459 gap->ga_itemsize = 0;
460 if (p == NULL)
461 return FAIL;
462 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200463 vim_free(evalarg->eval_tofree_lambda);
464 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200465 // Compute "end" relative to the end.
466 *end = *start + STRLEN(*start) - endoff;
467 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200468 }
469
470 return res;
471}
472
473/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100474 * Convert "tv" to a string.
475 * When "convert" is TRUE convert a List into a sequence of lines and convert
476 * a Float to a String.
477 * Returns an allocated string (NULL when out of memory).
478 */
479 char_u *
480typval2string(typval_T *tv, int convert)
481{
482 garray_T ga;
483 char_u *retval;
484#ifdef FEAT_FLOAT
485 char_u numbuf[NUMBUFLEN];
486#endif
487
488 if (convert && tv->v_type == VAR_LIST)
489 {
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000490 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaar883cf972021-01-15 18:04:43 +0100491 if (tv->vval.v_list != NULL)
492 {
493 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
494 if (tv->vval.v_list->lv_len > 0)
495 ga_append(&ga, NL);
496 }
497 ga_append(&ga, NUL);
498 retval = (char_u *)ga.ga_data;
499 }
500#ifdef FEAT_FLOAT
501 else if (convert && tv->v_type == VAR_FLOAT)
502 {
503 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
504 retval = vim_strsave(numbuf);
505 }
506#endif
507 else
508 retval = vim_strsave(tv_get_string(tv));
509 return retval;
510}
511
512/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200513 * Top level evaluation function, returning a string. Does not handle line
514 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000515 * When "convert" is TRUE convert a List into a sequence of lines and convert
516 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 * Return pointer to allocated memory, or NULL for failure.
518 */
519 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100520eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100521 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100522 int convert,
523 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524{
Bram Moolenaar33570922005-01-25 22:26:29 +0000525 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100527 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100529 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
530 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 retval = NULL;
532 else
533 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100534 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000535 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100537 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538
539 return retval;
540}
541
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100542 char_u *
543eval_to_string(
544 char_u *arg,
545 int convert)
546{
547 return eval_to_string_eap(arg, convert, NULL);
548}
549
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000551 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200552 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200553 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 */
555 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100556eval_to_string_safe(
557 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100558 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559{
560 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200561 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200562 int save_sc_version = current_sctx.sc_version;
Christian Brabandt070ac342021-09-09 12:12:03 +0200563 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200565 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200566 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000567 if (use_sandbox)
568 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200569 ++textwinlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200570 may_garbage_collect = FALSE;
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;
Christian Brabandt070ac342021-09-09 12:12:03 +0200575 may_garbage_collect = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200576 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200577 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 return retval;
579}
580
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581/*
582 * Top level evaluation function, returning a number.
583 * Evaluates "expr" silently.
584 * Returns -1 for an error.
585 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200586 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100587eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588{
Bram Moolenaar33570922005-01-25 22:26:29 +0000589 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200590 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000591 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592
593 ++emsg_off;
594
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200595 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 retval = -1;
597 else
598 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100599 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000600 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 }
602 --emsg_off;
603
604 return retval;
605}
606
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000607/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000608 * Top level evaluation function.
609 * Returns an allocated typval_T with the result.
610 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000611 */
612 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200613eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000614{
615 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200616 evalarg_T evalarg;
617
618 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000619
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200620 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200621 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100622 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000623
Bram Moolenaar37c83712020-06-30 21:18:36 +0200624 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000625 return tv;
626}
627
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628/*
Bram Moolenaar15d16352022-01-17 20:09:08 +0000629 * "*arg" points to what can be a function name in the form of "import.Name" or
630 * "Funcref". Return the name of the function. Set "tofree" to something that
631 * was allocated.
632 * If "verbose" is FALSE no errors are given.
633 * Return NULL for any failure.
634 */
635 static char_u *
636deref_function_name(
637 char_u **arg,
638 char_u **tofree,
639 evalarg_T *evalarg,
640 int verbose)
641{
642 typval_T ref;
643 char_u *name = *arg;
644
645 ref.v_type = VAR_UNKNOWN;
646 if (eval7(arg, &ref, evalarg, FALSE) == FAIL)
647 return NULL;
648 if (*skipwhite(*arg) != NUL)
649 {
650 if (verbose)
651 semsg(_(e_trailing_characters_str), *arg);
652 name = NULL;
653 }
654 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
655 {
656 name = ref.vval.v_string;
657 ref.vval.v_string = NULL;
658 *tofree = name;
659 }
660 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
661 {
662 if (ref.vval.v_partial->pt_argc > 0
663 || ref.vval.v_partial->pt_dict != NULL)
664 {
665 if (verbose)
666 emsg(_(e_cannot_use_partial_here));
667 name = NULL;
668 }
669 else
670 {
671 name = vim_strsave(partial_name(ref.vval.v_partial));
672 *tofree = name;
673 }
674 }
675 else
676 {
677 if (verbose)
678 semsg(_(e_not_callable_type_str), name);
679 name = NULL;
680 }
681 clear_tv(&ref);
682 return name;
683}
684
685/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100686 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200687 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
688 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000689 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200691 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100692call_vim_function(
693 char_u *func,
694 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200695 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200696 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000698 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200699 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000700 char_u *arg;
701 char_u *name;
702 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100704 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200705 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000706 funcexe.fe_firstline = curwin->w_cursor.lnum;
707 funcexe.fe_lastline = curwin->w_cursor.lnum;
708 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000709
710 // The name might be "import.Func" or "Funcref".
711 arg = func;
Bram Moolenaarb3d9cee2022-01-17 21:13:28 +0000712 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000713 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarb3d9cee2022-01-17 21:13:28 +0000714 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000715 if (name == NULL)
716 name = func;
717
718 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
719
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000720 if (ret == FAIL)
721 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000722 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000723
724 return ret;
725}
726
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100727/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100728 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000729 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
730 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000731 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000732 */
733 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100734call_func_retstr(
735 char_u *func,
736 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200737 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000738{
739 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000740 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000741
Bram Moolenaarded27a12018-08-01 19:06:03 +0200742 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000743 return NULL;
744
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100745 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000746 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 return retval;
748}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000749
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000750/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100751 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000752 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000753 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000754 */
755 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100756call_func_retlist(
757 char_u *func,
758 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200759 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000760{
761 typval_T rettv;
762
Bram Moolenaarded27a12018-08-01 19:06:03 +0200763 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000764 return NULL;
765
766 if (rettv.v_type != VAR_LIST)
767 {
768 clear_tv(&rettv);
769 return NULL;
770 }
771
772 return rettv.vval.v_list;
773}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775#ifdef FEAT_FOLDING
776/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100777 * Evaluate "arg", which is 'foldexpr'.
778 * Note: caller must set "curwin" to match "arg".
779 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
780 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 */
782 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100783eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784{
Bram Moolenaar33570922005-01-25 22:26:29 +0000785 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200786 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000788 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
789 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790
791 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000792 if (use_sandbox)
793 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200794 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200796 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 retval = 0;
798 else
799 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100800 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000801 if (tv.v_type == VAR_NUMBER)
802 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000803 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 retval = 0;
805 else
806 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100807 // If the result is a string, check if there is a non-digit before
808 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000809 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 if (!VIM_ISDIGIT(*s) && *s != '-')
811 *cp = *s++;
812 retval = atol((char *)s);
813 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000814 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 }
816 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000817 if (use_sandbox)
818 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200819 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200820 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200822 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823}
824#endif
825
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000827 * Get an lval: variable, Dict item or List item that can be assigned a value
828 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
829 * "name.key", "name.key[expr]" etc.
830 * Indexing only works if "name" is an existing List or Dictionary.
831 * "name" points to the start of the name.
832 * If "rettv" is not NULL it points to the value to be assigned.
833 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
834 * wrong; must end in space or cmd separator.
835 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100836 * flags:
837 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100838 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100839 * GLV_NO_AUTOLOAD: do not use script autoloading
840 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000841 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000842 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000843 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000844 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200845 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100846get_lval(
847 char_u *name,
848 typval_T *rettv,
849 lval_T *lp,
850 int unlet,
851 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100852 int flags, // GLV_ values
853 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000854{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000855 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000856 char_u *expr_start, *expr_end;
857 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000858 dictitem_T *v;
859 typval_T var1;
860 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000861 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000862 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000863 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100864 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100865 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100866 int writing;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000867
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100868 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200869 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000870
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100871 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000872 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100873 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000874 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200875 lp->ll_name_end = find_name_end(name, NULL, NULL,
876 FNE_INCL_BR | fne_flags);
877 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000878 }
879
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100880 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000881 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100882 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000883 if (expr_start != NULL)
884 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100885 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100886 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000887 && *p != '[' && *p != '.')
888 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000889 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000890 return NULL;
891 }
892
893 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
894 if (lp->ll_exp_name == NULL)
895 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100896 // Report an invalid expression in braces, unless the
897 // expression evaluation has been cancelled due to an
898 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000899 if (!aborting() && !quiet)
900 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000901 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000902 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000903 return NULL;
904 }
905 }
906 lp->ll_name = lp->ll_exp_name;
907 }
908 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100909 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000910 lp->ll_name = name;
911
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200912 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100913 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200914 // "a: type" is declaring variable "a" with a type, not "a:".
915 if (p == name + 2 && p[-1] == ':')
916 {
917 --p;
918 lp->ll_name_end = p;
919 }
920 if (*p == ':')
921 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000922 garray_T tmp_type_list;
923 garray_T *type_list;
924 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +0200925
926 if (tp == p + 1 && !quiet)
927 {
928 semsg(_(e_white_space_required_after_str_str), ":", p);
929 return NULL;
930 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100931
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000932 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
933 type_list = &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list;
934 else
935 {
936 type_list = &tmp_type_list;
937 ga_init2(type_list, sizeof(type_T), 10);
938 }
939
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200940 // parse the type after the name
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000941 lp->ll_type = parse_type(&tp, type_list, !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100942 if (lp->ll_type == NULL && !quiet)
943 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200944 lp->ll_name_end = tp;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000945
946 // drop the type when not in a script
947 if (type_list == &tmp_type_list)
948 {
949 lp->ll_type = NULL;
950 clear_type_list(type_list);
951 }
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200952 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100953 }
954 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000955 if (lp->ll_name == NULL)
956 return p;
957
958 if (*p == '.' && in_vim9script())
959 {
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000960 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name,
961 TRUE, NULL);
962
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000963 if (import != NULL)
964 {
965 ufunc_T *ufunc;
966 type_T *type;
967
968 lp->ll_sid = import->imp_sid;
969 lp->ll_name = skipwhite(p + 1);
970 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
971 lp->ll_name_end = p;
972
973 // check the item is exported
974 cc = *p;
975 *p = NUL;
976 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
977 NULL, TRUE) == -1)
978 {
979 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +0000980 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000981 }
982 *p = cc;
983 }
984 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100985
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100986 // Without [idx] or .key we are done.
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000987 if ((*p != '[' && *p != '.'))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000988 return p;
989
Bram Moolenaaraacc9662021-08-13 19:40:51 +0200990 if (in_vim9script() && lval_root != NULL)
991 {
992 // using local variable
993 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +0200994 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +0200995 }
996 else
997 {
998 cc = *p;
999 *p = NUL;
1000 // When we would write to the variable pass &ht and prevent autoload.
1001 writing = !(flags & GLV_READ_ONLY);
1002 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001003 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001004 if (v == NULL && !quiet)
1005 semsg(_(e_undefined_variable_str), lp->ll_name);
1006 *p = cc;
1007 if (v == NULL)
1008 return NULL;
1009 lp->ll_tv = &v->di_tv;
1010 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001011
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001012 if (in_vim9script() && (flags & GLV_NO_DECL) == 0)
1013 {
1014 if (!quiet)
1015 semsg(_(e_variable_already_declared), lp->ll_name);
1016 return NULL;
1017 }
1018
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001019 /*
1020 * Loop until no more [idx] or .key is following.
1021 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001022 var1.v_type = VAR_UNKNOWN;
1023 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001024 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001025 {
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001026 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
1027 {
1028 if (!quiet)
1029 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
1030 return NULL;
1031 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001032 if (lp->ll_tv->v_type != VAR_LIST
1033 && lp->ll_tv->v_type != VAR_DICT
1034 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001035 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001036 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001037 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001038 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001039 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001040
1041 // a NULL list/blob works like an empty list/blob, allocate one now.
1042 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
1043 rettv_list_alloc(lp->ll_tv);
1044 else if (lp->ll_tv->v_type == VAR_BLOB
1045 && lp->ll_tv->vval.v_blob == NULL)
1046 rettv_blob_alloc(lp->ll_tv);
1047
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001048 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001049 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001050 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001051 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001052 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001053 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001054
Bram Moolenaar10c65862020-10-08 21:16:42 +02001055 if (in_vim9script() && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001056 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001057 && lp->ll_tv == &v->di_tv
1058 && ht != NULL && ht == get_script_local_ht())
1059 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001060 svar_T *sv = find_typval_in_script(lp->ll_tv, 0);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001061
1062 // Vim9 script local variable: get the type
1063 if (sv != NULL)
1064 lp->ll_valtype = sv->sv_type;
1065 }
1066
Bram Moolenaar8c711452005-01-14 21:53:12 +00001067 len = -1;
1068 if (*p == '.')
1069 {
1070 key = p + 1;
1071 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1072 ;
1073 if (len == 0)
1074 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001075 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001076 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001077 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001078 }
1079 p = key + len;
1080 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001081 else
1082 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001083 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001084 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001085 if (*p == ':')
1086 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001087 else
1088 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001089 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001090 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001091 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001092 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001093 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001094 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001095 clear_tv(&var1);
1096 return NULL;
1097 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001098 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001099 }
1100
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001101 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001102 if (*p == ':')
1103 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001104 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001105 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001106 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001107 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001108 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001109 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001110 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001111 if (rettv != NULL
1112 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001113 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001114 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001115 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001116 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001117 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001118 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001119 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001120 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001121 }
1122 p = skipwhite(p + 1);
1123 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001124 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001125 else
1126 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001127 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001128 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001129 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001130 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001131 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001132 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001133 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001134 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001135 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001136 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001137 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001138 clear_tv(&var2);
1139 return NULL;
1140 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001141 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001142 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001143 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001144 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001145 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001146
Bram Moolenaar8c711452005-01-14 21:53:12 +00001147 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001148 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001149 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001150 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001151 clear_tv(&var1);
1152 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001153 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001154 }
1155
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001156 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001157 ++p;
1158 }
1159
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001160 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001161 {
1162 if (len == -1)
1163 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001164 // "[key]": get key from "var1"
1165 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001166 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001167 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001168 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001169 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001170 }
1171 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001172 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001173
1174 // a NULL dict is equivalent with an empty dict
1175 if (lp->ll_tv->vval.v_dict == NULL)
1176 {
1177 lp->ll_tv->vval.v_dict = dict_alloc();
1178 if (lp->ll_tv->vval.v_dict == NULL)
1179 {
1180 clear_tv(&var1);
1181 return NULL;
1182 }
1183 ++lp->ll_tv->vval.v_dict->dv_refcount;
1184 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001185 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001186
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001187 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001188
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001189 // When assigning to a scope dictionary check that a function and
1190 // variable name is valid (only variable name unless it is l: or
1191 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001192 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001193 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001194 int prevval;
1195 int wrong;
1196
1197 if (len != -1)
1198 {
1199 prevval = key[len];
1200 key[len] = NUL;
1201 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001202 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001203 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001204 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1205 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001206 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001207 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001208 if (len != -1)
1209 key[len] = prevval;
1210 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001211 {
1212 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001213 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001214 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001215 }
1216
Bram Moolenaar10c65862020-10-08 21:16:42 +02001217 if (lp->ll_valtype != NULL)
1218 // use the type of the member
1219 lp->ll_valtype = lp->ll_valtype->tt_member;
1220
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001221 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001222 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001223 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001224 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001225 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001226 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001227 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001228 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001229 return NULL;
1230 }
1231
Bram Moolenaar31b81602019-02-10 22:14:27 +01001232 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001233 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001234 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001235 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001236 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001237 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001238 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001239 }
1240 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001241 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001242 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001243 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001244 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001245 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001246 p = NULL;
1247 break;
1248 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001249 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001250 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001251 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1252 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001253 {
1254 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001255 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001256 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001257
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001258 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001259 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001260 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001261 else if (lp->ll_tv->v_type == VAR_BLOB)
1262 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001263 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1264
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001265 /*
1266 * Get the number and item for the only or first index of the List.
1267 */
1268 if (empty1)
1269 lp->ll_n1 = 0;
1270 else
1271 // is number or string
1272 lp->ll_n1 = (long)tv_get_number(&var1);
1273 clear_tv(&var1);
1274
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001275 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001276 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001277 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001278 return NULL;
1279 }
1280 if (lp->ll_range && !lp->ll_empty2)
1281 {
1282 lp->ll_n2 = (long)tv_get_number(&var2);
1283 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001284 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1285 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001286 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001287 }
1288 lp->ll_blob = lp->ll_tv->vval.v_blob;
1289 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001290 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001291 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001292 else
1293 {
1294 /*
1295 * Get the number and item for the only or first index of the List.
1296 */
1297 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001298 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001299 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001300 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001301 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001302 clear_tv(&var1);
1303
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001304 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001305 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001306 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001307 if (lp->ll_li == NULL)
1308 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001309 clear_tv(&var2);
1310 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001311 }
1312
Bram Moolenaar10c65862020-10-08 21:16:42 +02001313 if (lp->ll_valtype != NULL)
1314 // use the type of the member
1315 lp->ll_valtype = lp->ll_valtype->tt_member;
1316
Bram Moolenaar8c711452005-01-14 21:53:12 +00001317 /*
1318 * May need to find the item or absolute index for the second
1319 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001320 * When no index given: "lp->ll_empty2" is TRUE.
1321 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001322 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001323 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001324 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001325 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001326 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001327 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001328 if (check_range_index_two(lp->ll_list,
1329 &lp->ll_n1, lp->ll_li,
1330 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001331 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001332 }
1333
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001334 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001335 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001336 }
1337
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001338 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001339 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001340 return p;
1341}
1342
1343/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001344 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001345 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001346 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001347clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001348{
1349 vim_free(lp->ll_exp_name);
1350 vim_free(lp->ll_newkey);
1351}
1352
1353/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001354 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001355 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001356 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1357 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001358 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001359 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001360set_var_lval(
1361 lval_T *lp,
1362 char_u *endp,
1363 typval_T *rettv,
1364 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001365 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1366 char_u *op,
1367 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001368{
1369 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001370 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001371
1372 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001373 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001374 cc = *endp;
1375 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001376 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1377 return;
1378
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001379 if (lp->ll_blob != NULL)
1380 {
1381 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001382
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001383 if (op != NULL && *op != '=')
1384 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001385 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001386 return;
1387 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001388 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001389 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001390
1391 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1392 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001393 if (lp->ll_empty2)
1394 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1395
Bram Moolenaar68452172021-04-12 21:21:02 +02001396 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1397 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001398 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001399 }
1400 else
1401 {
1402 val = (int)tv_get_number_chk(rettv, &error);
1403 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001404 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001405 }
1406 }
1407 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001408 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001409 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001410
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001411 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1412 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001413 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001414 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001415 *endp = cc;
1416 return;
1417 }
1418
Bram Moolenaarff697e62019-02-12 22:28:33 +01001419 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001420 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001421 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001422 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001423 {
1424 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001425 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1426 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001427 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001428 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001429 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001430 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001431 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001432 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001433 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001434 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001435 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1436 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001437 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001438 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001439 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001440 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001441 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001442 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001443 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001444 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001445 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001446 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001447 else if (lp->ll_range)
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 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001452 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001453 return;
1454 }
1455
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001456 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1457 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001458 }
1459 else
1460 {
1461 /*
1462 * Assign to a List or Dictionary item.
1463 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001464 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1465 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001466 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001467 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001468 return;
1469 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001470
1471 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001472 && check_typval_arg_type(lp->ll_valtype, rettv,
1473 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001474 return;
1475
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001476 if (lp->ll_newkey != NULL)
1477 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001478 if (op != NULL && *op != '=')
1479 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001480 semsg(_(e_key_not_present_in_dictionary), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001481 return;
1482 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001483 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1484 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001485 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001486
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001487 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001488 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001489 if (di == NULL)
1490 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001491 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1492 {
1493 vim_free(di);
1494 return;
1495 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001496 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001497 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001498 else if (op != NULL && *op != '=')
1499 {
1500 tv_op(lp->ll_tv, rettv, op);
1501 return;
1502 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001503 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001504 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001505
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001506 /*
1507 * Assign the value to the variable or list item.
1508 */
1509 if (copy)
1510 copy_tv(rettv, lp->ll_tv);
1511 else
1512 {
1513 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001514 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001515 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001516 }
1517 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001518}
1519
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001520/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001521 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1522 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001523 * Returns OK or FAIL.
1524 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001525 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001526tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001527{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001528 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001529 char_u numbuf[NUMBUFLEN];
1530 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001531 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001532
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001533 // Can't do anything with a Funcref or Dict on the right.
1534 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001535 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001536 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1537 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001538 {
1539 switch (tv1->v_type)
1540 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001541 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001542 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001543 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001544 case VAR_DICT:
1545 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001546 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001547 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001548 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001549 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001550 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001551 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001552 break;
1553
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001554 case VAR_BLOB:
1555 if (*op != '+' || tv2->v_type != VAR_BLOB)
1556 break;
1557 // BLOB += BLOB
1558 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1559 {
1560 blob_T *b1 = tv1->vval.v_blob;
1561 blob_T *b2 = tv2->vval.v_blob;
1562 int i, len = blob_len(b2);
1563 for (i = 0; i < len; i++)
1564 ga_append(&b1->bv_ga, blob_get(b2, i));
1565 }
1566 return OK;
1567
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001568 case VAR_LIST:
1569 if (*op != '+' || tv2->v_type != VAR_LIST)
1570 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001571 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001572 if (tv2->vval.v_list != NULL)
1573 {
1574 if (tv1->vval.v_list == NULL)
1575 {
1576 tv1->vval.v_list = tv2->vval.v_list;
1577 ++tv1->vval.v_list->lv_refcount;
1578 }
1579 else
1580 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1581 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001582 return OK;
1583
1584 case VAR_NUMBER:
1585 case VAR_STRING:
1586 if (tv2->v_type == VAR_LIST)
1587 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001588 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001589 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001590 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001591 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001592#ifdef FEAT_FLOAT
1593 if (tv2->v_type == VAR_FLOAT)
1594 {
1595 float_T f = n;
1596
Bram Moolenaarff697e62019-02-12 22:28:33 +01001597 if (*op == '%')
1598 break;
1599 switch (*op)
1600 {
1601 case '+': f += tv2->vval.v_float; break;
1602 case '-': f -= tv2->vval.v_float; break;
1603 case '*': f *= tv2->vval.v_float; break;
1604 case '/': f /= tv2->vval.v_float; break;
1605 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001606 clear_tv(tv1);
1607 tv1->v_type = VAR_FLOAT;
1608 tv1->vval.v_float = f;
1609 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001610 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001611#endif
1612 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001613 switch (*op)
1614 {
1615 case '+': n += tv_get_number(tv2); break;
1616 case '-': n -= tv_get_number(tv2); break;
1617 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001618 case '/': n = num_divide(n, tv_get_number(tv2),
1619 &failed); break;
1620 case '%': n = num_modulus(n, tv_get_number(tv2),
1621 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001622 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001623 clear_tv(tv1);
1624 tv1->v_type = VAR_NUMBER;
1625 tv1->vval.v_number = n;
1626 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001627 }
1628 else
1629 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001630 if (tv2->v_type == VAR_FLOAT)
1631 break;
1632
Bram Moolenaarff697e62019-02-12 22:28:33 +01001633 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001634 s = tv_get_string(tv1);
1635 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001636 clear_tv(tv1);
1637 tv1->v_type = VAR_STRING;
1638 tv1->vval.v_string = s;
1639 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001640 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001641
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001642 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001643#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001644 {
1645 float_T f;
1646
Bram Moolenaarff697e62019-02-12 22:28:33 +01001647 if (*op == '%' || *op == '.'
1648 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001649 && tv2->v_type != VAR_NUMBER
1650 && tv2->v_type != VAR_STRING))
1651 break;
1652 if (tv2->v_type == VAR_FLOAT)
1653 f = tv2->vval.v_float;
1654 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001655 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001656 switch (*op)
1657 {
1658 case '+': tv1->vval.v_float += f; break;
1659 case '-': tv1->vval.v_float -= f; break;
1660 case '*': tv1->vval.v_float *= f; break;
1661 case '/': tv1->vval.v_float /= f; break;
1662 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001663 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001664#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001665 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001666 }
1667 }
1668
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001669 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001670 return FAIL;
1671}
1672
1673/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001674 * Evaluate the expression used in a ":for var in expr" command.
1675 * "arg" points to "var".
1676 * Set "*errp" to TRUE for an error, FALSE otherwise;
1677 * Return a pointer that holds the info. Null when there is an error.
1678 */
1679 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001680eval_for_line(
1681 char_u *arg,
1682 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001683 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001684 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001685{
Bram Moolenaar33570922005-01-25 22:26:29 +00001686 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001687 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001688 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001689 typval_T tv;
1690 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001691 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001692
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001693 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001694
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001695 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001696 if (fi == NULL)
1697 return NULL;
1698
Bram Moolenaar404557e2021-07-05 21:41:48 +02001699 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1700 &fi->fi_semicolon, FALSE);
1701 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001702 return fi;
1703
Bram Moolenaar404557e2021-07-05 21:41:48 +02001704 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001705 if (expr[0] != 'i' || expr[1] != 'n'
1706 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001707 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001708 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1709 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1710 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001711 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001712 return fi;
1713 }
1714
1715 if (skip)
1716 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001717 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1718 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001719 {
1720 *errp = FALSE;
1721 if (!skip)
1722 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001723 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001724 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001725 l = tv.vval.v_list;
1726 if (l == NULL)
1727 {
1728 // a null list is like an empty list: do nothing
1729 clear_tv(&tv);
1730 }
1731 else
1732 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001733 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001734 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001735
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001736 // No need to increment the refcount, it's already set for
1737 // the list being used in "tv".
1738 fi->fi_list = l;
1739 list_add_watch(l, &fi->fi_lw);
1740 fi->fi_lw.lw_item = l->lv_first;
1741 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001742 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001743 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001744 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001745 fi->fi_bi = 0;
1746 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001747 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001748 typval_T btv;
1749
1750 // Make a copy, so that the iteration still works when the
1751 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001752 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001753 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001754 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001755 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001756 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001757 else if (tv.v_type == VAR_STRING)
1758 {
1759 fi->fi_byte_idx = 0;
1760 fi->fi_string = tv.vval.v_string;
1761 tv.vval.v_string = NULL;
1762 if (fi->fi_string == NULL)
1763 fi->fi_string = vim_strsave((char_u *)"");
1764 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001765 else
1766 {
Sean Dewar80d73952021-08-04 19:25:54 +02001767 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001768 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001769 }
1770 }
1771 }
1772 if (skip)
1773 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001774 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001775
1776 return fi;
1777}
1778
1779/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001780 * Used when looping over a :for line, skip the "in expr" part.
1781 */
1782 void
1783skip_for_lines(void *fi_void, evalarg_T *evalarg)
1784{
1785 forinfo_T *fi = (forinfo_T *)fi_void;
1786 int i;
1787
1788 for (i = 0; i < fi->fi_break_count; ++i)
1789 eval_next_line(evalarg);
1790}
1791
1792/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001793 * Use the first item in a ":for" list. Advance to the next.
1794 * Assign the values to the variable (list). "arg" points to the first one.
1795 * Return TRUE when a valid item was found, FALSE when at end of list or
1796 * something wrong.
1797 */
1798 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001799next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001800{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001801 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001802 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001803 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001804 ? (ASSIGN_FINAL
1805 // first round: error if variable exists
1806 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
1807 | ASSIGN_NO_MEMBER_TYPE)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001808 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001809 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001810 int skip_assign = in_vim9script() && arg[0] == '_'
1811 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001812
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001813 if (fi->fi_blob != NULL)
1814 {
1815 typval_T tv;
1816
1817 if (fi->fi_bi >= blob_len(fi->fi_blob))
1818 return FALSE;
1819 tv.v_type = VAR_NUMBER;
1820 tv.v_lock = VAR_FIXED;
1821 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1822 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001823 if (skip_assign)
1824 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001825 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001826 fi->fi_varcount, flag, NULL) == OK;
1827 }
1828
1829 if (fi->fi_string != NULL)
1830 {
1831 typval_T tv;
1832 int len;
1833
1834 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1835 if (len == 0)
1836 return FALSE;
1837 tv.v_type = VAR_STRING;
1838 tv.v_lock = VAR_FIXED;
1839 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1840 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001841 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001842 if (skip_assign)
1843 result = TRUE;
1844 else
1845 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001846 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001847 vim_free(tv.vval.v_string);
1848 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001849 }
1850
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001851 item = fi->fi_lw.lw_item;
1852 if (item == NULL)
1853 result = FALSE;
1854 else
1855 {
1856 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001857 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001858 if (skip_assign)
1859 result = TRUE;
1860 else
1861 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001862 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001863 }
1864 return result;
1865}
1866
1867/*
1868 * Free the structure used to store info used by ":for".
1869 */
1870 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001871free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001872{
Bram Moolenaar33570922005-01-25 22:26:29 +00001873 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001874
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001875 if (fi == NULL)
1876 return;
1877 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001878 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001879 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001880 list_unref(fi->fi_list);
1881 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001882 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001883 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001884 else
1885 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001886 vim_free(fi);
1887}
1888
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001890set_context_for_expression(
1891 expand_T *xp,
1892 char_u *arg,
1893 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001895 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001897 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001899 if (cmdidx == CMD_let || cmdidx == CMD_var
1900 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001901 {
1902 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001903 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001904 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001905 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001906 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001907 {
1908 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001909 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001910 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001911 break;
1912 }
1913 return;
1914 }
1915 }
1916 else
1917 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1918 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 while ((xp->xp_pattern = vim_strpbrk(arg,
1920 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1921 {
1922 c = *xp->xp_pattern;
1923 if (c == '&')
1924 {
1925 c = xp->xp_pattern[1];
1926 if (c == '&')
1927 {
1928 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001929 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 }
1931 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001932 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001934 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1935 xp->xp_pattern += 2;
1936
1937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 }
1939 else if (c == '$')
1940 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001941 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 xp->xp_context = EXPAND_ENV_VARS;
1943 }
1944 else if (c == '=')
1945 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001946 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 xp->xp_context = EXPAND_EXPRESSION;
1948 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001949 else if (c == '#'
1950 && xp->xp_context == EXPAND_EXPRESSION)
1951 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001952 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001953 break;
1954 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001955 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 && xp->xp_context == EXPAND_FUNCTIONS
1957 && vim_strchr(xp->xp_pattern, '(') == NULL)
1958 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001959 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 break;
1961 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001962 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001964 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 {
1966 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1967 if (c == '\\' && xp->xp_pattern[1] != NUL)
1968 ++xp->xp_pattern;
1969 xp->xp_context = EXPAND_NOTHING;
1970 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001971 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001973 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1975 /* skip */ ;
1976 xp->xp_context = EXPAND_NOTHING;
1977 }
1978 else if (c == '|')
1979 {
1980 if (xp->xp_pattern[1] == '|')
1981 {
1982 ++xp->xp_pattern;
1983 xp->xp_context = EXPAND_EXPRESSION;
1984 }
1985 else
1986 xp->xp_context = EXPAND_COMMANDS;
1987 }
1988 else
1989 xp->xp_context = EXPAND_EXPRESSION;
1990 }
1991 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001992 // Doesn't look like something valid, expand as an expression
1993 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001994 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 arg = xp->xp_pattern;
1996 if (*arg != NUL)
1997 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1998 /* skip */ ;
1999 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002000
2001 // ":exe one two" completes "two"
2002 if ((cmdidx == CMD_execute
2003 || cmdidx == CMD_echo
2004 || cmdidx == CMD_echon
2005 || cmdidx == CMD_echomsg)
2006 && xp->xp_context == EXPAND_EXPRESSION)
2007 {
2008 for (;;)
2009 {
2010 char_u *n = skiptowhite(arg);
2011
2012 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2013 break;
2014 arg = skipwhite(n);
2015 }
2016 }
2017
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 xp->xp_pattern = arg;
2019}
2020
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002022 * Return TRUE if "pat" matches "text".
2023 * Does not use 'cpo' and always uses 'magic'.
2024 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002025 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002026pattern_match(char_u *pat, char_u *text, int ic)
2027{
2028 int matches = FALSE;
2029 char_u *save_cpo;
2030 regmatch_T regmatch;
2031
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002032 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002033 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002034 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002035 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2036 if (regmatch.regprog != NULL)
2037 {
2038 regmatch.rm_ic = ic;
2039 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2040 vim_regfree(regmatch.regprog);
2041 }
2042 p_cpo = save_cpo;
2043 return matches;
2044}
2045
2046/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002047 * Handle a name followed by "(". Both for just "name(arg)" and for
2048 * "expr->name(arg)".
2049 * Returns OK or FAIL.
2050 */
2051 static int
2052eval_func(
2053 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002054 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002055 char_u *name,
2056 int name_len,
2057 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002058 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002059 typval_T *basetv) // "expr" for "expr->name(arg)"
2060{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002061 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002062 char_u *s = name;
2063 int len = name_len;
2064 partial_T *partial;
2065 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002066 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002067 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002068
2069 if (!evaluate)
2070 check_vars(s, len);
2071
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002072 // If "s" is the name of a variable of type VAR_FUNC
2073 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002074 s = deref_func_name(s, &len, &partial,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002075 in_vim9script() ? &type : NULL, !evaluate, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002076
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002077 // Need to make a copy, in case evaluating the arguments makes
2078 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002079 s = vim_strsave(s);
Bram Moolenaarb3341372021-12-14 18:57:45 +00002080 if (s == NULL || (evaluate && (*s == NUL || (flags & EVAL_CONSTANT))))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002081 ret = FAIL;
2082 else
2083 {
2084 funcexe_T funcexe;
2085
2086 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002087 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002088 funcexe.fe_firstline = curwin->w_cursor.lnum;
2089 funcexe.fe_lastline = curwin->w_cursor.lnum;
2090 funcexe.fe_evaluate = evaluate;
2091 funcexe.fe_partial = partial;
2092 funcexe.fe_basetv = basetv;
2093 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002094 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002095 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002096 }
2097 vim_free(s);
2098
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002099 // If evaluate is FALSE rettv->v_type was not set in
2100 // get_func_tv, but it's needed in handle_subscript() to parse
2101 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002102 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2103 {
2104 rettv->vval.v_string = NULL;
2105 rettv->v_type = VAR_FUNC;
2106 }
2107
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002108 // Stop the expression evaluation when immediately
2109 // aborting on error, or when an interrupt occurred or
2110 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002111 if (evaluate && aborting())
2112 {
2113 if (ret == OK)
2114 clear_tv(rettv);
2115 ret = FAIL;
2116 }
2117 return ret;
2118}
2119
2120/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002121 * Get the next line source line without advancing. But do skip over comment
2122 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002123 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002124 */
2125 static char_u *
2126getline_peek_skip_comments(evalarg_T *evalarg)
2127{
2128 for (;;)
2129 {
2130 char_u *next = getline_peek(evalarg->eval_getline,
2131 evalarg->eval_cookie);
2132 char_u *p;
2133
2134 if (next == NULL)
2135 break;
2136 p = skipwhite(next);
2137 if (*p != NUL && !vim9_comment_start(p))
2138 return next;
2139 (void)eval_next_line(evalarg);
2140 }
2141 return NULL;
2142}
2143
2144/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002145 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2146 * comment) and there is a next line, return the next line (skipping blanks)
2147 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002148 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2149 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002150 * "arg" must point somewhere inside a line, not at the start.
2151 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002152 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002153eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2154{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002155 char_u *p = skipwhite(arg);
2156
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002157 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002158 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002159 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002160 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002161 && (*p == NUL || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002162 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002163 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002164
2165 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002166 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002167 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002168 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002169
Bram Moolenaar9d489562020-07-30 20:08:50 +02002170 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002171 {
2172 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002173 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002174 }
2175 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002176 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002177}
2178
2179/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002180 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002181 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002182 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002183 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002184eval_next_line(evalarg_T *evalarg)
2185{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002186 garray_T *gap = &evalarg->eval_ga;
2187 char_u *line;
2188
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002189 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002190 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2191 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002192 else
2193 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002194 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002195 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2196 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002197 char_u *p = skipwhite(line);
2198
2199 // Going to concatenate the lines after parsing. For an empty or
2200 // comment line use an empty string.
2201 if (*p == NUL || vim9_comment_start(p))
2202 {
2203 vim_free(line);
2204 line = vim_strsave((char_u *)"");
2205 }
2206
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002207 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2208 ++gap->ga_len;
2209 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002210 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002211 {
2212 vim_free(evalarg->eval_tofree);
2213 evalarg->eval_tofree = line;
2214 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002215
2216 // Advanced to the next line, "arg" no longer points into the previous
2217 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002218 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002219 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002220}
2221
Bram Moolenaare6b53242020-07-01 17:28:33 +02002222/*
2223 * Call eval_next_non_blank() and get the next line if needed.
2224 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002225 char_u *
2226skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2227{
2228 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002229 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002230
Bram Moolenaar962d7212020-07-04 14:15:00 +02002231 if (evalarg == NULL)
2232 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002233 eval_next_non_blank(p, evalarg, &getnext);
2234 if (getnext)
2235 return eval_next_line(evalarg);
2236 return p;
2237}
2238
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002239/*
Bram Moolenaar844fb642021-10-23 13:32:30 +01002240 * Initialize "evalarg" for use.
2241 */
2242 void
2243init_evalarg(evalarg_T *evalarg)
2244{
2245 CLEAR_POINTER(evalarg);
2246 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
2247}
2248
2249/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002250 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002251 */
2252 void
2253clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2254{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002255 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002256 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002257 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002258 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002259 if (eap != NULL)
2260 {
2261 // We may need to keep the original command line, e.g. for
2262 // ":let" it has the variable names. But we may also need the
2263 // new one, "nextcmd" points into it. Keep both.
2264 vim_free(eap->cmdline_tofree);
2265 eap->cmdline_tofree = *eap->cmdlinep;
2266 *eap->cmdlinep = evalarg->eval_tofree;
2267 }
2268 else
2269 vim_free(evalarg->eval_tofree);
2270 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002271 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002272
Bram Moolenaar844fb642021-10-23 13:32:30 +01002273 ga_clear_strings(&evalarg->eval_tofree_ga);
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002274 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002275 }
2276}
2277
2278/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002280 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2282 */
2283
2284/*
2285 * Handle zero level expression.
2286 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002287 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002288 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002289 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 * Return OK or FAIL.
2291 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002292 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002293eval0(
2294 char_u *arg,
2295 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002296 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002297 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002299 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2300}
2301
2302/*
2303 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2304 * expression and don't check what comes after the expression.
2305 */
2306 int
2307eval0_retarg(
2308 char_u *arg,
2309 typval_T *rettv,
2310 exarg_T *eap,
2311 evalarg_T *evalarg,
2312 char_u **retarg)
2313{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 int ret;
2315 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002316 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002317 int did_emsg_before = did_emsg;
2318 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002319 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002320 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002321 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322
2323 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002324 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002325 expr_end = p;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002326 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002327
Bram Moolenaar02929a32021-12-17 14:46:12 +00002328 // In Vim9 script a command block is not split at NL characters for
2329 // commands using an expression argument. Skip over a '#' comment to check
2330 // for a following NL. Require white space before the '#'.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002331 if (in_vim9script() && p > expr_end && retarg == NULL)
Bram Moolenaar02929a32021-12-17 14:46:12 +00002332 while (*p == '#')
2333 {
2334 char_u *nl = vim_strchr(p, NL);
2335
2336 if (nl == NULL)
2337 break;
2338 p = skipwhite(nl + 1);
2339 if (eap != NULL && *p != NUL)
2340 eap->nextcmd = p;
2341 check_for_end = FALSE;
2342 }
2343
2344 if (ret != FAIL && check_for_end)
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002345 end_error = !ends_excmd2(arg, p);
2346 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 {
2348 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002349 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 /*
2351 * Report the invalid expression unless the expression evaluation has
2352 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002353 * exception, or we already gave a more specific error.
2354 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002356 if (!aborting()
2357 && did_emsg == did_emsg_before
2358 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002359 && (flags & EVAL_CONSTANT) == 0
2360 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002361 {
2362 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002363 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002364 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002365 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002366 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002367
2368 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002369 // a next command to avoid more errors, unless "|" is following, which
2370 // could only be a command separator.
2371 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2372 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002373 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002375
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002376 if (retarg != NULL)
2377 *retarg = p;
2378 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002379 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002380
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 return ret;
2382}
2383
2384/*
2385 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002386 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002387 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 *
2389 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002390 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002392 * Note: "rettv.v_lock" is not set.
2393 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 * Return OK or FAIL.
2395 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002396 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002397eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002399 char_u *p;
2400 int getnext;
2401
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002402 CLEAR_POINTER(rettv);
2403
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 /*
2405 * Get the first variable.
2406 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002407 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 return FAIL;
2409
Bram Moolenaar793648f2020-06-26 21:28:25 +02002410 p = eval_next_non_blank(*arg, evalarg, &getnext);
2411 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002413 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002414 int result;
2415 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002416 evalarg_T *evalarg_used = evalarg;
2417 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002418 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002419 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002420 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002421
2422 if (evalarg == NULL)
2423 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002424 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002425 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002426 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002427 orig_flags = evalarg_used->eval_flags;
2428 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002429
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002430 if (getnext)
2431 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002432 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002433 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002434 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002435 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002436 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002437 clear_tv(rettv);
2438 return FAIL;
2439 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002440 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002441 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002442
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002444 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002446 int error = FALSE;
2447
Bram Moolenaar13106602020-10-04 16:06:05 +02002448 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002449 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002450 else if (vim9script)
2451 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002452 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002454 if (error || !op_falsy || !result)
2455 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002456 if (error)
2457 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 }
2459
2460 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002461 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002463 if (op_falsy)
2464 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002465 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002466 {
mityu4ac198c2021-05-28 17:52:40 +02002467 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002468 clear_tv(rettv);
2469 return FAIL;
2470 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002471 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002472 evalarg_used->eval_flags = (op_falsy ? !result : result)
2473 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2474 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002475 {
2476 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002478 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002479 if (!op_falsy || !result)
2480 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002482 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002484 /*
2485 * Check for the ":".
2486 */
2487 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2488 if (*p != ':')
2489 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002490 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002491 if (evaluate && result)
2492 clear_tv(rettv);
2493 evalarg_used->eval_flags = orig_flags;
2494 return FAIL;
2495 }
2496 if (getnext)
2497 *arg = eval_next_line(evalarg_used);
2498 else
2499 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002500 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002501 {
2502 error_white_both(p, 1);
2503 clear_tv(rettv);
2504 evalarg_used->eval_flags = orig_flags;
2505 return FAIL;
2506 }
2507 *arg = p;
2508 }
2509
2510 /*
2511 * Get the third variable. Recursive!
2512 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002513 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002514 {
mityu4ac198c2021-05-28 17:52:40 +02002515 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002516 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002517 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002518 return FAIL;
2519 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002520 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2521 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002522 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002523 if (eval1(arg, &var2, evalarg_used) == FAIL)
2524 {
2525 if (evaluate && result)
2526 clear_tv(rettv);
2527 evalarg_used->eval_flags = orig_flags;
2528 return FAIL;
2529 }
2530 if (evaluate && !result)
2531 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002533
2534 if (evalarg == NULL)
2535 clear_evalarg(&local_evalarg, NULL);
2536 else
2537 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 }
2539
2540 return OK;
2541}
2542
2543/*
2544 * Handle first level expression:
2545 * expr2 || expr2 || expr2 logical OR
2546 *
2547 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002548 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 *
2550 * Return OK or FAIL.
2551 */
2552 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002553eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002555 char_u *p;
2556 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557
2558 /*
2559 * Get the first variable.
2560 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002561 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 return FAIL;
2563
2564 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002565 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002567 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002568 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002570 evalarg_T *evalarg_used = evalarg;
2571 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002572 int evaluate;
2573 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002574 long result = FALSE;
2575 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002576 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002577 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002578
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002579 if (evalarg == NULL)
2580 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002581 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002582 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002583 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002584 orig_flags = evalarg_used->eval_flags;
2585 evaluate = orig_flags & EVAL_EVALUATE;
2586 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002587 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002588 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002589 result = tv_get_bool_chk(rettv, &error);
2590 else if (tv_get_number_chk(rettv, &error) != 0)
2591 result = TRUE;
2592 clear_tv(rettv);
2593 if (error)
2594 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 }
2596
2597 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002598 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002600 while (p[0] == '|' && p[1] == '|')
2601 {
2602 if (getnext)
2603 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002604 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002605 {
2606 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2607 {
2608 error_white_both(p, 2);
2609 clear_tv(rettv);
2610 return FAIL;
2611 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002612 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002613 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002614
2615 /*
2616 * Get the second variable.
2617 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002618 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2619 {
mityu4ac198c2021-05-28 17:52:40 +02002620 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002621 clear_tv(rettv);
2622 return FAIL;
2623 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002624 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2625 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002626 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002627 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002628 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002629
2630 /*
2631 * Compute the result.
2632 */
2633 if (evaluate && !result)
2634 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002635 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002636 result = tv_get_bool_chk(&var2, &error);
2637 else if (tv_get_number_chk(&var2, &error) != 0)
2638 result = TRUE;
2639 clear_tv(&var2);
2640 if (error)
2641 return FAIL;
2642 }
2643 if (evaluate)
2644 {
2645 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002646 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002647 rettv->v_type = VAR_BOOL;
2648 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002649 }
2650 else
2651 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002652 rettv->v_type = VAR_NUMBER;
2653 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002654 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002655 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002656
2657 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002659
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002660 if (evalarg == NULL)
2661 clear_evalarg(&local_evalarg, NULL);
2662 else
2663 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 }
2665
2666 return OK;
2667}
2668
2669/*
2670 * Handle second level expression:
2671 * expr3 && expr3 && expr3 logical AND
2672 *
2673 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002674 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 *
2676 * Return OK or FAIL.
2677 */
2678 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002679eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002681 char_u *p;
2682 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683
2684 /*
2685 * Get the first variable.
2686 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002687 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 return FAIL;
2689
2690 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002691 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002693 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002694 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002696 evalarg_T *evalarg_used = evalarg;
2697 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002698 int orig_flags;
2699 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002700 long result = TRUE;
2701 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002702 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002703 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002704
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002705 if (evalarg == NULL)
2706 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002707 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002708 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002709 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002710 orig_flags = evalarg_used->eval_flags;
2711 evaluate = orig_flags & EVAL_EVALUATE;
2712 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002713 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002714 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002715 result = tv_get_bool_chk(rettv, &error);
2716 else if (tv_get_number_chk(rettv, &error) == 0)
2717 result = FALSE;
2718 clear_tv(rettv);
2719 if (error)
2720 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 }
2722
2723 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002724 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002726 while (p[0] == '&' && p[1] == '&')
2727 {
2728 if (getnext)
2729 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002730 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002731 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002732 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002733 {
2734 error_white_both(p, 2);
2735 clear_tv(rettv);
2736 return FAIL;
2737 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002738 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002739 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002740
2741 /*
2742 * Get the second variable.
2743 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002744 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2745 {
mityu4ac198c2021-05-28 17:52:40 +02002746 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002747 clear_tv(rettv);
2748 return FAIL;
2749 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002750 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2751 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002752 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002753 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002754 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002755 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002756
2757 /*
2758 * Compute the result.
2759 */
2760 if (evaluate && result)
2761 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002762 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002763 result = tv_get_bool_chk(&var2, &error);
2764 else if (tv_get_number_chk(&var2, &error) == 0)
2765 result = FALSE;
2766 clear_tv(&var2);
2767 if (error)
2768 return FAIL;
2769 }
2770 if (evaluate)
2771 {
2772 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002773 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002774 rettv->v_type = VAR_BOOL;
2775 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002776 }
2777 else
2778 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002779 rettv->v_type = VAR_NUMBER;
2780 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002781 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002782 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002783
2784 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002786
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002787 if (evalarg == NULL)
2788 clear_evalarg(&local_evalarg, NULL);
2789 else
2790 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 }
2792
2793 return OK;
2794}
2795
2796/*
2797 * Handle third level expression:
2798 * var1 == var2
2799 * var1 =~ var2
2800 * var1 != var2
2801 * var1 !~ var2
2802 * var1 > var2
2803 * var1 >= var2
2804 * var1 < var2
2805 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002806 * var1 is var2
2807 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 *
2809 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002810 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 *
2812 * Return OK or FAIL.
2813 */
2814 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002815eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002818 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002819 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002821 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822
2823 /*
2824 * Get the first variable.
2825 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002826 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 return FAIL;
2828
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002829 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002830 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831
2832 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002833 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002835 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002837 typval_T var2;
2838 int ic;
2839 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002840 int evaluate = evalarg == NULL
2841 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002842
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002843 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002844 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002845 *arg = eval_next_line(evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002846 p = *arg;
2847 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002848 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2849 {
mityu4ac198c2021-05-28 17:52:40 +02002850 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002851 clear_tv(rettv);
2852 return FAIL;
2853 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002854
Bram Moolenaar696ba232020-07-29 21:20:41 +02002855 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2856 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002857 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002858 clear_tv(rettv);
2859 return FAIL;
2860 }
2861
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002862 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 if (p[len] == '?')
2864 {
2865 ic = TRUE;
2866 ++len;
2867 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002868 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 else if (p[len] == '#')
2870 {
2871 ic = FALSE;
2872 ++len;
2873 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002874 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002876 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877
2878 /*
2879 * Get the second variable.
2880 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002881 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2882 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002883 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002884 clear_tv(rettv);
2885 return FAIL;
2886 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002887 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002888 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002890 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 return FAIL;
2892 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002893 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002894 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002895 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002896
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002897 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002898 {
2899 ret = FAIL;
2900 clear_tv(rettv);
2901 }
2902 else
2903 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002904 clear_tv(&var2);
2905 return ret;
2906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 }
2908
2909 return OK;
2910}
2911
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002912/*
2913 * Make a copy of blob "tv1" and append blob "tv2".
2914 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002915 void
2916eval_addblob(typval_T *tv1, typval_T *tv2)
2917{
2918 blob_T *b1 = tv1->vval.v_blob;
2919 blob_T *b2 = tv2->vval.v_blob;
2920 blob_T *b = blob_alloc();
2921 int i;
2922
2923 if (b != NULL)
2924 {
2925 for (i = 0; i < blob_len(b1); i++)
2926 ga_append(&b->bv_ga, blob_get(b1, i));
2927 for (i = 0; i < blob_len(b2); i++)
2928 ga_append(&b->bv_ga, blob_get(b2, i));
2929
2930 clear_tv(tv1);
2931 rettv_blob_set(tv1, b);
2932 }
2933}
2934
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002935/*
2936 * Make a copy of list "tv1" and append list "tv2".
2937 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002938 int
2939eval_addlist(typval_T *tv1, typval_T *tv2)
2940{
2941 typval_T var3;
2942
2943 // concatenate Lists
2944 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2945 {
2946 clear_tv(tv1);
2947 clear_tv(tv2);
2948 return FAIL;
2949 }
2950 clear_tv(tv1);
2951 *tv1 = var3;
2952 return OK;
2953}
2954
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955/*
2956 * Handle fourth level expression:
2957 * + number addition
2958 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002959 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002960 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 *
2962 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002963 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 *
2965 * Return OK or FAIL.
2966 */
2967 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002968eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 /*
2971 * Get the first variable.
2972 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002973 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 return FAIL;
2975
2976 /*
2977 * Repeat computing, until no '+', '-' or '.' is following.
2978 */
2979 for (;;)
2980 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002981 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002982 int getnext;
2983 char_u *p;
2984 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002985 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002986 int concat;
2987 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002988 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002989
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002990 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002991 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002992 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002993 p = eval_next_non_blank(*arg, evalarg, &getnext);
2994 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02002995 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002996 if ((op != '+' && op != '-' && !concat) || p[1] == '='
2997 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002999 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3000 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003001
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003002 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003003 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003004 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003005 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003006 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003007 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003008 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003009 {
mityu4ac198c2021-05-28 17:52:40 +02003010 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003011 clear_tv(rettv);
3012 return FAIL;
3013 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003014 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003015 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003016 if ((op != '+' || (rettv->v_type != VAR_LIST
3017 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003018#ifdef FEAT_FLOAT
3019 && (op == '.' || rettv->v_type != VAR_FLOAT)
3020#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003021 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003022 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003023 int error = FALSE;
3024
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003025 // For "list + ...", an illegal use of the first operand as
3026 // a number cannot be determined before evaluating the 2nd
3027 // operand: if this is also a list, all is ok.
3028 // For "something . ...", "something - ..." or "non-list + ...",
3029 // we know that the first operand needs to be a string or number
3030 // without evaluating the 2nd operand. So check before to avoid
3031 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003032 if (op != '.')
3033 tv_get_number_chk(rettv, &error);
3034 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003035 {
3036 clear_tv(rettv);
3037 return FAIL;
3038 }
3039 }
3040
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 /*
3042 * Get the second variable.
3043 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003044 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003045 {
mityu89dcb4d2021-05-28 13:50:17 +02003046 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003047 clear_tv(rettv);
3048 return FAIL;
3049 }
3050 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02003051 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003053 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 return FAIL;
3055 }
3056
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003057 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 {
3059 /*
3060 * Compute the result.
3061 */
3062 if (op == '.')
3063 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003064 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3065 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003066 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003067
Bram Moolenaar2e086612020-08-25 22:37:48 +02003068 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003069 || var2.v_type == VAR_CHANNEL
3070 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003071 semsg(_(e_using_invalid_value_as_string_str),
3072 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003073#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02003074 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003075 {
3076 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3077 var2.vval.v_float);
3078 s2 = buf2;
3079 }
3080#endif
3081 else
3082 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003083 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003084 {
3085 clear_tv(rettv);
3086 clear_tv(&var2);
3087 return FAIL;
3088 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003089 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003090 clear_tv(rettv);
3091 rettv->v_type = VAR_STRING;
3092 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003094 else if (op == '+' && rettv->v_type == VAR_BLOB
3095 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003096 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003097 else if (op == '+' && rettv->v_type == VAR_LIST
3098 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003099 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003100 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003101 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 else
3104 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003105 int error = FALSE;
3106 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003107#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003108 float_T f1 = 0, f2 = 0;
3109
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003110 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003111 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003112 f1 = rettv->vval.v_float;
3113 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003114 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003115 else
3116#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003117 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003118 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003119 if (error)
3120 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003121 // This can only happen for "list + non-list" or
3122 // "blob + non-blob". For "non-list + ..." or
3123 // "something - ...", we returned before evaluating the
3124 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003125 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003126 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003127 return FAIL;
3128 }
3129#ifdef FEAT_FLOAT
3130 if (var2.v_type == VAR_FLOAT)
3131 f1 = n1;
3132#endif
3133 }
3134#ifdef FEAT_FLOAT
3135 if (var2.v_type == VAR_FLOAT)
3136 {
3137 f2 = var2.vval.v_float;
3138 n2 = 0;
3139 }
3140 else
3141#endif
3142 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003143 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003144 if (error)
3145 {
3146 clear_tv(rettv);
3147 clear_tv(&var2);
3148 return FAIL;
3149 }
3150#ifdef FEAT_FLOAT
3151 if (rettv->v_type == VAR_FLOAT)
3152 f2 = n2;
3153#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003154 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003155 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003156
3157#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003158 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003159 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3160 {
3161 if (op == '+')
3162 f1 = f1 + f2;
3163 else
3164 f1 = f1 - f2;
3165 rettv->v_type = VAR_FLOAT;
3166 rettv->vval.v_float = f1;
3167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003169#endif
3170 {
3171 if (op == '+')
3172 n1 = n1 + n2;
3173 else
3174 n1 = n1 - n2;
3175 rettv->v_type = VAR_NUMBER;
3176 rettv->vval.v_number = n1;
3177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003179 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 }
3181 }
3182 return OK;
3183}
3184
3185/*
3186 * Handle fifth level expression:
3187 * * number multiplication
3188 * / number division
3189 * % number modulo
3190 *
3191 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003192 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 *
3194 * Return OK or FAIL.
3195 */
3196 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003197eval6(
3198 char_u **arg,
3199 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003200 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003201 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003203#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003204 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206
3207 /*
3208 * Get the first variable.
3209 */
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003210 if (eval7t(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 return FAIL;
3212
3213 /*
3214 * Repeat computing, until no '*', '/' or '%' is following.
3215 */
3216 for (;;)
3217 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003218 int evaluate;
3219 int getnext;
3220 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003221 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003222 int op;
3223 varnumber_T n1, n2;
3224#ifdef FEAT_FLOAT
3225 float_T f1, f2;
3226#endif
3227 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003228
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003229 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003230 p = eval_next_non_blank(*arg, evalarg, &getnext);
3231 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003232 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003234
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003235 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003236 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003237 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003238 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003239 {
3240 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3241 {
mityu4ac198c2021-05-28 17:52:40 +02003242 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003243 clear_tv(rettv);
3244 return FAIL;
3245 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003246 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003249#ifdef FEAT_FLOAT
3250 f1 = 0;
3251 f2 = 0;
3252#endif
3253 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003254 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003256#ifdef FEAT_FLOAT
3257 if (rettv->v_type == VAR_FLOAT)
3258 {
3259 f1 = rettv->vval.v_float;
3260 use_float = TRUE;
3261 n1 = 0;
3262 }
3263 else
3264#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003265 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003266 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003267 if (error)
3268 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 }
3270 else
3271 n1 = 0;
3272
3273 /*
3274 * Get the second variable.
3275 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003276 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3277 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003278 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003279 clear_tv(rettv);
3280 return FAIL;
3281 }
3282 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003283 if (eval7t(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 return FAIL;
3285
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003286 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003288#ifdef FEAT_FLOAT
3289 if (var2.v_type == VAR_FLOAT)
3290 {
3291 if (!use_float)
3292 {
3293 f1 = n1;
3294 use_float = TRUE;
3295 }
3296 f2 = var2.vval.v_float;
3297 n2 = 0;
3298 }
3299 else
3300#endif
3301 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003302 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003303 clear_tv(&var2);
3304 if (error)
3305 return FAIL;
3306#ifdef FEAT_FLOAT
3307 if (use_float)
3308 f2 = n2;
3309#endif
3310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311
3312 /*
3313 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003314 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003316#ifdef FEAT_FLOAT
3317 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003319 if (op == '*')
3320 f1 = f1 * f2;
3321 else if (op == '/')
3322 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003323# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003324 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003325 if (f2 == 0.0)
3326 {
3327 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003328 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003329 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003330 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003331 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003332 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003333 }
3334 else
3335 f1 = f1 / f2;
3336# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003337 // We rely on the floating point library to handle divide
3338 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003339 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003340# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003343 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003344 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003345 return FAIL;
3346 }
3347 rettv->v_type = VAR_FLOAT;
3348 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 }
3350 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003351#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003353 int failed = FALSE;
3354
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003355 if (op == '*')
3356 n1 = n1 * n2;
3357 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003358 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003360 n1 = num_modulus(n1, n2, &failed);
3361 if (failed)
3362 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003363
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003364 rettv->v_type = VAR_NUMBER;
3365 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 }
3368 }
3369
3370 return OK;
3371}
3372
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003373/*
3374 * Handle a type cast before a base level expression.
3375 * "arg" must point to the first non-white of the expression.
3376 * "arg" is advanced to just after the recognized expression.
3377 * Return OK or FAIL.
3378 */
3379 static int
3380eval7t(
3381 char_u **arg,
3382 typval_T *rettv,
3383 evalarg_T *evalarg,
3384 int want_string) // after "." operator
3385{
3386 type_T *want_type = NULL;
3387 garray_T type_list; // list of pointers to allocated types
3388 int res;
3389 int evaluate = evalarg == NULL ? 0
3390 : (evalarg->eval_flags & EVAL_EVALUATE);
3391
3392 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003393 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3394 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003395 {
3396 ++*arg;
3397 ga_init2(&type_list, sizeof(type_T *), 10);
3398 want_type = parse_type(arg, &type_list, TRUE);
3399 if (want_type == NULL && (evaluate || **arg != '>'))
3400 {
3401 clear_type_list(&type_list);
3402 return FAIL;
3403 }
3404
3405 if (**arg != '>')
3406 {
3407 if (*skipwhite(*arg) == '>')
3408 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3409 else
3410 emsg(_(e_missing_gt));
3411 clear_type_list(&type_list);
3412 return FAIL;
3413 }
3414 ++*arg;
3415 *arg = skipwhite_and_linebreak(*arg, evalarg);
3416 }
3417
3418 res = eval7(arg, rettv, evalarg, want_string);
3419
3420 if (want_type != NULL && evaluate)
3421 {
3422 if (res == OK)
3423 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003424 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3425 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003426
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003427 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003428 {
3429 if (want_type == &t_bool && actual != &t_bool
3430 && (actual->tt_flags & TTFLAG_BOOL_OK))
3431 {
3432 int n = tv2bool(rettv);
3433
3434 // can use "0" and "1" for boolean in some places
3435 clear_tv(rettv);
3436 rettv->v_type = VAR_BOOL;
3437 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3438 }
3439 else
3440 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003441 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003442
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003443 where.wt_variable = TRUE;
3444 res = check_type(want_type, actual, TRUE, where);
3445 }
3446 }
3447 }
3448 clear_type_list(&type_list);
3449 }
3450
3451 return res;
3452}
3453
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003454 int
3455eval_leader(char_u **arg, int vim9)
3456{
3457 char_u *s = *arg;
3458 char_u *p = *arg;
3459
3460 while (*p == '!' || *p == '-' || *p == '+')
3461 {
3462 char_u *n = skipwhite(p + 1);
3463
3464 // ++, --, -+ and +- are not accepted in Vim9 script
3465 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3466 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003467 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003468 return FAIL;
3469 }
3470 p = n;
3471 }
3472 *arg = p;
3473 return OK;
3474}
3475
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476/*
3477 * Handle sixth level expression:
3478 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003479 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003480 * "string" string constant
3481 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 * &option-name option value
3483 * @r register contents
3484 * identifier variable value
3485 * function() function call
3486 * $VAR environment variable
3487 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003488 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003489 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003490 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003491 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 *
3493 * Also handle:
3494 * ! in front logical NOT
3495 * - in front unary minus
3496 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003497 * trailing [] subscript in String or List
3498 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003499 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 *
3501 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003502 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 *
3504 * Return OK or FAIL.
3505 */
3506 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003507eval7(
3508 char_u **arg,
3509 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003510 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003511 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003513 int evaluate = evalarg != NULL
3514 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 int len;
3516 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003517 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 char_u *start_leader, *end_leader;
3519 int ret = OK;
3520 char_u *alias;
3521
3522 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003523 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003524 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003526 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527
3528 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003529 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 */
3531 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003532 if (eval_leader(arg, in_vim9script()) == FAIL)
3533 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 end_leader = *arg;
3535
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003536 if (**arg == '.' && (!isdigit(*(*arg + 1))
3537#ifdef FEAT_FLOAT
Bram Moolenaardd9de502021-08-15 13:49:42 +02003538 || in_old_script(2)
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003539#endif
3540 ))
3541 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003542 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003543 ++*arg;
3544 return FAIL;
3545 }
3546
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 switch (**arg)
3548 {
3549 /*
3550 * Number constant.
3551 */
3552 case '0':
3553 case '1':
3554 case '2':
3555 case '3':
3556 case '4':
3557 case '5':
3558 case '6':
3559 case '7':
3560 case '8':
3561 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003562 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003563
3564 // Apply prefixed "-" and "+" now. Matters especially when
3565 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003566 if (ret == OK && evaluate && end_leader > start_leader
3567 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003568 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 break;
3570
3571 /*
3572 * String constant: "string".
3573 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003574 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 break;
3576
3577 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003578 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003580 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003581 break;
3582
3583 /*
3584 * List: [expr, expr]
3585 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003586 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 break;
3588
3589 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003590 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003591 */
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003592 case '#': if (in_vim9script())
3593 {
3594 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3595 }
3596 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003597 {
3598 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003599 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003600 }
3601 else
3602 ret = NOTDONE;
3603 break;
3604
3605 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003606 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003607 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003608 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003609 case '{': if (in_vim9script())
3610 ret = NOTDONE;
3611 else
3612 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003613 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003614 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003615 break;
3616
3617 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003618 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003620 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 break;
3622
3623 /*
3624 * Environment variable: $VAR.
3625 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003626 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 break;
3628
3629 /*
3630 * Register contents: @r.
3631 */
3632 case '@': ++*arg;
3633 if (evaluate)
3634 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003635 if (in_vim9script() && IS_WHITE_OR_NUL(**arg))
3636 semsg(_(e_syntax_error_at_str), *arg);
3637 else if (in_vim9script() && !valid_yank_reg(**arg, FALSE))
3638 emsg_invreg(**arg);
3639 else
3640 {
3641 rettv->v_type = VAR_STRING;
3642 rettv->vval.v_string = get_reg_contents(**arg,
3643 GREG_EXPR_SRC);
3644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 }
3646 if (**arg != NUL)
3647 ++*arg;
3648 break;
3649
3650 /*
3651 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003652 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003654 case '(': ret = NOTDONE;
3655 if (in_vim9script())
Bram Moolenaar06409502021-02-17 17:00:27 +01003656 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003657 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003658 if (ret == OK && evaluate)
3659 {
3660 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3661
Bram Moolenaara9931532021-06-12 15:58:16 +02003662 // Compile it here to get the return type. The return
3663 // type is optional, when it's missing use t_unknown.
3664 // This is recognized in compile_return().
3665 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3666 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003667 if (compile_def_function(ufunc,
Bram Moolenaare99d4222021-06-13 14:01:26 +02003668 FALSE, COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003669 {
3670 clear_tv(rettv);
3671 ret = FAIL;
3672 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003673 }
3674 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003675 if (ret == NOTDONE)
3676 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003677 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003678 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003679
Bram Moolenaar9215f012020-06-27 21:18:00 +02003680 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003681 if (**arg == ')')
3682 ++*arg;
3683 else if (ret == OK)
3684 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003685 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003686 clear_tv(rettv);
3687 ret = FAIL;
3688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 }
3690 break;
3691
Bram Moolenaar8c711452005-01-14 21:53:12 +00003692 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 break;
3694 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003695
3696 if (ret == NOTDONE)
3697 {
3698 /*
3699 * Must be a variable or function name.
3700 * Can also be a curly-braces kind of name: {expr}.
3701 */
3702 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003703 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003704 if (alias != NULL)
3705 s = alias;
3706
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003707 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003708 ret = FAIL;
3709 else
3710 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003711 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3712
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003713 if (evaluate && in_vim9script() && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003714 {
3715 emsg(_(e_cannot_use_underscore_here));
3716 ret = FAIL;
3717 }
3718 else if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003719 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003720 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003721 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003722 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003723 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003724 else if (flags & EVAL_CONSTANT)
3725 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003726 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003727 {
3728 // get the value of "true", "false" or a variable
3729 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3730 {
3731 rettv->v_type = VAR_BOOL;
3732 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003733 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003734 }
3735 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003736 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003737 {
3738 rettv->v_type = VAR_BOOL;
3739 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003740 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003741 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003742 else if (len == 4 && in_vim9script()
3743 && STRNCMP(s, "null", 4) == 0)
3744 {
3745 rettv->v_type = VAR_SPECIAL;
3746 rettv->vval.v_number = VVAL_NULL;
3747 ret = OK;
3748 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003749 else
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003750 {
3751 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003752 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003753 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003754 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003755 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003756 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003757 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003758 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003759 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003760 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003761 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003762 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003763 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003764 }
3765
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003766 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3767 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003768 if (ret == OK)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003769 ret = handle_subscript(arg, name_start, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770
3771 /*
3772 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3773 */
3774 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003775 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003776 return ret;
3777}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003778
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003779/*
3780 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003781 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003782 * Adjusts "end_leaderp" until it is at "start_leader".
3783 */
3784 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003785eval7_leader(
3786 typval_T *rettv,
3787 int numeric_only,
3788 char_u *start_leader,
3789 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003790{
3791 char_u *end_leader = *end_leaderp;
3792 int ret = OK;
3793 int error = FALSE;
3794 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003795 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003796#ifdef FEAT_FLOAT
3797 float_T f = 0.0;
3798
3799 if (rettv->v_type == VAR_FLOAT)
3800 f = rettv->vval.v_float;
3801 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003802#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003803 {
3804 while (VIM_ISWHITE(end_leader[-1]))
3805 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003806 if (in_vim9script() && end_leader[-1] == '!')
3807 val = tv2bool(rettv);
3808 else
3809 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003810 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003811 if (error)
3812 {
3813 clear_tv(rettv);
3814 ret = FAIL;
3815 }
3816 else
3817 {
3818 while (end_leader > start_leader)
3819 {
3820 --end_leader;
3821 if (*end_leader == '!')
3822 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003823 if (numeric_only)
3824 {
3825 ++end_leader;
3826 break;
3827 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003828#ifdef FEAT_FLOAT
3829 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003830 {
3831 if (in_vim9script())
3832 {
3833 rettv->v_type = VAR_BOOL;
3834 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3835 }
3836 else
3837 f = !f;
3838 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003839 else
3840#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003841 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003842 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003843 type = VAR_BOOL;
3844 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003845 }
3846 else if (*end_leader == '-')
3847 {
3848#ifdef FEAT_FLOAT
3849 if (rettv->v_type == VAR_FLOAT)
3850 f = -f;
3851 else
3852#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003853 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003854 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003855 type = VAR_NUMBER;
3856 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003857 }
3858 }
3859#ifdef FEAT_FLOAT
3860 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003862 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003863 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003865 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003866#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003867 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003868 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003869 if (in_vim9script())
3870 rettv->v_type = type;
3871 else
3872 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003873 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003876 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 return ret;
3878}
3879
3880/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003881 * Call the function referred to in "rettv".
3882 */
3883 static int
3884call_func_rettv(
3885 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003886 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003887 typval_T *rettv,
3888 int evaluate,
3889 dict_T *selfdict,
3890 typval_T *basetv)
3891{
3892 partial_T *pt = NULL;
3893 funcexe_T funcexe;
3894 typval_T functv;
3895 char_u *s;
3896 int ret;
3897
3898 // need to copy the funcref so that we can clear rettv
3899 if (evaluate)
3900 {
3901 functv = *rettv;
3902 rettv->v_type = VAR_UNKNOWN;
3903
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003904 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003905 if (functv.v_type == VAR_PARTIAL)
3906 {
3907 pt = functv.vval.v_partial;
3908 s = partial_name(pt);
3909 }
3910 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003911 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003912 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003913 if (s == NULL || *s == NUL)
3914 {
3915 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02003916 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003917 goto theend;
3918 }
3919 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003920 }
3921 else
3922 s = (char_u *)"";
3923
Bram Moolenaara80faa82020-04-12 19:37:17 +02003924 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003925 funcexe.fe_firstline = curwin->w_cursor.lnum;
3926 funcexe.fe_lastline = curwin->w_cursor.lnum;
3927 funcexe.fe_evaluate = evaluate;
3928 funcexe.fe_partial = pt;
3929 funcexe.fe_selfdict = selfdict;
3930 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003931 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003932
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003933theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003934 // Clear the funcref afterwards, so that deleting it while
3935 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003936 if (evaluate)
3937 clear_tv(&functv);
3938
3939 return ret;
3940}
3941
3942/*
3943 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003944 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003945 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3946 */
3947 static int
3948eval_lambda(
3949 char_u **arg,
3950 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003951 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003952 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003953{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003954 int evaluate = evalarg != NULL
3955 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003956 typval_T base = *rettv;
3957 int ret;
3958
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003959 rettv->v_type = VAR_UNKNOWN;
3960
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003961 if (**arg == '{')
3962 {
3963 // ->{lambda}()
3964 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3965 }
3966 else
3967 {
3968 // ->(lambda)()
3969 ++*arg;
3970 ret = eval1(arg, rettv, evalarg);
3971 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar605ec912021-12-18 16:54:31 +00003972 if (**arg == ')')
3973 {
3974 ++*arg;
3975 }
3976 else
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003977 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003978 emsg(_(e_missing_closing_paren));
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003979 ret = FAIL;
3980 }
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003981 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003982 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003983 return FAIL;
3984 else if (**arg != '(')
3985 {
3986 if (verbose)
3987 {
3988 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00003989 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003990 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003991 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003992 }
3993 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003994 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003995 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003996 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003997 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003998
3999 // Clear the funcref afterwards, so that deleting it while
4000 // evaluating the arguments is possible (see test55).
4001 if (evaluate)
4002 clear_tv(&base);
4003
4004 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004005}
4006
4007/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004008 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004009 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004010 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4011 */
4012 static int
4013eval_method(
4014 char_u **arg,
4015 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004016 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004017 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004018{
4019 char_u *name;
4020 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004021 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004022 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004023 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004024 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004025 int evaluate = evalarg != NULL
4026 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004027
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004028 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004029
Bram Moolenaarac92e252019-08-03 21:58:38 +02004030 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004031 len = get_name_len(arg, &alias, evaluate, TRUE);
4032 if (alias != NULL)
4033 name = alias;
4034
4035 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004036 {
4037 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004038 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004039 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004040 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004041 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004042 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004043 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004044
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004045 // If there is no "(" immediately following, but there is further on,
4046 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4047 // Does not handle anything where "(" is part of the expression.
4048 *arg = skipwhite(*arg);
4049
4050 if (**arg != '(' && alias == NULL
4051 && (paren = vim_strchr(*arg, '(')) != NULL)
4052 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004053 *arg = name;
4054 *paren = NUL;
Bram Moolenaar15d16352022-01-17 20:09:08 +00004055 name = deref_function_name(arg, &tofree, evalarg, verbose);
4056 if (name == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004057 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004058 *arg = name + len;
4059 ret = FAIL;
4060 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004061 else
Bram Moolenaar15d16352022-01-17 20:09:08 +00004062 len = STRLEN(name);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004063 *paren = '(';
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004064 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004065
4066 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004067 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004068 *arg = skipwhite(*arg);
4069
4070 if (**arg != '(')
4071 {
4072 if (verbose)
4073 semsg(_(e_missing_parenthesis_str), name);
4074 ret = FAIL;
4075 }
4076 else if (VIM_ISWHITE((*arg)[-1]))
4077 {
4078 if (verbose)
4079 emsg(_(e_no_white_space_allowed_before_parenthesis));
4080 ret = FAIL;
4081 }
4082 else
4083 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004084 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004085 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004086 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004087
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004088 // Clear the funcref afterwards, so that deleting it while
4089 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004090 if (evaluate)
4091 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004092 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004093
Bram Moolenaarac92e252019-08-03 21:58:38 +02004094 return ret;
4095}
4096
4097/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004098 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4099 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004100 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4101 */
4102 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004103eval_index(
4104 char_u **arg,
4105 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004106 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004107 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004108{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004109 int evaluate = evalarg != NULL
4110 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004111 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004112 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004113 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004114 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004115 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004116 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004117
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004118 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4119 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004120
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004121 init_tv(&var1);
4122 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004123 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004124 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004125 /*
4126 * dict.name
4127 */
4128 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004129 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004130 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004131 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004132 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004133 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004134 }
4135 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004136 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004137 /*
4138 * something[idx]
4139 *
4140 * Get the (first) variable from inside the [].
4141 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004142 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004143 if (**arg == ':')
4144 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004145 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004146 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004147 else if (vim9 && **arg == ':')
4148 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004149 semsg(_(e_white_space_required_before_and_after_str_at_str),
4150 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004151 clear_tv(&var1);
4152 return FAIL;
4153 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004154 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004155 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004156 int error = FALSE;
4157
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004158#ifdef FEAT_FLOAT
4159 // allow for indexing with float
4160 if (vim9 && rettv->v_type == VAR_DICT
4161 && var1.v_type == VAR_FLOAT)
4162 {
4163 var1.vval.v_string = typval_tostring(&var1, TRUE);
4164 var1.v_type = VAR_STRING;
4165 }
4166#endif
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004167 if (vim9 && rettv->v_type == VAR_LIST)
4168 tv_get_number_chk(&var1, &error);
4169 else
4170 error = tv_get_string_chk(&var1) == NULL;
4171 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004172 {
4173 // not a number or string
4174 clear_tv(&var1);
4175 return FAIL;
4176 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004177 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004178
4179 /*
4180 * Get the second variable from inside the [:].
4181 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004182 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004183 if (**arg == ':')
4184 {
4185 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004186 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01004187 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004188 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004189 semsg(_(e_white_space_required_before_and_after_str_at_str),
4190 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004191 if (!empty1)
4192 clear_tv(&var1);
4193 return FAIL;
4194 }
4195 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004196 if (**arg == ']')
4197 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004198 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004199 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004200 if (!empty1)
4201 clear_tv(&var1);
4202 return FAIL;
4203 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004204 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004205 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004206 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004207 if (!empty1)
4208 clear_tv(&var1);
4209 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004210 return FAIL;
4211 }
4212 }
4213
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004214 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004215 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004216 if (**arg != ']')
4217 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004218 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004219 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004220 clear_tv(&var1);
4221 if (range)
4222 clear_tv(&var2);
4223 return FAIL;
4224 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004225 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004226 }
4227
4228 if (evaluate)
4229 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004230 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004231 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004232 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004233
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004234 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004235 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004236 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004237 clear_tv(&var2);
4238 return res;
4239 }
4240 return OK;
4241}
4242
4243/*
4244 * Check if "rettv" can have an [index] or [sli:ce]
4245 */
4246 int
4247check_can_index(typval_T *rettv, int evaluate, int verbose)
4248{
4249 switch (rettv->v_type)
4250 {
4251 case VAR_FUNC:
4252 case VAR_PARTIAL:
4253 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004254 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004255 return FAIL;
4256 case VAR_FLOAT:
4257#ifdef FEAT_FLOAT
4258 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004259 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004260 return FAIL;
4261#endif
4262 case VAR_BOOL:
4263 case VAR_SPECIAL:
4264 case VAR_JOB:
4265 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004266 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004267 if (verbose)
4268 emsg(_(e_cannot_index_special_variable));
4269 return FAIL;
4270 case VAR_UNKNOWN:
4271 case VAR_ANY:
4272 case VAR_VOID:
4273 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004274 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004275 emsg(_(e_cannot_index_special_variable));
4276 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004277 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004278 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004279
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004280 case VAR_STRING:
4281 case VAR_LIST:
4282 case VAR_DICT:
4283 case VAR_BLOB:
4284 break;
4285 case VAR_NUMBER:
4286 if (in_vim9script())
4287 emsg(_(e_cannot_index_number));
4288 break;
4289 }
4290 return OK;
4291}
4292
4293/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004294 * slice() function
4295 */
4296 void
4297f_slice(typval_T *argvars, typval_T *rettv)
4298{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004299 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004300 && ((argvars[0].v_type != VAR_STRING
4301 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004302 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004303 && check_for_list_arg(argvars, 0) == FAIL)
4304 || check_for_number_arg(argvars, 1) == FAIL
4305 || check_for_opt_number_arg(argvars, 2) == FAIL))
4306 return;
4307
Bram Moolenaar6601b622021-01-13 21:47:15 +01004308 if (check_can_index(argvars, TRUE, FALSE) == OK)
4309 {
4310 copy_tv(argvars, rettv);
4311 eval_index_inner(rettv, TRUE, argvars + 1,
4312 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4313 TRUE, NULL, 0, FALSE);
4314 }
4315}
4316
4317/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004318 * Apply index or range to "rettv".
4319 * "var1" is the first index, NULL for [:expr].
4320 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004321 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4322 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004323 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4324 */
4325 int
4326eval_index_inner(
4327 typval_T *rettv,
4328 int is_range,
4329 typval_T *var1,
4330 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004331 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004332 char_u *key,
4333 int keylen,
4334 int verbose)
4335{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004336 varnumber_T n1, n2 = 0;
4337 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004338
4339 n1 = 0;
4340 if (var1 != NULL && rettv->v_type != VAR_DICT)
4341 n1 = tv_get_number(var1);
4342
4343 if (is_range)
4344 {
4345 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004346 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004347 if (verbose)
4348 emsg(_(e_cannot_slice_dictionary));
4349 return FAIL;
4350 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004351 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004352 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004353 else
4354 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004355 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004356
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004357 switch (rettv->v_type)
4358 {
4359 case VAR_UNKNOWN:
4360 case VAR_ANY:
4361 case VAR_VOID:
4362 case VAR_FUNC:
4363 case VAR_PARTIAL:
4364 case VAR_FLOAT:
4365 case VAR_BOOL:
4366 case VAR_SPECIAL:
4367 case VAR_JOB:
4368 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004369 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004370 break; // not evaluating, skipping over subscript
4371
4372 case VAR_NUMBER:
4373 case VAR_STRING:
4374 {
4375 char_u *s = tv_get_string(rettv);
4376
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004377 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004378 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004379 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004380 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004381 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004382 else
4383 s = char_from_string(s, n1);
4384 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004385 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004386 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004387 // The resulting variable is a substring. If the indexes
4388 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004389 if (n1 < 0)
4390 {
4391 n1 = len + n1;
4392 if (n1 < 0)
4393 n1 = 0;
4394 }
4395 if (n2 < 0)
4396 n2 = len + n2;
4397 else if (n2 >= len)
4398 n2 = len;
4399 if (n1 >= len || n2 < 0 || n1 > n2)
4400 s = NULL;
4401 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004402 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004403 }
4404 else
4405 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004406 // The resulting variable is a string of a single
4407 // character. If the index is too big or negative the
4408 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004409 if (n1 >= len || n1 < 0)
4410 s = NULL;
4411 else
4412 s = vim_strnsave(s + n1, 1);
4413 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004414 clear_tv(rettv);
4415 rettv->v_type = VAR_STRING;
4416 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004417 }
4418 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004419
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004420 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004421 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4422 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004423 break;
4424
4425 case VAR_LIST:
4426 if (var1 == NULL)
4427 n1 = 0;
4428 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004429 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004430 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004431 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004432 return FAIL;
4433 break;
4434
4435 case VAR_DICT:
4436 {
4437 dictitem_T *item;
4438 typval_T tmp;
4439
4440 if (key == NULL)
4441 {
4442 key = tv_get_string_chk(var1);
4443 if (key == NULL)
4444 return FAIL;
4445 }
4446
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004447 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004448
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004449 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004450 {
4451 if (verbose)
4452 {
4453 if (keylen > 0)
4454 key[keylen] = NUL;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004455 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004456 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004457 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004458 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004459
4460 copy_tv(&item->di_tv, &tmp);
4461 clear_tv(rettv);
4462 *rettv = tmp;
4463 }
4464 break;
4465 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004466 return OK;
4467}
4468
4469/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004470 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004471 */
4472 char_u *
4473partial_name(partial_T *pt)
4474{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004475 if (pt != NULL)
4476 {
4477 if (pt->pt_name != NULL)
4478 return pt->pt_name;
4479 if (pt->pt_func != NULL)
4480 return pt->pt_func->uf_name;
4481 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004482 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004483}
4484
Bram Moolenaarddecc252016-04-06 22:59:37 +02004485 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004486partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004487{
4488 int i;
4489
4490 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004491 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004492 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004493 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004494 if (pt->pt_name != NULL)
4495 {
4496 func_unref(pt->pt_name);
4497 vim_free(pt->pt_name);
4498 }
4499 else
4500 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004501
Bram Moolenaar54656012021-06-09 20:50:46 +02004502 // "out_up" is no longer used, decrement refcount on partial that owns it.
4503 partial_unref(pt->pt_outer.out_up_partial);
4504
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004505 // Decrease the reference count for the context of a closure. If down
4506 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004507 if (pt->pt_funcstack != NULL)
4508 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004509 --pt->pt_funcstack->fs_refcount;
4510 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004511 }
4512
Bram Moolenaarddecc252016-04-06 22:59:37 +02004513 vim_free(pt);
4514}
4515
4516/*
4517 * Unreference a closure: decrement the reference count and free it when it
4518 * becomes zero.
4519 */
4520 void
4521partial_unref(partial_T *pt)
4522{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004523 if (pt != NULL)
4524 {
4525 if (--pt->pt_refcount <= 0)
4526 partial_free(pt);
4527
4528 // If the reference count goes down to one, the funcstack may be the
4529 // only reference and can be freed if no other partials reference it.
4530 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4531 funcstack_check_refcount(pt->pt_funcstack);
4532 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004533}
4534
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004535/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004536 * Return the next (unique) copy ID.
4537 * Used for serializing nested structures.
4538 */
4539 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004540get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004541{
4542 current_copyID += COPYID_INC;
4543 return current_copyID;
4544}
4545
4546/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004547 * Garbage collection for lists and dictionaries.
4548 *
4549 * We use reference counts to be able to free most items right away when they
4550 * are no longer used. But for composite items it's possible that it becomes
4551 * unused while the reference count is > 0: When there is a recursive
4552 * reference. Example:
4553 * :let l = [1, 2, 3]
4554 * :let d = {9: l}
4555 * :let l[1] = d
4556 *
4557 * Since this is quite unusual we handle this with garbage collection: every
4558 * once in a while find out which lists and dicts are not referenced from any
4559 * variable.
4560 *
4561 * Here is a good reference text about garbage collection (refers to Python
4562 * but it applies to all reference-counting mechanisms):
4563 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004564 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004565
4566/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004567 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004568 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004569 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004570 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004571 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004572garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004573{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004574 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004575 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004576 buf_T *buf;
4577 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004578 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004579 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004580
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004581 if (!testing)
4582 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004583 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004584 want_garbage_collect = FALSE;
4585 may_garbage_collect = FALSE;
4586 garbage_collect_at_exit = FALSE;
4587 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004588
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004589 // The execution stack can grow big, limit the size.
4590 if (exestack.ga_maxlen - exestack.ga_len > 500)
4591 {
4592 size_t new_len;
4593 char_u *pp;
4594 int n;
4595
4596 // Keep 150% of the current size, with a minimum of the growth size.
4597 n = exestack.ga_len / 2;
4598 if (n < exestack.ga_growsize)
4599 n = exestack.ga_growsize;
4600
4601 // Don't make it bigger though.
4602 if (exestack.ga_len + n < exestack.ga_maxlen)
4603 {
4604 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4605 pp = vim_realloc(exestack.ga_data, new_len);
4606 if (pp == NULL)
4607 return FAIL;
4608 exestack.ga_maxlen = exestack.ga_len + n;
4609 exestack.ga_data = pp;
4610 }
4611 }
4612
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004613 // We advance by two because we add one for items referenced through
4614 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004615 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004616
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004617 /*
4618 * 1. Go through all accessible variables and mark all lists and dicts
4619 * with copyID.
4620 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004621
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004622 // Don't free variables in the previous_funccal list unless they are only
4623 // referenced through previous_funccal. This must be first, because if
4624 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004625 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004626
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004627 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004628 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004629
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004630 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004631 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004632 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4633 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004634
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004635 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004636 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004637 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4638 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004639 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004640 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4641 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004642#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004643 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004644 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4645 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004646 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004647 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004648 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4649 NULL, NULL);
4650#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004651
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004652 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004653 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004654 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4655 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004656 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004657 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004658
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004659 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004660 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004661
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004662 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004663 abort = abort || set_ref_in_functions(copyID);
4664
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004665 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004666 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004667
Bram Moolenaar7509ad82021-12-14 18:14:37 +00004668 // funcstacks keep variables for closures
4669 abort = abort || set_ref_in_funcstacks(copyID);
4670
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004671 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004672 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004673
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004674 // callbacks in buffers
4675 abort = abort || set_ref_in_buffers(copyID);
4676
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00004677 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
4678 abort = abort || set_ref_in_insexpand_funcs(copyID);
4679
4680 // 'operatorfunc' callback
4681 abort = abort || set_ref_in_opfunc(copyID);
4682
4683 // 'tagfunc' callback
4684 abort = abort || set_ref_in_tagfunc(copyID);
4685
4686 // 'imactivatefunc' and 'imstatusfunc' callbacks
4687 abort = abort || set_ref_in_im_funcs(copyID);
4688
Bram Moolenaar1dced572012-04-05 16:54:08 +02004689#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004690 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004691#endif
4692
Bram Moolenaardb913952012-06-29 12:54:53 +02004693#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004694 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004695#endif
4696
4697#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004698 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004699#endif
4700
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004701#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004702 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004703 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004704#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004705#ifdef FEAT_NETBEANS_INTG
4706 abort = abort || set_ref_in_nb_channel(copyID);
4707#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004708
Bram Moolenaare3188e22016-05-31 21:13:04 +02004709#ifdef FEAT_TIMERS
4710 abort = abort || set_ref_in_timer(copyID);
4711#endif
4712
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004713#ifdef FEAT_QUICKFIX
4714 abort = abort || set_ref_in_quickfix(copyID);
4715#endif
4716
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004717#ifdef FEAT_TERMINAL
4718 abort = abort || set_ref_in_term(copyID);
4719#endif
4720
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004721#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004722 abort = abort || set_ref_in_popups(copyID);
4723#endif
4724
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004725 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004726 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004727 /*
4728 * 2. Free lists and dictionaries that are not referenced.
4729 */
4730 did_free = free_unref_items(copyID);
4731
4732 /*
4733 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004734 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004735 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004736 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004737 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004738 else if (p_verbose > 0)
4739 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004740 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004741 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004742
4743 return did_free;
4744}
4745
4746/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004747 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004748 */
4749 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004750free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004751{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004752 int did_free = FALSE;
4753
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004754 // Let all "free" functions know that we are here. This means no
4755 // dictionaries, lists, channels or jobs are to be freed, because we will
4756 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004757 in_free_unref_items = TRUE;
4758
4759 /*
4760 * PASS 1: free the contents of the items. We don't free the items
4761 * themselves yet, so that it is possible to decrement refcount counters
4762 */
4763
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004764 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004765 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004766
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004767 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004768 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004769
4770#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004771 // Go through the list of jobs and free items without the copyID. This
4772 // must happen before doing channels, because jobs refer to channels, but
4773 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004774 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4775
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004776 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004777 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4778#endif
4779
4780 /*
4781 * PASS 2: free the items themselves.
4782 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004783 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004784 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004785
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004786#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004787 // Go through the list of jobs and free items without the copyID. This
4788 // must happen before doing channels, because jobs refer to channels, but
4789 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004790 free_unused_jobs(copyID, COPYID_MASK);
4791
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004792 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004793 free_unused_channels(copyID, COPYID_MASK);
4794#endif
4795
4796 in_free_unref_items = FALSE;
4797
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004798 return did_free;
4799}
4800
4801/*
4802 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004803 * "list_stack" is used to add lists to be marked. Can be NULL.
4804 *
4805 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004806 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004807 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004808set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004809{
4810 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004811 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004812 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004813 hashtab_T *cur_ht;
4814 ht_stack_T *ht_stack = NULL;
4815 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004816
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004817 cur_ht = ht;
4818 for (;;)
4819 {
4820 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004821 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004822 // Mark each item in the hashtab. If the item contains a hashtab
4823 // it is added to ht_stack, if it contains a list it is added to
4824 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004825 todo = (int)cur_ht->ht_used;
4826 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4827 if (!HASHITEM_EMPTY(hi))
4828 {
4829 --todo;
4830 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4831 &ht_stack, list_stack);
4832 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004833 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004834
4835 if (ht_stack == NULL)
4836 break;
4837
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004838 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004839 cur_ht = ht_stack->ht;
4840 tempitem = ht_stack;
4841 ht_stack = ht_stack->prev;
4842 free(tempitem);
4843 }
4844
4845 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004846}
4847
Dominique Pelle748b3082022-01-08 12:41:16 +00004848#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
4849 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004850/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004851 * Mark a dict and its items with "copyID".
4852 * Returns TRUE if setting references failed somehow.
4853 */
4854 int
4855set_ref_in_dict(dict_T *d, int copyID)
4856{
4857 if (d != NULL && d->dv_copyID != copyID)
4858 {
4859 d->dv_copyID = copyID;
4860 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4861 }
4862 return FALSE;
4863}
Dominique Pelle748b3082022-01-08 12:41:16 +00004864#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004865
4866/*
4867 * Mark a list and its items with "copyID".
4868 * Returns TRUE if setting references failed somehow.
4869 */
4870 int
4871set_ref_in_list(list_T *ll, int copyID)
4872{
4873 if (ll != NULL && ll->lv_copyID != copyID)
4874 {
4875 ll->lv_copyID = copyID;
4876 return set_ref_in_list_items(ll, copyID, NULL);
4877 }
4878 return FALSE;
4879}
4880
4881/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004882 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004883 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4884 *
4885 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004886 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004887 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004888set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004889{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004890 listitem_T *li;
4891 int abort = FALSE;
4892 list_T *cur_l;
4893 list_stack_T *list_stack = NULL;
4894 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004895
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004896 cur_l = l;
4897 for (;;)
4898 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004899 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004900 // Mark each item in the list. If the item contains a hashtab
4901 // it is added to ht_stack, if it contains a list it is added to
4902 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004903 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4904 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4905 ht_stack, &list_stack);
4906 if (list_stack == NULL)
4907 break;
4908
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004909 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004910 cur_l = list_stack->list;
4911 tempitem = list_stack;
4912 list_stack = list_stack->prev;
4913 free(tempitem);
4914 }
4915
4916 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004917}
4918
4919/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00004920 * Mark the partial in callback 'cb' with "copyID".
4921 */
4922 int
4923set_ref_in_callback(callback_T *cb, int copyID)
4924{
4925 typval_T tv;
4926
4927 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
4928 return FALSE;
4929
4930 tv.v_type = VAR_PARTIAL;
4931 tv.vval.v_partial = cb->cb_partial;
4932 return set_ref_in_item(&tv, copyID, NULL, NULL);
4933}
4934
4935/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004936 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004937 * "list_stack" is used to add lists to be marked. Can be NULL.
4938 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4939 *
4940 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004941 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004942 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004943set_ref_in_item(
4944 typval_T *tv,
4945 int copyID,
4946 ht_stack_T **ht_stack,
4947 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004948{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004949 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004950
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004951 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004952 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004953 dict_T *dd = tv->vval.v_dict;
4954
Bram Moolenaara03f2332016-02-06 18:09:59 +01004955 if (dd != NULL && dd->dv_copyID != copyID)
4956 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004957 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004958 dd->dv_copyID = copyID;
4959 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004960 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004961 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4962 }
4963 else
4964 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004965 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4966
Bram Moolenaara03f2332016-02-06 18:09:59 +01004967 if (newitem == NULL)
4968 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004969 else
4970 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004971 newitem->ht = &dd->dv_hashtab;
4972 newitem->prev = *ht_stack;
4973 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004974 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004975 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004976 }
4977 }
4978 else if (tv->v_type == VAR_LIST)
4979 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004980 list_T *ll = tv->vval.v_list;
4981
Bram Moolenaara03f2332016-02-06 18:09:59 +01004982 if (ll != NULL && ll->lv_copyID != copyID)
4983 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004984 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004985 ll->lv_copyID = copyID;
4986 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004987 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004988 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004989 }
4990 else
4991 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004992 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4993
Bram Moolenaara03f2332016-02-06 18:09:59 +01004994 if (newitem == NULL)
4995 abort = TRUE;
4996 else
4997 {
4998 newitem->list = ll;
4999 newitem->prev = *list_stack;
5000 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005001 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005002 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005003 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005004 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005005 else if (tv->v_type == VAR_FUNC)
5006 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005007 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005008 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005009 else if (tv->v_type == VAR_PARTIAL)
5010 {
5011 partial_T *pt = tv->vval.v_partial;
5012 int i;
5013
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005014 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005015 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005016 // Didn't see this partial yet.
5017 pt->pt_copyID = copyID;
5018
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005019 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005020
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005021 if (pt->pt_dict != NULL)
5022 {
5023 typval_T dtv;
5024
5025 dtv.v_type = VAR_DICT;
5026 dtv.vval.v_dict = pt->pt_dict;
5027 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5028 }
5029
5030 for (i = 0; i < pt->pt_argc; ++i)
5031 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5032 ht_stack, list_stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005033 // pt_funcstack is handled in set_ref_in_funcstacks()
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005034 }
5035 }
5036#ifdef FEAT_JOB_CHANNEL
5037 else if (tv->v_type == VAR_JOB)
5038 {
5039 job_T *job = tv->vval.v_job;
5040 typval_T dtv;
5041
5042 if (job != NULL && job->jv_copyID != copyID)
5043 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005044 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005045 if (job->jv_channel != NULL)
5046 {
5047 dtv.v_type = VAR_CHANNEL;
5048 dtv.vval.v_channel = job->jv_channel;
5049 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5050 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005051 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005052 {
5053 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005054 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005055 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5056 }
5057 }
5058 }
5059 else if (tv->v_type == VAR_CHANNEL)
5060 {
5061 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005062 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005063 typval_T dtv;
5064 jsonq_T *jq;
5065 cbq_T *cq;
5066
5067 if (ch != NULL && ch->ch_copyID != copyID)
5068 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005069 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005070 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005071 {
5072 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5073 jq = jq->jq_next)
5074 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5075 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5076 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005077 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005078 {
5079 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005080 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005081 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5082 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005083 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005084 {
5085 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005086 dtv.vval.v_partial =
5087 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005088 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5089 }
5090 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005091 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005092 {
5093 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005094 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005095 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5096 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005097 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005098 {
5099 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005100 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005101 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5102 }
5103 }
5104 }
5105#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005106 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005107}
5108
Bram Moolenaar8c711452005-01-14 21:53:12 +00005109/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005110 * Return a string with the string representation of a variable.
5111 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005112 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005113 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005114 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005115 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005116 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005117 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5118 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005119 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005120 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005121 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005122echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005123 typval_T *tv,
5124 char_u **tofree,
5125 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005126 int copyID,
5127 int echo_style,
5128 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005129 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005130{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005131 static int recurse = 0;
5132 char_u *r = NULL;
5133
Bram Moolenaar33570922005-01-25 22:26:29 +00005134 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005135 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005136 if (!did_echo_string_emsg)
5137 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005138 // Only give this message once for a recursive call to avoid
5139 // flooding the user with errors. And stop iterating over lists
5140 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005141 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005142 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005143 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005144 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005145 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005146 }
5147 ++recurse;
5148
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005149 switch (tv->v_type)
5150 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005151 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005152 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005153 {
5154 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005155 r = tv->vval.v_string;
5156 if (r == NULL)
5157 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005158 }
5159 else
5160 {
5161 *tofree = string_quote(tv->vval.v_string, FALSE);
5162 r = *tofree;
5163 }
5164 break;
5165
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005166 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005167 if (echo_style)
5168 {
5169 *tofree = NULL;
5170 r = tv->vval.v_string;
5171 }
5172 else
5173 {
5174 *tofree = string_quote(tv->vval.v_string, TRUE);
5175 r = *tofree;
5176 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005177 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005178
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005179 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005180 {
5181 partial_T *pt = tv->vval.v_partial;
5182 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005183 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005184 garray_T ga;
5185 int i;
5186 char_u *tf;
5187
5188 ga_init2(&ga, 1, 100);
5189 ga_concat(&ga, (char_u *)"function(");
5190 if (fname != NULL)
5191 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005192 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005193 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005194 && vim_isupper(fname[1]))
5195 {
5196 ga_concat(&ga, (char_u *)"'g:");
5197 ga_concat(&ga, fname + 1);
5198 }
5199 else
5200 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005201 vim_free(fname);
5202 }
5203 if (pt != NULL && pt->pt_argc > 0)
5204 {
5205 ga_concat(&ga, (char_u *)", [");
5206 for (i = 0; i < pt->pt_argc; ++i)
5207 {
5208 if (i > 0)
5209 ga_concat(&ga, (char_u *)", ");
5210 ga_concat(&ga,
5211 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5212 vim_free(tf);
5213 }
5214 ga_concat(&ga, (char_u *)"]");
5215 }
5216 if (pt != NULL && pt->pt_dict != NULL)
5217 {
5218 typval_T dtv;
5219
5220 ga_concat(&ga, (char_u *)", ");
5221 dtv.v_type = VAR_DICT;
5222 dtv.vval.v_dict = pt->pt_dict;
5223 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5224 vim_free(tf);
5225 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005226 // terminate with ')' and a NUL
5227 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005228
5229 *tofree = ga.ga_data;
5230 r = *tofree;
5231 break;
5232 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005233
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005234 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005235 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005236 break;
5237
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005238 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005239 if (tv->vval.v_list == NULL)
5240 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005241 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005242 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005243 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005244 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005245 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5246 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005247 {
5248 *tofree = NULL;
5249 r = (char_u *)"[...]";
5250 }
5251 else
5252 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005253 int old_copyID = tv->vval.v_list->lv_copyID;
5254
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005255 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005256 *tofree = list2string(tv, copyID, restore_copyID);
5257 if (restore_copyID)
5258 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005259 r = *tofree;
5260 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005261 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005262
Bram Moolenaar8c711452005-01-14 21:53:12 +00005263 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005264 if (tv->vval.v_dict == NULL)
5265 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005266 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005267 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005268 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005269 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005270 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5271 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005272 {
5273 *tofree = NULL;
5274 r = (char_u *)"{...}";
5275 }
5276 else
5277 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005278 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005279
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005280 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005281 *tofree = dict2string(tv, copyID, restore_copyID);
5282 if (restore_copyID)
5283 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005284 r = *tofree;
5285 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005286 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005287
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005288 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005289 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005290 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005291 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005292 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005293 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005294 break;
5295
Bram Moolenaar835dc632016-02-07 14:27:38 +01005296 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005297 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005298#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005299 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005300 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5301 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005302 if (composite_val)
5303 {
5304 *tofree = string_quote(r, FALSE);
5305 r = *tofree;
5306 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005307#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005308 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005309
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005310 case VAR_INSTR:
5311 *tofree = NULL;
5312 r = (char_u *)"instructions";
5313 break;
5314
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005315 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005316#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005317 *tofree = NULL;
5318 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5319 r = numbuf;
5320 break;
5321#endif
5322
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005323 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005324 case VAR_SPECIAL:
5325 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005326 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005327 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005328 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005329
Bram Moolenaar8502c702014-06-17 12:51:16 +02005330 if (--recurse == 0)
5331 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005332 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005333}
5334
5335/*
5336 * Return a string with the string representation of a variable.
5337 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5338 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005339 * Does not put quotes around strings, as ":echo" displays values.
5340 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5341 * May return NULL.
5342 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005343 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005344echo_string(
5345 typval_T *tv,
5346 char_u **tofree,
5347 char_u *numbuf,
5348 int copyID)
5349{
5350 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5351}
5352
5353/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005354 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5355 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005356 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005357 */
5358 int
5359buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5360{
5361 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005362 char_u *t;
5363 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005364
5365 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5366 return -1;
5367
5368 if (lnum > buf->b_ml.ml_line_count)
5369 lnum = buf->b_ml.ml_line_count;
5370
5371 str = ml_get_buf(buf, lnum, FALSE);
5372 if (str == NULL)
5373 return -1;
5374
5375 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005376 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005377
Bram Moolenaar91458462021-01-13 20:08:38 +01005378 // count the number of characters
5379 t = str;
5380 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5381 t += mb_ptr2len(t);
5382
5383 // In insert mode, when the cursor is at the end of a non-empty line,
5384 // byteidx points to the NUL character immediately past the end of the
5385 // string. In this case, add one to the character count.
5386 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5387 count++;
5388
5389 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005390}
5391
5392/*
5393 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005394 * byte index. Works only for loaded buffers. Returns -1 on failure.
5395 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005396 */
5397 int
5398buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5399{
5400 char_u *str;
5401 char_u *t;
5402
5403 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5404 return -1;
5405
5406 if (lnum > buf->b_ml.ml_line_count)
5407 lnum = buf->b_ml.ml_line_count;
5408
5409 str = ml_get_buf(buf, lnum, FALSE);
5410 if (str == NULL)
5411 return -1;
5412
5413 // Convert the character offset to a byte offset
5414 t = str;
5415 while (*t != NUL && --charidx > 0)
5416 t += mb_ptr2len(t);
5417
Bram Moolenaar91458462021-01-13 20:08:38 +01005418 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005419}
5420
5421/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005423 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005425 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005426var2fpos(
5427 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005428 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005429 int *fnum, // set to fnum for '0, 'A, etc.
5430 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005432 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005434 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005436 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005437 if (varp->v_type == VAR_LIST)
5438 {
5439 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005440 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005441 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005442 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005443
5444 l = varp->vval.v_list;
5445 if (l == NULL)
5446 return NULL;
5447
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005448 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005449 pos.lnum = list_find_nr(l, 0L, &error);
5450 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005451 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005452 if (charcol)
5453 len = (long)mb_charlen(ml_get(pos.lnum));
5454 else
5455 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005456
Bram Moolenaarec65d772020-08-20 22:29:12 +02005457 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005458 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005459 li = list_find(l, 1L);
5460 if (li != NULL && li->li_tv.v_type == VAR_STRING
5461 && li->li_tv.vval.v_string != NULL
5462 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005463 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005464 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005465 }
5466 else
5467 {
5468 pos.col = list_find_nr(l, 1L, &error);
5469 if (error)
5470 return NULL;
5471 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005472
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005473 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005474 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005475 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005476 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005477
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005478 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005479 pos.coladd = list_find_nr(l, 2L, &error);
5480 if (error)
5481 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005482
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005483 return &pos;
5484 }
5485
Bram Moolenaarc5809432021-03-27 21:23:30 +01005486 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5487 return NULL;
5488
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005489 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005490 if (name == NULL)
5491 return NULL;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005492 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005493 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005494 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005495 pos = curwin->w_cursor;
5496 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005497 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005498 return &pos;
5499 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005500 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005501 {
5502 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005503 pos = VIsual;
5504 else
5505 pos = curwin->w_cursor;
5506 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005507 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005508 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005509 }
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005510 if (name[0] == '\'' && (!in_vim9script()
5511 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005513 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005514 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5516 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005517 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005518 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 return pp;
5520 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005521
Bram Moolenaara5525202006-03-02 22:52:09 +00005522 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005523
Bram Moolenaar477933c2007-07-17 14:32:23 +00005524 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005525 {
5526 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005527 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005528 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005529 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005530 // In silent Ex mode topline is zero, but that's not a valid line
5531 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005532 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005533 return &pos;
5534 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005535 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005536 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005537 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005538 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005539 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005540 return &pos;
5541 }
5542 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005543 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005545 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 {
5547 pos.lnum = curbuf->b_ml.ml_line_count;
5548 pos.col = 0;
5549 }
5550 else
5551 {
5552 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005553 if (charcol)
5554 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5555 else
5556 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 }
5558 return &pos;
5559 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005560 if (in_vim9script())
5561 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 return NULL;
5563}
5564
5565/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005566 * Convert list in "arg" into a position and optional file number.
5567 * When "fnump" is NULL there is no file number, only 3 items.
5568 * Note that the column is passed on as-is, the caller may want to decrement
5569 * it to use 1 for the first column.
5570 * Return FAIL when conversion is not possible, doesn't check the position for
5571 * validity.
5572 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005573 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005574list2fpos(
5575 typval_T *arg,
5576 pos_T *posp,
5577 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005578 colnr_T *curswantp,
5579 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005580{
5581 list_T *l = arg->vval.v_list;
5582 long i = 0;
5583 long n;
5584
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005585 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5586 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005587 if (arg->v_type != VAR_LIST
5588 || l == NULL
5589 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005590 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005591 return FAIL;
5592
5593 if (fnump != NULL)
5594 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005595 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005596 if (n < 0)
5597 return FAIL;
5598 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005599 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005600 *fnump = n;
5601 }
5602
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005603 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005604 if (n < 0)
5605 return FAIL;
5606 posp->lnum = n;
5607
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005608 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005609 if (n < 0)
5610 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005611 // If character position is specified, then convert to byte position
5612 if (charcol)
5613 {
5614 buf_T *buf;
5615
5616 // Get the text for the specified line in a loaded buffer
5617 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5618 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5619 return FAIL;
5620
Bram Moolenaar91458462021-01-13 20:08:38 +01005621 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005622 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005623 posp->col = n;
5624
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005625 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005626 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005627 posp->coladd = 0;
5628 else
5629 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005630
Bram Moolenaar493c1782014-05-28 14:34:46 +02005631 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005632 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005633
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005634 return OK;
5635}
5636
5637/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 * Get the length of an environment variable name.
5639 * Advance "arg" to the first character after the name.
5640 * Return 0 for error.
5641 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005642 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005643get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644{
5645 char_u *p;
5646 int len;
5647
5648 for (p = *arg; vim_isIDc(*p); ++p)
5649 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005650 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 return 0;
5652
5653 len = (int)(p - *arg);
5654 *arg = p;
5655 return len;
5656}
5657
5658/*
5659 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005660 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 * Return 0 if something is wrong.
5662 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005663 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005664get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665{
5666 char_u *p;
5667 int len;
5668
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005669 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005671 {
5672 if (*p == ':')
5673 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005674 // "s:" is start of "s:var", but "n:" is not and can be used in
5675 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005676 len = (int)(p - *arg);
5677 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5678 || len > 1)
5679 break;
5680 }
5681 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005682 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 return 0;
5684
5685 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005686 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687
5688 return len;
5689}
5690
5691/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005692 * Get the length of the name of a variable or function.
5693 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005695 * Return -1 if curly braces expansion failed.
5696 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 * If the name contains 'magic' {}'s, expand them and return the
5698 * expanded name in an allocated string via 'alias' - caller must free.
5699 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005700 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005701get_name_len(
5702 char_u **arg,
5703 char_u **alias,
5704 int evaluate,
5705 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706{
5707 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 char_u *p;
5709 char_u *expr_start;
5710 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005712 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713
5714 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5715 && (*arg)[2] == (int)KE_SNR)
5716 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005717 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 *arg += 3;
5719 return get_id_len(arg) + 3;
5720 }
5721 len = eval_fname_script(*arg);
5722 if (len > 0)
5723 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005724 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 *arg += len;
5726 }
5727
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005729 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005731 p = find_name_end(*arg, &expr_start, &expr_end,
5732 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 if (expr_start != NULL)
5734 {
5735 char_u *temp_string;
5736
5737 if (!evaluate)
5738 {
5739 len += (int)(p - *arg);
5740 *arg = skipwhite(p);
5741 return len;
5742 }
5743
5744 /*
5745 * Include any <SID> etc in the expanded string:
5746 * Thus the -len here.
5747 */
5748 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5749 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005750 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 *alias = temp_string;
5752 *arg = skipwhite(p);
5753 return (int)STRLEN(temp_string);
5754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755
5756 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005757 // Only give an error when there is something, otherwise it will be
5758 // reported at a higher level.
5759 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005760 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761
5762 return len;
5763}
5764
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005765/*
5766 * Find the end of a variable or function name, taking care of magic braces.
5767 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5768 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005769 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005770 * Return a pointer to just after the name. Equal to "arg" if there is no
5771 * valid name.
5772 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005773 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005774find_name_end(
5775 char_u *arg,
5776 char_u **expr_start,
5777 char_u **expr_end,
5778 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005780 int mb_nest = 0;
5781 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005783 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005784 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005786 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005788 *expr_start = NULL;
5789 *expr_end = NULL;
5790 }
5791
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005792 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005793 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5794 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005795 return arg;
5796
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005797 for (p = arg; *p != NUL
5798 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005799 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005800 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005801 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005802 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005803 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005804 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005805 if (*p == '\'')
5806 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005807 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005808 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005809 ;
5810 if (*p == NUL)
5811 break;
5812 }
5813 else if (*p == '"')
5814 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005815 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005816 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005817 if (*p == '\\' && p[1] != NUL)
5818 ++p;
5819 if (*p == NUL)
5820 break;
5821 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005822 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5823 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005824 // "s:" is start of "s:var", but "n:" is not and can be used in
5825 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005826 len = (int)(p - arg);
5827 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005828 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005829 break;
5830 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005831
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005832 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005834 if (*p == '[')
5835 ++br_nest;
5836 else if (*p == ']')
5837 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005839
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005840 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005842 if (*p == '{')
5843 {
5844 mb_nest++;
5845 if (expr_start != NULL && *expr_start == NULL)
5846 *expr_start = p;
5847 }
5848 else if (*p == '}')
5849 {
5850 mb_nest--;
5851 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5852 *expr_end = p;
5853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 }
5856
5857 return p;
5858}
5859
5860/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005861 * Expands out the 'magic' {}'s in a variable/function name.
5862 * Note that this can call itself recursively, to deal with
5863 * constructs like foo{bar}{baz}{bam}
5864 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5865 * "in_start" ^
5866 * "expr_start" ^
5867 * "expr_end" ^
5868 * "in_end" ^
5869 *
5870 * Returns a new allocated string, which the caller must free.
5871 * Returns NULL for failure.
5872 */
5873 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005874make_expanded_name(
5875 char_u *in_start,
5876 char_u *expr_start,
5877 char_u *expr_end,
5878 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005879{
5880 char_u c1;
5881 char_u *retval = NULL;
5882 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005883
5884 if (expr_end == NULL || in_end == NULL)
5885 return NULL;
5886 *expr_start = NUL;
5887 *expr_end = NUL;
5888 c1 = *in_end;
5889 *in_end = NUL;
5890
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005891 temp_result = eval_to_string(expr_start + 1, FALSE);
5892 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005893 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005894 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5895 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005896 if (retval != NULL)
5897 {
5898 STRCPY(retval, in_start);
5899 STRCAT(retval, temp_result);
5900 STRCAT(retval, expr_end + 1);
5901 }
5902 }
5903 vim_free(temp_result);
5904
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005905 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005906 *expr_start = '{';
5907 *expr_end = '}';
5908
5909 if (retval != NULL)
5910 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005911 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005912 if (expr_start != NULL)
5913 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005914 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005915 temp_result = make_expanded_name(retval, expr_start,
5916 expr_end, temp_result);
5917 vim_free(retval);
5918 retval = temp_result;
5919 }
5920 }
5921
5922 return retval;
5923}
5924
5925/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005927 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005929 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005930eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005932 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005933}
5934
5935/*
5936 * Return TRUE if character "c" can be used as the first character in a
5937 * variable or function name (excluding '{' and '}').
5938 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005939 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005940eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005941{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005942 return ASCII_ISALPHA(c) || c == '_';
5943}
5944
5945/*
5946 * Return TRUE if character "c" can be used as the first character of a
5947 * dictionary key.
5948 */
5949 int
5950eval_isdictc(int c)
5951{
5952 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953}
5954
5955/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005956 * Handle:
5957 * - expr[expr], expr[expr:expr] subscript
5958 * - ".name" lookup
5959 * - function call with Funcref variable: func(expr)
5960 * - method call: var->method()
5961 *
5962 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00005963 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005964 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005965 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005966handle_subscript(
5967 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00005968 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01005969 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005970 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005971 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005972{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005973 int evaluate = evalarg != NULL
5974 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005975 int ret = OK;
5976 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005977 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005978 int getnext;
5979 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005980
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005981 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005982 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005983 // When at the end of the line and ".name" or "->{" or "->X" follows in
5984 // the next line then consume the line break.
5985 p = eval_next_non_blank(*arg, evalarg, &getnext);
5986 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005987 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02005988 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
5989 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
5990 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005991 {
5992 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005993 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005994 check_white = FALSE;
5995 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005996
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01005997 if (rettv->v_type == VAR_ANY)
5998 {
5999 char_u *exp_name;
6000 int cc;
6001 int idx;
6002 ufunc_T *ufunc;
6003 type_T *type;
6004
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006005 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006006 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006007 if (**arg != '.')
6008 {
6009 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006010 semsg(_(e_expected_dot_after_name_str),
6011 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006012 ret = FAIL;
6013 break;
6014 }
6015 ++*arg;
6016 if (IS_WHITE_OR_NUL(**arg))
6017 {
6018 if (verbose)
6019 emsg(_(e_no_white_space_allowed_after_dot));
6020 ret = FAIL;
6021 break;
6022 }
6023
6024 // isolate the name
6025 exp_name = *arg;
6026 while (eval_isnamec(**arg))
6027 ++*arg;
6028 cc = **arg;
6029 **arg = NUL;
6030
6031 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
6032 evalarg->eval_cctx, verbose);
6033 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006034
6035 if (idx < 0 && ufunc == NULL)
6036 {
6037 ret = FAIL;
6038 break;
6039 }
6040 if (idx >= 0)
6041 {
6042 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6043 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6044
6045 copy_tv(sv->sv_tv, rettv);
6046 }
6047 else
6048 {
6049 rettv->v_type = VAR_FUNC;
6050 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6051 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006052 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006053 }
6054
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006055 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6056 || rettv->v_type == VAR_PARTIAL))
6057 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006058 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006059 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6060 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006061
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006062 // Stop the expression evaluation when immediately aborting on
6063 // error, or when an interrupt occurred or an exception was thrown
6064 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006065 if (aborting())
6066 {
6067 if (ret == OK)
6068 clear_tv(rettv);
6069 ret = FAIL;
6070 }
6071 dict_unref(selfdict);
6072 selfdict = NULL;
6073 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006074 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006075 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006076 if (in_vim9script())
6077 *arg = skipwhite(p + 2);
6078 else
6079 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006080 if (ret == OK)
6081 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006082 if (VIM_ISWHITE(**arg))
6083 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00006084 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006085 ret = FAIL;
6086 }
6087 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01006088 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006089 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006090 else
6091 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02006092 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006093 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02006094 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006095 // "." is ".name" lookup when we found a dict or when evaluating and
6096 // scriptversion is at least 2, where string concatenation is "..".
6097 else if (**arg == '['
6098 || (**arg == '.' && (rettv->v_type == VAR_DICT
6099 || (!evaluate
6100 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006101 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006102 {
6103 dict_unref(selfdict);
6104 if (rettv->v_type == VAR_DICT)
6105 {
6106 selfdict = rettv->vval.v_dict;
6107 if (selfdict != NULL)
6108 ++selfdict->dv_refcount;
6109 }
6110 else
6111 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006112 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006113 {
6114 clear_tv(rettv);
6115 ret = FAIL;
6116 }
6117 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006118 else
6119 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006120 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006121
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006122 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6123 // Don't do this when "Func" is already a partial that was bound
6124 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006125 if (selfdict != NULL
6126 && (rettv->v_type == VAR_FUNC
6127 || (rettv->v_type == VAR_PARTIAL
6128 && (rettv->vval.v_partial->pt_auto
6129 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006130 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006131
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006132 dict_unref(selfdict);
6133 return ret;
6134}
6135
6136/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006137 * Make a copy of an item.
6138 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006139 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6140 * reference to an already copied list/dict can be used.
6141 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006142 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006143 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006144item_copy(
6145 typval_T *from,
6146 typval_T *to,
6147 int deep,
6148 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006149{
6150 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006151 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006152
Bram Moolenaar33570922005-01-25 22:26:29 +00006153 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006154 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006155 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006156 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006157 }
6158 ++recurse;
6159
6160 switch (from->v_type)
6161 {
6162 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006163 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006164 case VAR_STRING:
6165 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006166 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006167 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006168 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006169 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006170 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006171 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006172 copy_tv(from, to);
6173 break;
6174 case VAR_LIST:
6175 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006176 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006177 if (from->vval.v_list == NULL)
6178 to->vval.v_list = NULL;
6179 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6180 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006181 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006182 to->vval.v_list = from->vval.v_list->lv_copylist;
6183 ++to->vval.v_list->lv_refcount;
6184 }
6185 else
6186 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
6187 if (to->vval.v_list == NULL)
6188 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006189 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006190 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006191 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006192 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006193 case VAR_DICT:
6194 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006195 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006196 if (from->vval.v_dict == NULL)
6197 to->vval.v_dict = NULL;
6198 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6199 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006200 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006201 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6202 ++to->vval.v_dict->dv_refcount;
6203 }
6204 else
6205 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
6206 if (to->vval.v_dict == NULL)
6207 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006208 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006209 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006210 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006211 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006212 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006213 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006214 }
6215 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006216 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006217}
6218
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006219 void
6220echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6221{
6222 char_u *tofree;
6223 char_u numbuf[NUMBUFLEN];
6224 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6225
6226 if (*atstart)
6227 {
6228 *atstart = FALSE;
6229 // Call msg_start() after eval1(), evaluating the expression
6230 // may cause a message to appear.
6231 if (with_space)
6232 {
6233 // Mark the saved text as finishing the line, so that what
6234 // follows is displayed on a new line when scrolling back
6235 // at the more prompt.
6236 msg_sb_eol();
6237 msg_start();
6238 }
6239 }
6240 else if (with_space)
6241 msg_puts_attr(" ", echo_attr);
6242
6243 if (p != NULL)
6244 for ( ; *p != NUL && !got_int; ++p)
6245 {
6246 if (*p == '\n' || *p == '\r' || *p == TAB)
6247 {
6248 if (*p != TAB && *needclr)
6249 {
6250 // remove any text still there from the command
6251 msg_clr_eos();
6252 *needclr = FALSE;
6253 }
6254 msg_putchar_attr(*p, echo_attr);
6255 }
6256 else
6257 {
6258 if (has_mbyte)
6259 {
6260 int i = (*mb_ptr2len)(p);
6261
6262 (void)msg_outtrans_len_attr(p, i, echo_attr);
6263 p += i - 1;
6264 }
6265 else
6266 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6267 }
6268 }
6269 vim_free(tofree);
6270}
6271
Bram Moolenaare9a41262005-01-15 22:18:47 +00006272/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 * ":echo expr1 ..." print each argument separated with a space, add a
6274 * newline at the end.
6275 * ":echon expr1 ..." print each argument plain.
6276 */
6277 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006278ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279{
6280 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006281 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006282 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 int needclr = TRUE;
6284 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006285 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006286 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006287 evalarg_T evalarg;
6288
Bram Moolenaare707c882020-07-01 13:07:37 +02006289 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290
6291 if (eap->skip)
6292 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006293 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006295 // If eval1() causes an error message the text from the command may
6296 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006297 need_clr_eos = needclr;
6298
Bram Moolenaar68db9962021-05-09 23:19:22 +02006299 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006300 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 {
6302 /*
6303 * Report the invalid expression unless the expression evaluation
6304 * has been cancelled due to an aborting error, an interrupt, or an
6305 * exception.
6306 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006307 if (!aborting() && did_emsg == did_emsg_before
6308 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006309 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006310 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 break;
6312 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006313 need_clr_eos = FALSE;
6314
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006316 {
6317 if (rettv.v_type == VAR_VOID)
6318 {
6319 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6320 break;
6321 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006322 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006323 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006324
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006325 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 arg = skipwhite(arg);
6327 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006328 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006329 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330
6331 if (eap->skip)
6332 --emsg_skip;
6333 else
6334 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006335 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 if (needclr)
6337 msg_clr_eos();
6338 if (eap->cmdidx == CMD_echo)
6339 msg_end();
6340 }
6341}
6342
6343/*
6344 * ":echohl {name}".
6345 */
6346 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006347ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006349 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350}
6351
6352/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006353 * Returns the :echo attribute
6354 */
6355 int
6356get_echo_attr(void)
6357{
6358 return echo_attr;
6359}
6360
6361/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 * ":execute expr1 ..." execute the result of an expression.
6363 * ":echomsg expr1 ..." Print a message
6364 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006365 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 * Each gets spaces around each argument and a newline at the end for
6367 * echo commands
6368 */
6369 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006370ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371{
6372 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006373 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 int ret = OK;
6375 char_u *p;
6376 garray_T ga;
6377 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006378 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379
6380 ga_init2(&ga, 1, 80);
6381
6382 if (eap->skip)
6383 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006384 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006386 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006387 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389
6390 if (!eap->skip)
6391 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006392 char_u buf[NUMBUFLEN];
6393
6394 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006395 {
6396 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6397 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006398 semsg(_(e_using_invalid_value_as_string_str),
6399 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006400 p = NULL;
6401 }
6402 else
6403 p = tv_get_string_buf(&rettv, buf);
6404 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006405 else
6406 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006407 if (p == NULL)
6408 {
6409 clear_tv(&rettv);
6410 ret = FAIL;
6411 break;
6412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 len = (int)STRLEN(p);
6414 if (ga_grow(&ga, len + 2) == FAIL)
6415 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006416 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 ret = FAIL;
6418 break;
6419 }
6420 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 ga.ga_len += len;
6424 }
6425
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006426 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 arg = skipwhite(arg);
6428 }
6429
6430 if (ret != FAIL && ga.ga_data != NULL)
6431 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006432 // use the first line of continuation lines for messages
6433 SOURCING_LNUM = start_lnum;
6434
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006435 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6436 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006437 // Mark the already saved text as finishing the line, so that what
6438 // follows is displayed on a new line when scrolling back at the
6439 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006440 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006441 }
6442
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006444 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006445 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006446 out_flush();
6447 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006448 else if (eap->cmdidx == CMD_echoconsole)
6449 {
6450 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6451 ui_write((char_u *)"\r\n", 2, TRUE);
6452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 else if (eap->cmdidx == CMD_echoerr)
6454 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006455 int save_did_emsg = did_emsg;
6456
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006457 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006458 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 if (!force_abort)
6460 did_emsg = save_did_emsg;
6461 }
6462 else if (eap->cmdidx == CMD_execute)
6463 do_cmdline((char_u *)ga.ga_data,
6464 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6465 }
6466
6467 ga_clear(&ga);
6468
6469 if (eap->skip)
6470 --emsg_skip;
6471
Bram Moolenaar63b91732021-08-05 20:40:03 +02006472 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473}
6474
6475/*
6476 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6477 * "arg" points to the "&" or '+' when called, to "option" when returning.
6478 * Returns NULL when no option name found. Otherwise pointer to the char
6479 * after the option name.
6480 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006481 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006482find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483{
6484 char_u *p = *arg;
6485
6486 ++p;
6487 if (*p == 'g' && p[1] == ':')
6488 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006489 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 p += 2;
6491 }
6492 else if (*p == 'l' && p[1] == ':')
6493 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006494 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 p += 2;
6496 }
6497 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006498 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499
6500 if (!ASCII_ISALPHA(*p))
6501 return NULL;
6502 *arg = p;
6503
6504 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006505 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 else
6507 while (ASCII_ISALPHA(*p))
6508 ++p;
6509 return p;
6510}
6511
6512/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006513 * Display script name where an item was last set.
6514 * Should only be invoked when 'verbose' is non-zero.
6515 */
6516 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006517last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006518{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006519 char_u *p;
6520
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006521 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006522 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006523 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006524 if (p != NULL)
6525 {
6526 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006527 msg_puts(_("\n\tLast set from "));
6528 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006529 if (script_ctx.sc_lnum > 0)
6530 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006531 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006532 msg_outnum((long)script_ctx.sc_lnum);
6533 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006534 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006535 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006536 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006537 }
6538}
6539
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006540#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541
6542/*
6543 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006544 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 * "flags" can be "g" to do a global substitute.
6546 * Returns an allocated string, NULL for error.
6547 */
6548 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006549do_string_sub(
6550 char_u *str,
6551 char_u *pat,
6552 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006553 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006554 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555{
6556 int sublen;
6557 regmatch_T regmatch;
6558 int i;
6559 int do_all;
6560 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006561 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 garray_T ga;
6563 char_u *ret;
6564 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006565 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006567 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006569 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570
6571 ga_init2(&ga, 1, 200);
6572
6573 do_all = (flags[0] == 'g');
6574
6575 regmatch.rm_ic = p_ic;
6576 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6577 if (regmatch.regprog != NULL)
6578 {
6579 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006580 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6582 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006583 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006584 if (regmatch.startp[0] == regmatch.endp[0])
6585 {
6586 if (zero_width == regmatch.startp[0])
6587 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006588 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006589 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006590 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6591 (size_t)i);
6592 ga.ga_len += i;
6593 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006594 continue;
6595 }
6596 zero_width = regmatch.startp[0];
6597 }
6598
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 /*
6600 * Get some space for a temporary buffer to do the substitution
6601 * into. It will contain:
6602 * - The text up to where the match is.
6603 * - The substituted text.
6604 * - The text after the match.
6605 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006606 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006607 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6609 {
6610 ga_clear(&ga);
6611 break;
6612 }
6613
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006614 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 i = (int)(regmatch.startp[0] - tail);
6616 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006617 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006618 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 + ga.ga_len + i, TRUE, TRUE, FALSE);
6620 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006621 tail = regmatch.endp[0];
6622 if (*tail == NUL)
6623 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 if (!do_all)
6625 break;
6626 }
6627
6628 if (ga.ga_data != NULL)
6629 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6630
Bram Moolenaar473de612013-06-08 18:19:48 +02006631 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 }
6633
6634 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6635 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006636 if (p_cpo == empty_option)
6637 p_cpo = save_cpo;
6638 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006639 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006640 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006641 // If it's still empty it was changed and restored, need to restore in
6642 // the complicated way.
6643 if (*p_cpo == NUL)
6644 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006645 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647
6648 return ret;
6649}