blob: ec896814d46eee96c6ebaca9c6ac9f23dab92822 [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 {
490 ga_init2(&ga, (int)sizeof(char), 80);
491 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 Moolenaarb544f3c2017-02-23 19:03:28 +0100629 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200630 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
631 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000632 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200634 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100635call_vim_function(
636 char_u *func,
637 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200638 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200639 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000641 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200642 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100644 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200645 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000646 funcexe.fe_firstline = curwin->w_cursor.lnum;
647 funcexe.fe_lastline = curwin->w_cursor.lnum;
648 funcexe.fe_evaluate = TRUE;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200649 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000650 if (ret == FAIL)
651 clear_tv(rettv);
652
653 return ret;
654}
655
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100656/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100657 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000658 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
659 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000660 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000661 */
662 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100663call_func_retstr(
664 char_u *func,
665 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200666 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000667{
668 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000669 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000670
Bram Moolenaarded27a12018-08-01 19:06:03 +0200671 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000672 return NULL;
673
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100674 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000675 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 return retval;
677}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000678
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000679/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100680 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000681 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000682 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000683 */
684 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100685call_func_retlist(
686 char_u *func,
687 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200688 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000689{
690 typval_T rettv;
691
Bram Moolenaarded27a12018-08-01 19:06:03 +0200692 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000693 return NULL;
694
695 if (rettv.v_type != VAR_LIST)
696 {
697 clear_tv(&rettv);
698 return NULL;
699 }
700
701 return rettv.vval.v_list;
702}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704#ifdef FEAT_FOLDING
705/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100706 * Evaluate "arg", which is 'foldexpr'.
707 * Note: caller must set "curwin" to match "arg".
708 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
709 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 */
711 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100712eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713{
Bram Moolenaar33570922005-01-25 22:26:29 +0000714 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200715 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000717 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
718 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719
720 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000721 if (use_sandbox)
722 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200723 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200725 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 retval = 0;
727 else
728 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100729 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000730 if (tv.v_type == VAR_NUMBER)
731 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000732 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 retval = 0;
734 else
735 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100736 // If the result is a string, check if there is a non-digit before
737 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000738 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 if (!VIM_ISDIGIT(*s) && *s != '-')
740 *cp = *s++;
741 retval = atol((char *)s);
742 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000743 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 }
745 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000746 if (use_sandbox)
747 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200748 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200749 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200751 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752}
753#endif
754
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000756 * Get an lval: variable, Dict item or List item that can be assigned a value
757 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
758 * "name.key", "name.key[expr]" etc.
759 * Indexing only works if "name" is an existing List or Dictionary.
760 * "name" points to the start of the name.
761 * If "rettv" is not NULL it points to the value to be assigned.
762 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
763 * wrong; must end in space or cmd separator.
764 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100765 * flags:
766 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100767 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100768 * GLV_NO_AUTOLOAD: do not use script autoloading
769 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000770 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000771 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000772 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000773 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200774 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100775get_lval(
776 char_u *name,
777 typval_T *rettv,
778 lval_T *lp,
779 int unlet,
780 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100781 int flags, // GLV_ values
782 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000783{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000784 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000785 char_u *expr_start, *expr_end;
786 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000787 dictitem_T *v;
788 typval_T var1;
789 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000790 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000791 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000792 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100793 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100794 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100795 int writing;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000796
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100797 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200798 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000799
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100800 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000801 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100802 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000803 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200804 lp->ll_name_end = find_name_end(name, NULL, NULL,
805 FNE_INCL_BR | fne_flags);
806 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000807 }
808
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100809 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000810 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100811 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000812 if (expr_start != NULL)
813 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100814 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100815 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000816 && *p != '[' && *p != '.')
817 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000818 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000819 return NULL;
820 }
821
822 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
823 if (lp->ll_exp_name == NULL)
824 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100825 // Report an invalid expression in braces, unless the
826 // expression evaluation has been cancelled due to an
827 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000828 if (!aborting() && !quiet)
829 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000830 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000831 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000832 return NULL;
833 }
834 }
835 lp->ll_name = lp->ll_exp_name;
836 }
837 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100838 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000839 lp->ll_name = name;
840
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200841 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100842 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200843 // "a: type" is declaring variable "a" with a type, not "a:".
844 if (p == name + 2 && p[-1] == ':')
845 {
846 --p;
847 lp->ll_name_end = p;
848 }
849 if (*p == ':')
850 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000851 garray_T tmp_type_list;
852 garray_T *type_list;
853 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +0200854
855 if (tp == p + 1 && !quiet)
856 {
857 semsg(_(e_white_space_required_after_str_str), ":", p);
858 return NULL;
859 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100860
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000861 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
862 type_list = &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list;
863 else
864 {
865 type_list = &tmp_type_list;
866 ga_init2(type_list, sizeof(type_T), 10);
867 }
868
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200869 // parse the type after the name
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000870 lp->ll_type = parse_type(&tp, type_list, !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100871 if (lp->ll_type == NULL && !quiet)
872 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200873 lp->ll_name_end = tp;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000874
875 // drop the type when not in a script
876 if (type_list == &tmp_type_list)
877 {
878 lp->ll_type = NULL;
879 clear_type_list(type_list);
880 }
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200881 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100882 }
883 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000884 if (lp->ll_name == NULL)
885 return p;
886
887 if (*p == '.' && in_vim9script())
888 {
889 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, NULL);
890 if (import != NULL)
891 {
892 ufunc_T *ufunc;
893 type_T *type;
894
895 lp->ll_sid = import->imp_sid;
896 lp->ll_name = skipwhite(p + 1);
897 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
898 lp->ll_name_end = p;
899
900 // check the item is exported
901 cc = *p;
902 *p = NUL;
903 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
904 NULL, TRUE) == -1)
905 {
906 *p = cc;
907 return FAIL;
908 }
909 *p = cc;
910 }
911 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100912
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100913 // Without [idx] or .key we are done.
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000914 if ((*p != '[' && *p != '.'))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000915 return p;
916
Bram Moolenaaraacc9662021-08-13 19:40:51 +0200917 if (in_vim9script() && lval_root != NULL)
918 {
919 // using local variable
920 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +0200921 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +0200922 }
923 else
924 {
925 cc = *p;
926 *p = NUL;
927 // When we would write to the variable pass &ht and prevent autoload.
928 writing = !(flags & GLV_READ_ONLY);
929 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100930 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +0200931 if (v == NULL && !quiet)
932 semsg(_(e_undefined_variable_str), lp->ll_name);
933 *p = cc;
934 if (v == NULL)
935 return NULL;
936 lp->ll_tv = &v->di_tv;
937 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000938
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +0100939 if (in_vim9script() && (flags & GLV_NO_DECL) == 0)
940 {
941 if (!quiet)
942 semsg(_(e_variable_already_declared), lp->ll_name);
943 return NULL;
944 }
945
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000946 /*
947 * Loop until no more [idx] or .key is following.
948 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100949 var1.v_type = VAR_UNKNOWN;
950 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +0200951 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000952 {
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +0200953 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
954 {
955 if (!quiet)
956 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
957 return NULL;
958 }
Bram Moolenaare65081d2021-06-27 15:04:05 +0200959 if (lp->ll_tv->v_type != VAR_LIST
960 && lp->ll_tv->v_type != VAR_DICT
961 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000962 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000963 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +0000964 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000965 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000966 }
Bram Moolenaare65081d2021-06-27 15:04:05 +0200967
968 // a NULL list/blob works like an empty list/blob, allocate one now.
969 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
970 rettv_list_alloc(lp->ll_tv);
971 else if (lp->ll_tv->v_type == VAR_BLOB
972 && lp->ll_tv->vval.v_blob == NULL)
973 rettv_blob_alloc(lp->ll_tv);
974
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000975 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000976 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000977 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +0000978 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000979 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000980 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000981
Bram Moolenaar10c65862020-10-08 21:16:42 +0200982 if (in_vim9script() && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +0200983 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +0200984 && lp->ll_tv == &v->di_tv
985 && ht != NULL && ht == get_script_local_ht())
986 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000987 svar_T *sv = find_typval_in_script(lp->ll_tv, 0);
Bram Moolenaar10c65862020-10-08 21:16:42 +0200988
989 // Vim9 script local variable: get the type
990 if (sv != NULL)
991 lp->ll_valtype = sv->sv_type;
992 }
993
Bram Moolenaar8c711452005-01-14 21:53:12 +0000994 len = -1;
995 if (*p == '.')
996 {
997 key = p + 1;
998 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
999 ;
1000 if (len == 0)
1001 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001002 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001003 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001004 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001005 }
1006 p = key + len;
1007 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001008 else
1009 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001010 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001011 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001012 if (*p == ':')
1013 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001014 else
1015 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001016 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001017 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001018 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001019 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001020 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001021 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001022 clear_tv(&var1);
1023 return NULL;
1024 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001025 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001026 }
1027
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001028 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001029 if (*p == ':')
1030 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001031 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001032 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001033 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001034 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001035 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001036 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001037 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001038 if (rettv != NULL
1039 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001040 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001041 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001042 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001043 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001044 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001045 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001046 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001047 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001048 }
1049 p = skipwhite(p + 1);
1050 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001051 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001052 else
1053 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001054 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001055 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001056 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001057 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001058 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001059 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001060 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001061 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001062 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001063 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001064 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001065 clear_tv(&var2);
1066 return NULL;
1067 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001068 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001069 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001070 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001071 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001072 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001073
Bram Moolenaar8c711452005-01-14 21:53:12 +00001074 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001075 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001076 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001077 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001078 clear_tv(&var1);
1079 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001080 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001081 }
1082
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001083 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001084 ++p;
1085 }
1086
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001087 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001088 {
1089 if (len == -1)
1090 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001091 // "[key]": get key from "var1"
1092 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001093 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001094 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001095 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001096 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001097 }
1098 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001099 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001100
1101 // a NULL dict is equivalent with an empty dict
1102 if (lp->ll_tv->vval.v_dict == NULL)
1103 {
1104 lp->ll_tv->vval.v_dict = dict_alloc();
1105 if (lp->ll_tv->vval.v_dict == NULL)
1106 {
1107 clear_tv(&var1);
1108 return NULL;
1109 }
1110 ++lp->ll_tv->vval.v_dict->dv_refcount;
1111 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001112 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001113
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001114 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001115
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001116 // When assigning to a scope dictionary check that a function and
1117 // variable name is valid (only variable name unless it is l: or
1118 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001119 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001120 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001121 int prevval;
1122 int wrong;
1123
1124 if (len != -1)
1125 {
1126 prevval = key[len];
1127 key[len] = NUL;
1128 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001129 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001130 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001131 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1132 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001133 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001134 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001135 if (len != -1)
1136 key[len] = prevval;
1137 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001138 {
1139 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001140 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001141 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001142 }
1143
Bram Moolenaar10c65862020-10-08 21:16:42 +02001144 if (lp->ll_valtype != NULL)
1145 // use the type of the member
1146 lp->ll_valtype = lp->ll_valtype->tt_member;
1147
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001148 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001149 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001150 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001151 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001152 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001153 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001154 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001155 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001156 return NULL;
1157 }
1158
Bram Moolenaar31b81602019-02-10 22:14:27 +01001159 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001160 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001161 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001162 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001163 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001164 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001165 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001166 }
1167 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001168 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001169 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001170 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001171 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001172 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001173 p = NULL;
1174 break;
1175 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001176 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001177 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001178 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1179 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001180 {
1181 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001182 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001183 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001184
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001185 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001186 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001187 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001188 else if (lp->ll_tv->v_type == VAR_BLOB)
1189 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001190 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1191
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001192 /*
1193 * Get the number and item for the only or first index of the List.
1194 */
1195 if (empty1)
1196 lp->ll_n1 = 0;
1197 else
1198 // is number or string
1199 lp->ll_n1 = (long)tv_get_number(&var1);
1200 clear_tv(&var1);
1201
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001202 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001203 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001204 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001205 return NULL;
1206 }
1207 if (lp->ll_range && !lp->ll_empty2)
1208 {
1209 lp->ll_n2 = (long)tv_get_number(&var2);
1210 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001211 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1212 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001213 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001214 }
1215 lp->ll_blob = lp->ll_tv->vval.v_blob;
1216 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001217 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001218 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001219 else
1220 {
1221 /*
1222 * Get the number and item for the only or first index of the List.
1223 */
1224 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001225 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001226 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001227 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001228 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001229 clear_tv(&var1);
1230
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001231 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001232 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001233 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001234 if (lp->ll_li == NULL)
1235 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001236 clear_tv(&var2);
1237 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001238 }
1239
Bram Moolenaar10c65862020-10-08 21:16:42 +02001240 if (lp->ll_valtype != NULL)
1241 // use the type of the member
1242 lp->ll_valtype = lp->ll_valtype->tt_member;
1243
Bram Moolenaar8c711452005-01-14 21:53:12 +00001244 /*
1245 * May need to find the item or absolute index for the second
1246 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001247 * When no index given: "lp->ll_empty2" is TRUE.
1248 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001249 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001250 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001251 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001252 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001253 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001254 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001255 if (check_range_index_two(lp->ll_list,
1256 &lp->ll_n1, lp->ll_li,
1257 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001258 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001259 }
1260
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001261 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001262 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001263 }
1264
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001265 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001266 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001267 return p;
1268}
1269
1270/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001271 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001272 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001273 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001274clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001275{
1276 vim_free(lp->ll_exp_name);
1277 vim_free(lp->ll_newkey);
1278}
1279
1280/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001281 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001282 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001283 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1284 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001285 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001286 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001287set_var_lval(
1288 lval_T *lp,
1289 char_u *endp,
1290 typval_T *rettv,
1291 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001292 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1293 char_u *op,
1294 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001295{
1296 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001297 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001298
1299 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001300 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001301 cc = *endp;
1302 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001303 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1304 return;
1305
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001306 if (lp->ll_blob != NULL)
1307 {
1308 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001309
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001310 if (op != NULL && *op != '=')
1311 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001312 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001313 return;
1314 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001315 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001316 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001317
1318 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1319 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001320 if (lp->ll_empty2)
1321 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1322
Bram Moolenaar68452172021-04-12 21:21:02 +02001323 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1324 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001325 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001326 }
1327 else
1328 {
1329 val = (int)tv_get_number_chk(rettv, &error);
1330 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001331 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001332 }
1333 }
1334 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001335 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001336 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001337
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001338 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1339 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001340 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001341 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001342 *endp = cc;
1343 return;
1344 }
1345
Bram Moolenaarff697e62019-02-12 22:28:33 +01001346 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001347 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001348 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001349 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001350 {
1351 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001352 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1353 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001354 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001355 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001356 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001357 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001358 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001359 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001360 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001361 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001362 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1363 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001364 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001365 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001366 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001367 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001368 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001369 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001370 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001371 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001372 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001373 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001374 else if (lp->ll_range)
1375 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001376 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1377 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001378 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001379 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001380 return;
1381 }
1382
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001383 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1384 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001385 }
1386 else
1387 {
1388 /*
1389 * Assign to a List or Dictionary item.
1390 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001391 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1392 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001393 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001394 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001395 return;
1396 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001397
1398 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001399 && check_typval_arg_type(lp->ll_valtype, rettv,
1400 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001401 return;
1402
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001403 if (lp->ll_newkey != NULL)
1404 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001405 if (op != NULL && *op != '=')
1406 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001407 semsg(_(e_key_not_present_in_dictionary), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001408 return;
1409 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001410 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1411 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001412 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001413
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001414 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001415 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001416 if (di == NULL)
1417 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001418 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1419 {
1420 vim_free(di);
1421 return;
1422 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001423 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001424 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001425 else if (op != NULL && *op != '=')
1426 {
1427 tv_op(lp->ll_tv, rettv, op);
1428 return;
1429 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001430 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001431 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001432
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001433 /*
1434 * Assign the value to the variable or list item.
1435 */
1436 if (copy)
1437 copy_tv(rettv, lp->ll_tv);
1438 else
1439 {
1440 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001441 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001442 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001443 }
1444 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001445}
1446
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001447/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001448 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1449 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001450 * Returns OK or FAIL.
1451 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001452 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001453tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001454{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001455 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001456 char_u numbuf[NUMBUFLEN];
1457 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001458 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001459
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001460 // Can't do anything with a Funcref or Dict on the right.
1461 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001462 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001463 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1464 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001465 {
1466 switch (tv1->v_type)
1467 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001468 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001469 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001470 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001471 case VAR_DICT:
1472 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001473 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001474 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001475 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001476 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001477 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001478 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001479 break;
1480
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001481 case VAR_BLOB:
1482 if (*op != '+' || tv2->v_type != VAR_BLOB)
1483 break;
1484 // BLOB += BLOB
1485 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1486 {
1487 blob_T *b1 = tv1->vval.v_blob;
1488 blob_T *b2 = tv2->vval.v_blob;
1489 int i, len = blob_len(b2);
1490 for (i = 0; i < len; i++)
1491 ga_append(&b1->bv_ga, blob_get(b2, i));
1492 }
1493 return OK;
1494
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001495 case VAR_LIST:
1496 if (*op != '+' || tv2->v_type != VAR_LIST)
1497 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001498 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001499 if (tv2->vval.v_list != NULL)
1500 {
1501 if (tv1->vval.v_list == NULL)
1502 {
1503 tv1->vval.v_list = tv2->vval.v_list;
1504 ++tv1->vval.v_list->lv_refcount;
1505 }
1506 else
1507 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1508 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001509 return OK;
1510
1511 case VAR_NUMBER:
1512 case VAR_STRING:
1513 if (tv2->v_type == VAR_LIST)
1514 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001515 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001516 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001517 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001518 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001519#ifdef FEAT_FLOAT
1520 if (tv2->v_type == VAR_FLOAT)
1521 {
1522 float_T f = n;
1523
Bram Moolenaarff697e62019-02-12 22:28:33 +01001524 if (*op == '%')
1525 break;
1526 switch (*op)
1527 {
1528 case '+': f += tv2->vval.v_float; break;
1529 case '-': f -= tv2->vval.v_float; break;
1530 case '*': f *= tv2->vval.v_float; break;
1531 case '/': f /= tv2->vval.v_float; break;
1532 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001533 clear_tv(tv1);
1534 tv1->v_type = VAR_FLOAT;
1535 tv1->vval.v_float = f;
1536 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001537 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001538#endif
1539 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001540 switch (*op)
1541 {
1542 case '+': n += tv_get_number(tv2); break;
1543 case '-': n -= tv_get_number(tv2); break;
1544 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001545 case '/': n = num_divide(n, tv_get_number(tv2),
1546 &failed); break;
1547 case '%': n = num_modulus(n, tv_get_number(tv2),
1548 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001549 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001550 clear_tv(tv1);
1551 tv1->v_type = VAR_NUMBER;
1552 tv1->vval.v_number = n;
1553 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001554 }
1555 else
1556 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001557 if (tv2->v_type == VAR_FLOAT)
1558 break;
1559
Bram Moolenaarff697e62019-02-12 22:28:33 +01001560 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001561 s = tv_get_string(tv1);
1562 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001563 clear_tv(tv1);
1564 tv1->v_type = VAR_STRING;
1565 tv1->vval.v_string = s;
1566 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001567 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001568
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001569 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001570#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001571 {
1572 float_T f;
1573
Bram Moolenaarff697e62019-02-12 22:28:33 +01001574 if (*op == '%' || *op == '.'
1575 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001576 && tv2->v_type != VAR_NUMBER
1577 && tv2->v_type != VAR_STRING))
1578 break;
1579 if (tv2->v_type == VAR_FLOAT)
1580 f = tv2->vval.v_float;
1581 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001582 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001583 switch (*op)
1584 {
1585 case '+': tv1->vval.v_float += f; break;
1586 case '-': tv1->vval.v_float -= f; break;
1587 case '*': tv1->vval.v_float *= f; break;
1588 case '/': tv1->vval.v_float /= f; break;
1589 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001590 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001591#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001592 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001593 }
1594 }
1595
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001596 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001597 return FAIL;
1598}
1599
1600/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001601 * Evaluate the expression used in a ":for var in expr" command.
1602 * "arg" points to "var".
1603 * Set "*errp" to TRUE for an error, FALSE otherwise;
1604 * Return a pointer that holds the info. Null when there is an error.
1605 */
1606 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001607eval_for_line(
1608 char_u *arg,
1609 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001610 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001611 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001612{
Bram Moolenaar33570922005-01-25 22:26:29 +00001613 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001614 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001615 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001616 typval_T tv;
1617 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001618 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001619
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001620 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001621
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001622 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001623 if (fi == NULL)
1624 return NULL;
1625
Bram Moolenaar404557e2021-07-05 21:41:48 +02001626 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1627 &fi->fi_semicolon, FALSE);
1628 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001629 return fi;
1630
Bram Moolenaar404557e2021-07-05 21:41:48 +02001631 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001632 if (expr[0] != 'i' || expr[1] != 'n'
1633 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001634 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001635 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1636 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1637 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001638 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001639 return fi;
1640 }
1641
1642 if (skip)
1643 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001644 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1645 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001646 {
1647 *errp = FALSE;
1648 if (!skip)
1649 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001650 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001651 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001652 l = tv.vval.v_list;
1653 if (l == NULL)
1654 {
1655 // a null list is like an empty list: do nothing
1656 clear_tv(&tv);
1657 }
1658 else
1659 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001660 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001661 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001662
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001663 // No need to increment the refcount, it's already set for
1664 // the list being used in "tv".
1665 fi->fi_list = l;
1666 list_add_watch(l, &fi->fi_lw);
1667 fi->fi_lw.lw_item = l->lv_first;
1668 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001669 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001670 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001671 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001672 fi->fi_bi = 0;
1673 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001674 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001675 typval_T btv;
1676
1677 // Make a copy, so that the iteration still works when the
1678 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001679 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001680 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001681 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001682 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001683 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001684 else if (tv.v_type == VAR_STRING)
1685 {
1686 fi->fi_byte_idx = 0;
1687 fi->fi_string = tv.vval.v_string;
1688 tv.vval.v_string = NULL;
1689 if (fi->fi_string == NULL)
1690 fi->fi_string = vim_strsave((char_u *)"");
1691 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001692 else
1693 {
Sean Dewar80d73952021-08-04 19:25:54 +02001694 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001695 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001696 }
1697 }
1698 }
1699 if (skip)
1700 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001701 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001702
1703 return fi;
1704}
1705
1706/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001707 * Used when looping over a :for line, skip the "in expr" part.
1708 */
1709 void
1710skip_for_lines(void *fi_void, evalarg_T *evalarg)
1711{
1712 forinfo_T *fi = (forinfo_T *)fi_void;
1713 int i;
1714
1715 for (i = 0; i < fi->fi_break_count; ++i)
1716 eval_next_line(evalarg);
1717}
1718
1719/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001720 * Use the first item in a ":for" list. Advance to the next.
1721 * Assign the values to the variable (list). "arg" points to the first one.
1722 * Return TRUE when a valid item was found, FALSE when at end of list or
1723 * something wrong.
1724 */
1725 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001726next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001727{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001728 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001729 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001730 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001731 ? (ASSIGN_FINAL
1732 // first round: error if variable exists
1733 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
1734 | ASSIGN_NO_MEMBER_TYPE)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001735 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001736 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001737 int skip_assign = in_vim9script() && arg[0] == '_'
1738 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001739
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001740 if (fi->fi_blob != NULL)
1741 {
1742 typval_T tv;
1743
1744 if (fi->fi_bi >= blob_len(fi->fi_blob))
1745 return FALSE;
1746 tv.v_type = VAR_NUMBER;
1747 tv.v_lock = VAR_FIXED;
1748 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1749 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001750 if (skip_assign)
1751 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001752 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001753 fi->fi_varcount, flag, NULL) == OK;
1754 }
1755
1756 if (fi->fi_string != NULL)
1757 {
1758 typval_T tv;
1759 int len;
1760
1761 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1762 if (len == 0)
1763 return FALSE;
1764 tv.v_type = VAR_STRING;
1765 tv.v_lock = VAR_FIXED;
1766 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1767 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001768 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001769 if (skip_assign)
1770 result = TRUE;
1771 else
1772 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001773 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001774 vim_free(tv.vval.v_string);
1775 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001776 }
1777
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001778 item = fi->fi_lw.lw_item;
1779 if (item == NULL)
1780 result = FALSE;
1781 else
1782 {
1783 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001784 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001785 if (skip_assign)
1786 result = TRUE;
1787 else
1788 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001789 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001790 }
1791 return result;
1792}
1793
1794/*
1795 * Free the structure used to store info used by ":for".
1796 */
1797 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001798free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001799{
Bram Moolenaar33570922005-01-25 22:26:29 +00001800 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001801
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001802 if (fi == NULL)
1803 return;
1804 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001805 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001806 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001807 list_unref(fi->fi_list);
1808 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001809 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001810 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001811 else
1812 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001813 vim_free(fi);
1814}
1815
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001817set_context_for_expression(
1818 expand_T *xp,
1819 char_u *arg,
1820 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001822 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001824 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001826 if (cmdidx == CMD_let || cmdidx == CMD_var
1827 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001828 {
1829 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001830 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001831 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001832 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001833 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001834 {
1835 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001836 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001837 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001838 break;
1839 }
1840 return;
1841 }
1842 }
1843 else
1844 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1845 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 while ((xp->xp_pattern = vim_strpbrk(arg,
1847 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1848 {
1849 c = *xp->xp_pattern;
1850 if (c == '&')
1851 {
1852 c = xp->xp_pattern[1];
1853 if (c == '&')
1854 {
1855 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001856 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 }
1858 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001859 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001861 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1862 xp->xp_pattern += 2;
1863
1864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 }
1866 else if (c == '$')
1867 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001868 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 xp->xp_context = EXPAND_ENV_VARS;
1870 }
1871 else if (c == '=')
1872 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001873 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 xp->xp_context = EXPAND_EXPRESSION;
1875 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001876 else if (c == '#'
1877 && xp->xp_context == EXPAND_EXPRESSION)
1878 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001879 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001880 break;
1881 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001882 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 && xp->xp_context == EXPAND_FUNCTIONS
1884 && vim_strchr(xp->xp_pattern, '(') == NULL)
1885 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001886 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 break;
1888 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001889 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001891 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 {
1893 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1894 if (c == '\\' && xp->xp_pattern[1] != NUL)
1895 ++xp->xp_pattern;
1896 xp->xp_context = EXPAND_NOTHING;
1897 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001898 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001900 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1902 /* skip */ ;
1903 xp->xp_context = EXPAND_NOTHING;
1904 }
1905 else if (c == '|')
1906 {
1907 if (xp->xp_pattern[1] == '|')
1908 {
1909 ++xp->xp_pattern;
1910 xp->xp_context = EXPAND_EXPRESSION;
1911 }
1912 else
1913 xp->xp_context = EXPAND_COMMANDS;
1914 }
1915 else
1916 xp->xp_context = EXPAND_EXPRESSION;
1917 }
1918 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001919 // Doesn't look like something valid, expand as an expression
1920 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001921 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 arg = xp->xp_pattern;
1923 if (*arg != NUL)
1924 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1925 /* skip */ ;
1926 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01001927
1928 // ":exe one two" completes "two"
1929 if ((cmdidx == CMD_execute
1930 || cmdidx == CMD_echo
1931 || cmdidx == CMD_echon
1932 || cmdidx == CMD_echomsg)
1933 && xp->xp_context == EXPAND_EXPRESSION)
1934 {
1935 for (;;)
1936 {
1937 char_u *n = skiptowhite(arg);
1938
1939 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
1940 break;
1941 arg = skipwhite(n);
1942 }
1943 }
1944
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 xp->xp_pattern = arg;
1946}
1947
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001949 * Return TRUE if "pat" matches "text".
1950 * Does not use 'cpo' and always uses 'magic'.
1951 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001952 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001953pattern_match(char_u *pat, char_u *text, int ic)
1954{
1955 int matches = FALSE;
1956 char_u *save_cpo;
1957 regmatch_T regmatch;
1958
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001959 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001960 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01001961 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001962 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1963 if (regmatch.regprog != NULL)
1964 {
1965 regmatch.rm_ic = ic;
1966 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1967 vim_regfree(regmatch.regprog);
1968 }
1969 p_cpo = save_cpo;
1970 return matches;
1971}
1972
1973/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001974 * Handle a name followed by "(". Both for just "name(arg)" and for
1975 * "expr->name(arg)".
1976 * Returns OK or FAIL.
1977 */
1978 static int
1979eval_func(
1980 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02001981 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001982 char_u *name,
1983 int name_len,
1984 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001985 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001986 typval_T *basetv) // "expr" for "expr->name(arg)"
1987{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001988 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001989 char_u *s = name;
1990 int len = name_len;
1991 partial_T *partial;
1992 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001993 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001994 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001995
1996 if (!evaluate)
1997 check_vars(s, len);
1998
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001999 // If "s" is the name of a variable of type VAR_FUNC
2000 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002001 s = deref_func_name(s, &len, &partial,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002002 in_vim9script() ? &type : NULL, !evaluate, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002003
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002004 // Need to make a copy, in case evaluating the arguments makes
2005 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002006 s = vim_strsave(s);
Bram Moolenaarb3341372021-12-14 18:57:45 +00002007 if (s == NULL || (evaluate && (*s == NUL || (flags & EVAL_CONSTANT))))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002008 ret = FAIL;
2009 else
2010 {
2011 funcexe_T funcexe;
2012
2013 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002014 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002015 funcexe.fe_firstline = curwin->w_cursor.lnum;
2016 funcexe.fe_lastline = curwin->w_cursor.lnum;
2017 funcexe.fe_evaluate = evaluate;
2018 funcexe.fe_partial = partial;
2019 funcexe.fe_basetv = basetv;
2020 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002021 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002022 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002023 }
2024 vim_free(s);
2025
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002026 // If evaluate is FALSE rettv->v_type was not set in
2027 // get_func_tv, but it's needed in handle_subscript() to parse
2028 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002029 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2030 {
2031 rettv->vval.v_string = NULL;
2032 rettv->v_type = VAR_FUNC;
2033 }
2034
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002035 // Stop the expression evaluation when immediately
2036 // aborting on error, or when an interrupt occurred or
2037 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002038 if (evaluate && aborting())
2039 {
2040 if (ret == OK)
2041 clear_tv(rettv);
2042 ret = FAIL;
2043 }
2044 return ret;
2045}
2046
2047/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002048 * Get the next line source line without advancing. But do skip over comment
2049 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002050 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002051 */
2052 static char_u *
2053getline_peek_skip_comments(evalarg_T *evalarg)
2054{
2055 for (;;)
2056 {
2057 char_u *next = getline_peek(evalarg->eval_getline,
2058 evalarg->eval_cookie);
2059 char_u *p;
2060
2061 if (next == NULL)
2062 break;
2063 p = skipwhite(next);
2064 if (*p != NUL && !vim9_comment_start(p))
2065 return next;
2066 (void)eval_next_line(evalarg);
2067 }
2068 return NULL;
2069}
2070
2071/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002072 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2073 * comment) and there is a next line, return the next line (skipping blanks)
2074 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002075 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2076 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002077 * "arg" must point somewhere inside a line, not at the start.
2078 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002079 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002080eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2081{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002082 char_u *p = skipwhite(arg);
2083
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002084 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002085 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002086 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002087 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002088 && (*p == NUL || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002089 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002090 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002091
2092 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002093 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002094 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002095 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002096
Bram Moolenaar9d489562020-07-30 20:08:50 +02002097 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002098 {
2099 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002100 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002101 }
2102 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002103 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002104}
2105
2106/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002107 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002108 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002109 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002110 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002111eval_next_line(evalarg_T *evalarg)
2112{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002113 garray_T *gap = &evalarg->eval_ga;
2114 char_u *line;
2115
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002116 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002117 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2118 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002119 else
2120 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002121 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002122 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2123 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002124 char_u *p = skipwhite(line);
2125
2126 // Going to concatenate the lines after parsing. For an empty or
2127 // comment line use an empty string.
2128 if (*p == NUL || vim9_comment_start(p))
2129 {
2130 vim_free(line);
2131 line = vim_strsave((char_u *)"");
2132 }
2133
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002134 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2135 ++gap->ga_len;
2136 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002137 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002138 {
2139 vim_free(evalarg->eval_tofree);
2140 evalarg->eval_tofree = line;
2141 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002142
2143 // Advanced to the next line, "arg" no longer points into the previous
2144 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002145 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002146 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002147}
2148
Bram Moolenaare6b53242020-07-01 17:28:33 +02002149/*
2150 * Call eval_next_non_blank() and get the next line if needed.
2151 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002152 char_u *
2153skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2154{
2155 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002156 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002157
Bram Moolenaar962d7212020-07-04 14:15:00 +02002158 if (evalarg == NULL)
2159 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002160 eval_next_non_blank(p, evalarg, &getnext);
2161 if (getnext)
2162 return eval_next_line(evalarg);
2163 return p;
2164}
2165
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002166/*
Bram Moolenaar844fb642021-10-23 13:32:30 +01002167 * Initialize "evalarg" for use.
2168 */
2169 void
2170init_evalarg(evalarg_T *evalarg)
2171{
2172 CLEAR_POINTER(evalarg);
2173 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
2174}
2175
2176/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002177 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002178 */
2179 void
2180clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2181{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002182 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002183 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002184 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002185 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002186 if (eap != NULL)
2187 {
2188 // We may need to keep the original command line, e.g. for
2189 // ":let" it has the variable names. But we may also need the
2190 // new one, "nextcmd" points into it. Keep both.
2191 vim_free(eap->cmdline_tofree);
2192 eap->cmdline_tofree = *eap->cmdlinep;
2193 *eap->cmdlinep = evalarg->eval_tofree;
2194 }
2195 else
2196 vim_free(evalarg->eval_tofree);
2197 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002198 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002199
Bram Moolenaar844fb642021-10-23 13:32:30 +01002200 ga_clear_strings(&evalarg->eval_tofree_ga);
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002201 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002202 }
2203}
2204
2205/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002207 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2209 */
2210
2211/*
2212 * Handle zero level expression.
2213 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002215 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002216 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 * Return OK or FAIL.
2218 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002219 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002220eval0(
2221 char_u *arg,
2222 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002223 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002224 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002226 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2227}
2228
2229/*
2230 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2231 * expression and don't check what comes after the expression.
2232 */
2233 int
2234eval0_retarg(
2235 char_u *arg,
2236 typval_T *rettv,
2237 exarg_T *eap,
2238 evalarg_T *evalarg,
2239 char_u **retarg)
2240{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 int ret;
2242 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002243 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002244 int did_emsg_before = did_emsg;
2245 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002246 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002247 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002248 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249
2250 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002251 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002252 expr_end = p;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002253 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002254
Bram Moolenaar02929a32021-12-17 14:46:12 +00002255 // In Vim9 script a command block is not split at NL characters for
2256 // commands using an expression argument. Skip over a '#' comment to check
2257 // for a following NL. Require white space before the '#'.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002258 if (in_vim9script() && p > expr_end && retarg == NULL)
Bram Moolenaar02929a32021-12-17 14:46:12 +00002259 while (*p == '#')
2260 {
2261 char_u *nl = vim_strchr(p, NL);
2262
2263 if (nl == NULL)
2264 break;
2265 p = skipwhite(nl + 1);
2266 if (eap != NULL && *p != NUL)
2267 eap->nextcmd = p;
2268 check_for_end = FALSE;
2269 }
2270
2271 if (ret != FAIL && check_for_end)
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002272 end_error = !ends_excmd2(arg, p);
2273 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 {
2275 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002276 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 /*
2278 * Report the invalid expression unless the expression evaluation has
2279 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002280 * exception, or we already gave a more specific error.
2281 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002283 if (!aborting()
2284 && did_emsg == did_emsg_before
2285 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002286 && (flags & EVAL_CONSTANT) == 0
2287 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002288 {
2289 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002290 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002291 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002292 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002293 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002294
2295 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002296 // a next command to avoid more errors, unless "|" is following, which
2297 // could only be a command separator.
2298 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2299 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002300 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002302
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002303 if (retarg != NULL)
2304 *retarg = p;
2305 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002306 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002307
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 return ret;
2309}
2310
2311/*
2312 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002313 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002314 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 *
2316 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002317 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002319 * Note: "rettv.v_lock" is not set.
2320 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 * Return OK or FAIL.
2322 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002323 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002324eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002326 char_u *p;
2327 int getnext;
2328
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002329 CLEAR_POINTER(rettv);
2330
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 /*
2332 * Get the first variable.
2333 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002334 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 return FAIL;
2336
Bram Moolenaar793648f2020-06-26 21:28:25 +02002337 p = eval_next_non_blank(*arg, evalarg, &getnext);
2338 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002340 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002341 int result;
2342 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002343 evalarg_T *evalarg_used = evalarg;
2344 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002345 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002346 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002347 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002348
2349 if (evalarg == NULL)
2350 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002351 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002352 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002353 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002354 orig_flags = evalarg_used->eval_flags;
2355 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002356
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002357 if (getnext)
2358 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002359 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002360 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002361 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002362 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002363 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002364 clear_tv(rettv);
2365 return FAIL;
2366 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002367 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002368 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002369
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002371 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002373 int error = FALSE;
2374
Bram Moolenaar13106602020-10-04 16:06:05 +02002375 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002376 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002377 else if (vim9script)
2378 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002379 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002381 if (error || !op_falsy || !result)
2382 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002383 if (error)
2384 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 }
2386
2387 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002388 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002390 if (op_falsy)
2391 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002392 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002393 {
mityu4ac198c2021-05-28 17:52:40 +02002394 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002395 clear_tv(rettv);
2396 return FAIL;
2397 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002398 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002399 evalarg_used->eval_flags = (op_falsy ? !result : result)
2400 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2401 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002402 {
2403 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002405 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002406 if (!op_falsy || !result)
2407 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002409 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002411 /*
2412 * Check for the ":".
2413 */
2414 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2415 if (*p != ':')
2416 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002417 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002418 if (evaluate && result)
2419 clear_tv(rettv);
2420 evalarg_used->eval_flags = orig_flags;
2421 return FAIL;
2422 }
2423 if (getnext)
2424 *arg = eval_next_line(evalarg_used);
2425 else
2426 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002427 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002428 {
2429 error_white_both(p, 1);
2430 clear_tv(rettv);
2431 evalarg_used->eval_flags = orig_flags;
2432 return FAIL;
2433 }
2434 *arg = p;
2435 }
2436
2437 /*
2438 * Get the third variable. Recursive!
2439 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002440 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002441 {
mityu4ac198c2021-05-28 17:52:40 +02002442 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002443 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002444 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002445 return FAIL;
2446 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002447 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2448 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002449 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002450 if (eval1(arg, &var2, evalarg_used) == FAIL)
2451 {
2452 if (evaluate && result)
2453 clear_tv(rettv);
2454 evalarg_used->eval_flags = orig_flags;
2455 return FAIL;
2456 }
2457 if (evaluate && !result)
2458 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002460
2461 if (evalarg == NULL)
2462 clear_evalarg(&local_evalarg, NULL);
2463 else
2464 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 }
2466
2467 return OK;
2468}
2469
2470/*
2471 * Handle first level expression:
2472 * expr2 || expr2 || expr2 logical OR
2473 *
2474 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002475 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 *
2477 * Return OK or FAIL.
2478 */
2479 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002480eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002482 char_u *p;
2483 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484
2485 /*
2486 * Get the first variable.
2487 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002488 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 return FAIL;
2490
2491 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002492 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002494 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002495 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002497 evalarg_T *evalarg_used = evalarg;
2498 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002499 int evaluate;
2500 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002501 long result = FALSE;
2502 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002503 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002504 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002505
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002506 if (evalarg == NULL)
2507 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002508 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002509 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002510 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002511 orig_flags = evalarg_used->eval_flags;
2512 evaluate = orig_flags & EVAL_EVALUATE;
2513 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002514 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002515 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002516 result = tv_get_bool_chk(rettv, &error);
2517 else if (tv_get_number_chk(rettv, &error) != 0)
2518 result = TRUE;
2519 clear_tv(rettv);
2520 if (error)
2521 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 }
2523
2524 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002525 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002527 while (p[0] == '|' && p[1] == '|')
2528 {
2529 if (getnext)
2530 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002531 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002532 {
2533 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2534 {
2535 error_white_both(p, 2);
2536 clear_tv(rettv);
2537 return FAIL;
2538 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002539 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002540 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002541
2542 /*
2543 * Get the second variable.
2544 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002545 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2546 {
mityu4ac198c2021-05-28 17:52:40 +02002547 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002548 clear_tv(rettv);
2549 return FAIL;
2550 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002551 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2552 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002553 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002554 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002555 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002556
2557 /*
2558 * Compute the result.
2559 */
2560 if (evaluate && !result)
2561 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002562 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002563 result = tv_get_bool_chk(&var2, &error);
2564 else if (tv_get_number_chk(&var2, &error) != 0)
2565 result = TRUE;
2566 clear_tv(&var2);
2567 if (error)
2568 return FAIL;
2569 }
2570 if (evaluate)
2571 {
2572 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002573 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002574 rettv->v_type = VAR_BOOL;
2575 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002576 }
2577 else
2578 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002579 rettv->v_type = VAR_NUMBER;
2580 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002581 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002582 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002583
2584 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002586
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002587 if (evalarg == NULL)
2588 clear_evalarg(&local_evalarg, NULL);
2589 else
2590 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 }
2592
2593 return OK;
2594}
2595
2596/*
2597 * Handle second level expression:
2598 * expr3 && expr3 && expr3 logical AND
2599 *
2600 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002601 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 *
2603 * Return OK or FAIL.
2604 */
2605 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002606eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002608 char_u *p;
2609 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610
2611 /*
2612 * Get the first variable.
2613 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002614 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 return FAIL;
2616
2617 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002618 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002620 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002621 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002623 evalarg_T *evalarg_used = evalarg;
2624 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002625 int orig_flags;
2626 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002627 long result = TRUE;
2628 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002629 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002630 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002631
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002632 if (evalarg == NULL)
2633 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002634 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002635 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002636 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002637 orig_flags = evalarg_used->eval_flags;
2638 evaluate = orig_flags & EVAL_EVALUATE;
2639 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002640 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002641 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002642 result = tv_get_bool_chk(rettv, &error);
2643 else if (tv_get_number_chk(rettv, &error) == 0)
2644 result = FALSE;
2645 clear_tv(rettv);
2646 if (error)
2647 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 }
2649
2650 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002651 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002653 while (p[0] == '&' && p[1] == '&')
2654 {
2655 if (getnext)
2656 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002657 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002658 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002659 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002660 {
2661 error_white_both(p, 2);
2662 clear_tv(rettv);
2663 return FAIL;
2664 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002665 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002666 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002667
2668 /*
2669 * Get the second variable.
2670 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002671 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2672 {
mityu4ac198c2021-05-28 17:52:40 +02002673 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002674 clear_tv(rettv);
2675 return FAIL;
2676 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002677 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2678 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002679 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002680 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002681 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002682 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002683
2684 /*
2685 * Compute the result.
2686 */
2687 if (evaluate && result)
2688 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002689 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002690 result = tv_get_bool_chk(&var2, &error);
2691 else if (tv_get_number_chk(&var2, &error) == 0)
2692 result = FALSE;
2693 clear_tv(&var2);
2694 if (error)
2695 return FAIL;
2696 }
2697 if (evaluate)
2698 {
2699 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002700 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002701 rettv->v_type = VAR_BOOL;
2702 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002703 }
2704 else
2705 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002706 rettv->v_type = VAR_NUMBER;
2707 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002708 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002709 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002710
2711 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002713
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002714 if (evalarg == NULL)
2715 clear_evalarg(&local_evalarg, NULL);
2716 else
2717 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 }
2719
2720 return OK;
2721}
2722
2723/*
2724 * Handle third level expression:
2725 * var1 == var2
2726 * var1 =~ var2
2727 * var1 != var2
2728 * var1 !~ var2
2729 * var1 > var2
2730 * var1 >= var2
2731 * var1 < var2
2732 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002733 * var1 is var2
2734 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 *
2736 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002737 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 *
2739 * Return OK or FAIL.
2740 */
2741 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002742eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002745 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002746 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002748 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749
2750 /*
2751 * Get the first variable.
2752 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002753 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 return FAIL;
2755
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002756 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002757 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758
2759 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002760 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002762 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002764 typval_T var2;
2765 int ic;
2766 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002767 int evaluate = evalarg == NULL
2768 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002769
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002770 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002771 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002772 *arg = eval_next_line(evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002773 p = *arg;
2774 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002775 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2776 {
mityu4ac198c2021-05-28 17:52:40 +02002777 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002778 clear_tv(rettv);
2779 return FAIL;
2780 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002781
Bram Moolenaar696ba232020-07-29 21:20:41 +02002782 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2783 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002784 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002785 clear_tv(rettv);
2786 return FAIL;
2787 }
2788
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002789 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 if (p[len] == '?')
2791 {
2792 ic = TRUE;
2793 ++len;
2794 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002795 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 else if (p[len] == '#')
2797 {
2798 ic = FALSE;
2799 ++len;
2800 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002801 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002803 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804
2805 /*
2806 * Get the second variable.
2807 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002808 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2809 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002810 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002811 clear_tv(rettv);
2812 return FAIL;
2813 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002814 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002815 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002817 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 return FAIL;
2819 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002820 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002821 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002822 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002823
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002824 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002825 {
2826 ret = FAIL;
2827 clear_tv(rettv);
2828 }
2829 else
2830 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002831 clear_tv(&var2);
2832 return ret;
2833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 }
2835
2836 return OK;
2837}
2838
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002839/*
2840 * Make a copy of blob "tv1" and append blob "tv2".
2841 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002842 void
2843eval_addblob(typval_T *tv1, typval_T *tv2)
2844{
2845 blob_T *b1 = tv1->vval.v_blob;
2846 blob_T *b2 = tv2->vval.v_blob;
2847 blob_T *b = blob_alloc();
2848 int i;
2849
2850 if (b != NULL)
2851 {
2852 for (i = 0; i < blob_len(b1); i++)
2853 ga_append(&b->bv_ga, blob_get(b1, i));
2854 for (i = 0; i < blob_len(b2); i++)
2855 ga_append(&b->bv_ga, blob_get(b2, i));
2856
2857 clear_tv(tv1);
2858 rettv_blob_set(tv1, b);
2859 }
2860}
2861
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002862/*
2863 * Make a copy of list "tv1" and append list "tv2".
2864 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002865 int
2866eval_addlist(typval_T *tv1, typval_T *tv2)
2867{
2868 typval_T var3;
2869
2870 // concatenate Lists
2871 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2872 {
2873 clear_tv(tv1);
2874 clear_tv(tv2);
2875 return FAIL;
2876 }
2877 clear_tv(tv1);
2878 *tv1 = var3;
2879 return OK;
2880}
2881
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882/*
2883 * Handle fourth level expression:
2884 * + number addition
2885 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002886 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002887 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 *
2889 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002890 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 *
2892 * Return OK or FAIL.
2893 */
2894 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002895eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 /*
2898 * Get the first variable.
2899 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002900 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 return FAIL;
2902
2903 /*
2904 * Repeat computing, until no '+', '-' or '.' is following.
2905 */
2906 for (;;)
2907 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002908 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002909 int getnext;
2910 char_u *p;
2911 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002912 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002913 int concat;
2914 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002915 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002916
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002917 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002918 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002919 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002920 p = eval_next_non_blank(*arg, evalarg, &getnext);
2921 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02002922 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002923 if ((op != '+' && op != '-' && !concat) || p[1] == '='
2924 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002926 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
2927 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002928
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002929 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002930 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002931 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002932 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002933 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002934 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002935 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002936 {
mityu4ac198c2021-05-28 17:52:40 +02002937 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002938 clear_tv(rettv);
2939 return FAIL;
2940 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002941 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002942 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002943 if ((op != '+' || (rettv->v_type != VAR_LIST
2944 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002945#ifdef FEAT_FLOAT
2946 && (op == '.' || rettv->v_type != VAR_FLOAT)
2947#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002948 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002949 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002950 int error = FALSE;
2951
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002952 // For "list + ...", an illegal use of the first operand as
2953 // a number cannot be determined before evaluating the 2nd
2954 // operand: if this is also a list, all is ok.
2955 // For "something . ...", "something - ..." or "non-list + ...",
2956 // we know that the first operand needs to be a string or number
2957 // without evaluating the 2nd operand. So check before to avoid
2958 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002959 if (op != '.')
2960 tv_get_number_chk(rettv, &error);
2961 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002962 {
2963 clear_tv(rettv);
2964 return FAIL;
2965 }
2966 }
2967
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 /*
2969 * Get the second variable.
2970 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002971 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002972 {
mityu89dcb4d2021-05-28 13:50:17 +02002973 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002974 clear_tv(rettv);
2975 return FAIL;
2976 }
2977 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002978 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002980 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 return FAIL;
2982 }
2983
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002984 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 {
2986 /*
2987 * Compute the result.
2988 */
2989 if (op == '.')
2990 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002991 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2992 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002993 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002994
Bram Moolenaar2e086612020-08-25 22:37:48 +02002995 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002996 || var2.v_type == VAR_CHANNEL
2997 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02002998 semsg(_(e_using_invalid_value_as_string_str),
2999 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003000#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02003001 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003002 {
3003 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3004 var2.vval.v_float);
3005 s2 = buf2;
3006 }
3007#endif
3008 else
3009 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003010 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003011 {
3012 clear_tv(rettv);
3013 clear_tv(&var2);
3014 return FAIL;
3015 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003016 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003017 clear_tv(rettv);
3018 rettv->v_type = VAR_STRING;
3019 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003021 else if (op == '+' && rettv->v_type == VAR_BLOB
3022 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003023 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003024 else if (op == '+' && rettv->v_type == VAR_LIST
3025 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003026 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003027 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003028 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 else
3031 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003032 int error = FALSE;
3033 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003034#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003035 float_T f1 = 0, f2 = 0;
3036
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003037 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003038 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003039 f1 = rettv->vval.v_float;
3040 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003041 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003042 else
3043#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003044 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003045 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003046 if (error)
3047 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003048 // This can only happen for "list + non-list" or
3049 // "blob + non-blob". For "non-list + ..." or
3050 // "something - ...", we returned before evaluating the
3051 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003052 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003053 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003054 return FAIL;
3055 }
3056#ifdef FEAT_FLOAT
3057 if (var2.v_type == VAR_FLOAT)
3058 f1 = n1;
3059#endif
3060 }
3061#ifdef FEAT_FLOAT
3062 if (var2.v_type == VAR_FLOAT)
3063 {
3064 f2 = var2.vval.v_float;
3065 n2 = 0;
3066 }
3067 else
3068#endif
3069 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003070 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003071 if (error)
3072 {
3073 clear_tv(rettv);
3074 clear_tv(&var2);
3075 return FAIL;
3076 }
3077#ifdef FEAT_FLOAT
3078 if (rettv->v_type == VAR_FLOAT)
3079 f2 = n2;
3080#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003081 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003082 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003083
3084#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003085 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003086 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3087 {
3088 if (op == '+')
3089 f1 = f1 + f2;
3090 else
3091 f1 = f1 - f2;
3092 rettv->v_type = VAR_FLOAT;
3093 rettv->vval.v_float = f1;
3094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003096#endif
3097 {
3098 if (op == '+')
3099 n1 = n1 + n2;
3100 else
3101 n1 = n1 - n2;
3102 rettv->v_type = VAR_NUMBER;
3103 rettv->vval.v_number = n1;
3104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003106 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 }
3108 }
3109 return OK;
3110}
3111
3112/*
3113 * Handle fifth level expression:
3114 * * number multiplication
3115 * / number division
3116 * % number modulo
3117 *
3118 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003119 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 *
3121 * Return OK or FAIL.
3122 */
3123 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003124eval6(
3125 char_u **arg,
3126 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003127 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003128 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003130#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003131 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003132#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133
3134 /*
3135 * Get the first variable.
3136 */
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003137 if (eval7t(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 return FAIL;
3139
3140 /*
3141 * Repeat computing, until no '*', '/' or '%' is following.
3142 */
3143 for (;;)
3144 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003145 int evaluate;
3146 int getnext;
3147 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003148 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003149 int op;
3150 varnumber_T n1, n2;
3151#ifdef FEAT_FLOAT
3152 float_T f1, f2;
3153#endif
3154 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003155
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003156 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003157 p = eval_next_non_blank(*arg, evalarg, &getnext);
3158 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003159 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003161
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003162 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003163 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003164 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003165 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003166 {
3167 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3168 {
mityu4ac198c2021-05-28 17:52:40 +02003169 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003170 clear_tv(rettv);
3171 return FAIL;
3172 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003173 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003176#ifdef FEAT_FLOAT
3177 f1 = 0;
3178 f2 = 0;
3179#endif
3180 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003181 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003183#ifdef FEAT_FLOAT
3184 if (rettv->v_type == VAR_FLOAT)
3185 {
3186 f1 = rettv->vval.v_float;
3187 use_float = TRUE;
3188 n1 = 0;
3189 }
3190 else
3191#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003192 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003193 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003194 if (error)
3195 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 }
3197 else
3198 n1 = 0;
3199
3200 /*
3201 * Get the second variable.
3202 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003203 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3204 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003205 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003206 clear_tv(rettv);
3207 return FAIL;
3208 }
3209 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003210 if (eval7t(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 return FAIL;
3212
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003213 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003215#ifdef FEAT_FLOAT
3216 if (var2.v_type == VAR_FLOAT)
3217 {
3218 if (!use_float)
3219 {
3220 f1 = n1;
3221 use_float = TRUE;
3222 }
3223 f2 = var2.vval.v_float;
3224 n2 = 0;
3225 }
3226 else
3227#endif
3228 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003229 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003230 clear_tv(&var2);
3231 if (error)
3232 return FAIL;
3233#ifdef FEAT_FLOAT
3234 if (use_float)
3235 f2 = n2;
3236#endif
3237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238
3239 /*
3240 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003241 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003243#ifdef FEAT_FLOAT
3244 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003246 if (op == '*')
3247 f1 = f1 * f2;
3248 else if (op == '/')
3249 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003250# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003251 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003252 if (f2 == 0.0)
3253 {
3254 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003255 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003256 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003257 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003258 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003259 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003260 }
3261 else
3262 f1 = f1 / f2;
3263# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003264 // We rely on the floating point library to handle divide
3265 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003266 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003267# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003270 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003271 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003272 return FAIL;
3273 }
3274 rettv->v_type = VAR_FLOAT;
3275 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 }
3277 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003280 int failed = FALSE;
3281
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003282 if (op == '*')
3283 n1 = n1 * n2;
3284 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003285 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003287 n1 = num_modulus(n1, n2, &failed);
3288 if (failed)
3289 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003290
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003291 rettv->v_type = VAR_NUMBER;
3292 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 }
3295 }
3296
3297 return OK;
3298}
3299
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003300/*
3301 * Handle a type cast before a base level expression.
3302 * "arg" must point to the first non-white of the expression.
3303 * "arg" is advanced to just after the recognized expression.
3304 * Return OK or FAIL.
3305 */
3306 static int
3307eval7t(
3308 char_u **arg,
3309 typval_T *rettv,
3310 evalarg_T *evalarg,
3311 int want_string) // after "." operator
3312{
3313 type_T *want_type = NULL;
3314 garray_T type_list; // list of pointers to allocated types
3315 int res;
3316 int evaluate = evalarg == NULL ? 0
3317 : (evalarg->eval_flags & EVAL_EVALUATE);
3318
3319 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003320 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3321 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003322 {
3323 ++*arg;
3324 ga_init2(&type_list, sizeof(type_T *), 10);
3325 want_type = parse_type(arg, &type_list, TRUE);
3326 if (want_type == NULL && (evaluate || **arg != '>'))
3327 {
3328 clear_type_list(&type_list);
3329 return FAIL;
3330 }
3331
3332 if (**arg != '>')
3333 {
3334 if (*skipwhite(*arg) == '>')
3335 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3336 else
3337 emsg(_(e_missing_gt));
3338 clear_type_list(&type_list);
3339 return FAIL;
3340 }
3341 ++*arg;
3342 *arg = skipwhite_and_linebreak(*arg, evalarg);
3343 }
3344
3345 res = eval7(arg, rettv, evalarg, want_string);
3346
3347 if (want_type != NULL && evaluate)
3348 {
3349 if (res == OK)
3350 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003351 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3352 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003353
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003354 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003355 {
3356 if (want_type == &t_bool && actual != &t_bool
3357 && (actual->tt_flags & TTFLAG_BOOL_OK))
3358 {
3359 int n = tv2bool(rettv);
3360
3361 // can use "0" and "1" for boolean in some places
3362 clear_tv(rettv);
3363 rettv->v_type = VAR_BOOL;
3364 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3365 }
3366 else
3367 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003368 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003369
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003370 where.wt_variable = TRUE;
3371 res = check_type(want_type, actual, TRUE, where);
3372 }
3373 }
3374 }
3375 clear_type_list(&type_list);
3376 }
3377
3378 return res;
3379}
3380
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003381 int
3382eval_leader(char_u **arg, int vim9)
3383{
3384 char_u *s = *arg;
3385 char_u *p = *arg;
3386
3387 while (*p == '!' || *p == '-' || *p == '+')
3388 {
3389 char_u *n = skipwhite(p + 1);
3390
3391 // ++, --, -+ and +- are not accepted in Vim9 script
3392 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3393 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003394 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003395 return FAIL;
3396 }
3397 p = n;
3398 }
3399 *arg = p;
3400 return OK;
3401}
3402
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403/*
3404 * Handle sixth level expression:
3405 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003406 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003407 * "string" string constant
3408 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 * &option-name option value
3410 * @r register contents
3411 * identifier variable value
3412 * function() function call
3413 * $VAR environment variable
3414 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003415 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003416 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003417 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003418 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 *
3420 * Also handle:
3421 * ! in front logical NOT
3422 * - in front unary minus
3423 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003424 * trailing [] subscript in String or List
3425 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003426 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 *
3428 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003429 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 *
3431 * Return OK or FAIL.
3432 */
3433 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003434eval7(
3435 char_u **arg,
3436 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003437 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003438 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003440 int evaluate = evalarg != NULL
3441 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 int len;
3443 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003444 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 char_u *start_leader, *end_leader;
3446 int ret = OK;
3447 char_u *alias;
3448
3449 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003450 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003451 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003453 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454
3455 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003456 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 */
3458 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003459 if (eval_leader(arg, in_vim9script()) == FAIL)
3460 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 end_leader = *arg;
3462
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003463 if (**arg == '.' && (!isdigit(*(*arg + 1))
3464#ifdef FEAT_FLOAT
Bram Moolenaardd9de502021-08-15 13:49:42 +02003465 || in_old_script(2)
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003466#endif
3467 ))
3468 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003469 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003470 ++*arg;
3471 return FAIL;
3472 }
3473
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 switch (**arg)
3475 {
3476 /*
3477 * Number constant.
3478 */
3479 case '0':
3480 case '1':
3481 case '2':
3482 case '3':
3483 case '4':
3484 case '5':
3485 case '6':
3486 case '7':
3487 case '8':
3488 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003489 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003490
3491 // Apply prefixed "-" and "+" now. Matters especially when
3492 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003493 if (ret == OK && evaluate && end_leader > start_leader
3494 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003495 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 break;
3497
3498 /*
3499 * String constant: "string".
3500 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003501 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 break;
3503
3504 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003505 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003507 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003508 break;
3509
3510 /*
3511 * List: [expr, expr]
3512 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003513 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 break;
3515
3516 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003517 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003518 */
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003519 case '#': if (in_vim9script())
3520 {
3521 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3522 }
3523 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003524 {
3525 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003526 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003527 }
3528 else
3529 ret = NOTDONE;
3530 break;
3531
3532 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003533 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003534 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003535 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003536 case '{': if (in_vim9script())
3537 ret = NOTDONE;
3538 else
3539 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003540 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003541 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003542 break;
3543
3544 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003545 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003547 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 break;
3549
3550 /*
3551 * Environment variable: $VAR.
3552 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003553 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 break;
3555
3556 /*
3557 * Register contents: @r.
3558 */
3559 case '@': ++*arg;
3560 if (evaluate)
3561 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003562 if (in_vim9script() && IS_WHITE_OR_NUL(**arg))
3563 semsg(_(e_syntax_error_at_str), *arg);
3564 else if (in_vim9script() && !valid_yank_reg(**arg, FALSE))
3565 emsg_invreg(**arg);
3566 else
3567 {
3568 rettv->v_type = VAR_STRING;
3569 rettv->vval.v_string = get_reg_contents(**arg,
3570 GREG_EXPR_SRC);
3571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 }
3573 if (**arg != NUL)
3574 ++*arg;
3575 break;
3576
3577 /*
3578 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003579 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003581 case '(': ret = NOTDONE;
3582 if (in_vim9script())
Bram Moolenaar06409502021-02-17 17:00:27 +01003583 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003584 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003585 if (ret == OK && evaluate)
3586 {
3587 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3588
Bram Moolenaara9931532021-06-12 15:58:16 +02003589 // Compile it here to get the return type. The return
3590 // type is optional, when it's missing use t_unknown.
3591 // This is recognized in compile_return().
3592 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3593 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003594 if (compile_def_function(ufunc,
Bram Moolenaare99d4222021-06-13 14:01:26 +02003595 FALSE, COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003596 {
3597 clear_tv(rettv);
3598 ret = FAIL;
3599 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003600 }
3601 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003602 if (ret == NOTDONE)
3603 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003604 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003605 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003606
Bram Moolenaar9215f012020-06-27 21:18:00 +02003607 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003608 if (**arg == ')')
3609 ++*arg;
3610 else if (ret == OK)
3611 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003612 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003613 clear_tv(rettv);
3614 ret = FAIL;
3615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 }
3617 break;
3618
Bram Moolenaar8c711452005-01-14 21:53:12 +00003619 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 break;
3621 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003622
3623 if (ret == NOTDONE)
3624 {
3625 /*
3626 * Must be a variable or function name.
3627 * Can also be a curly-braces kind of name: {expr}.
3628 */
3629 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003630 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003631 if (alias != NULL)
3632 s = alias;
3633
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003634 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003635 ret = FAIL;
3636 else
3637 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003638 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3639
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003640 if (evaluate && in_vim9script() && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003641 {
3642 emsg(_(e_cannot_use_underscore_here));
3643 ret = FAIL;
3644 }
3645 else if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003646 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003647 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003648 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003649 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003650 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003651 else if (flags & EVAL_CONSTANT)
3652 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003653 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003654 {
3655 // get the value of "true", "false" or a variable
3656 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3657 {
3658 rettv->v_type = VAR_BOOL;
3659 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003660 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003661 }
3662 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003663 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003664 {
3665 rettv->v_type = VAR_BOOL;
3666 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003667 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003668 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003669 else if (len == 4 && in_vim9script()
3670 && STRNCMP(s, "null", 4) == 0)
3671 {
3672 rettv->v_type = VAR_SPECIAL;
3673 rettv->vval.v_number = VVAL_NULL;
3674 ret = OK;
3675 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003676 else
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003677 {
3678 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003679 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003680 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003681 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003682 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003683 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003684 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003685 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003686 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003687 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003688 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003689 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003690 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003691 }
3692
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003693 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3694 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003695 if (ret == OK)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003696 ret = handle_subscript(arg, name_start, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697
3698 /*
3699 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3700 */
3701 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003702 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003703 return ret;
3704}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003705
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003706/*
3707 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003708 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003709 * Adjusts "end_leaderp" until it is at "start_leader".
3710 */
3711 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003712eval7_leader(
3713 typval_T *rettv,
3714 int numeric_only,
3715 char_u *start_leader,
3716 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003717{
3718 char_u *end_leader = *end_leaderp;
3719 int ret = OK;
3720 int error = FALSE;
3721 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003722 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003723#ifdef FEAT_FLOAT
3724 float_T f = 0.0;
3725
3726 if (rettv->v_type == VAR_FLOAT)
3727 f = rettv->vval.v_float;
3728 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003729#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003730 {
3731 while (VIM_ISWHITE(end_leader[-1]))
3732 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003733 if (in_vim9script() && end_leader[-1] == '!')
3734 val = tv2bool(rettv);
3735 else
3736 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003737 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003738 if (error)
3739 {
3740 clear_tv(rettv);
3741 ret = FAIL;
3742 }
3743 else
3744 {
3745 while (end_leader > start_leader)
3746 {
3747 --end_leader;
3748 if (*end_leader == '!')
3749 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003750 if (numeric_only)
3751 {
3752 ++end_leader;
3753 break;
3754 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003755#ifdef FEAT_FLOAT
3756 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003757 {
3758 if (in_vim9script())
3759 {
3760 rettv->v_type = VAR_BOOL;
3761 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3762 }
3763 else
3764 f = !f;
3765 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003766 else
3767#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003768 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003769 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003770 type = VAR_BOOL;
3771 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003772 }
3773 else if (*end_leader == '-')
3774 {
3775#ifdef FEAT_FLOAT
3776 if (rettv->v_type == VAR_FLOAT)
3777 f = -f;
3778 else
3779#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003780 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003781 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003782 type = VAR_NUMBER;
3783 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003784 }
3785 }
3786#ifdef FEAT_FLOAT
3787 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003789 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003790 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003792 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003793#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003794 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003795 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003796 if (in_vim9script())
3797 rettv->v_type = type;
3798 else
3799 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003800 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003803 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 return ret;
3805}
3806
3807/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003808 * Call the function referred to in "rettv".
3809 */
3810 static int
3811call_func_rettv(
3812 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003813 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003814 typval_T *rettv,
3815 int evaluate,
3816 dict_T *selfdict,
3817 typval_T *basetv)
3818{
3819 partial_T *pt = NULL;
3820 funcexe_T funcexe;
3821 typval_T functv;
3822 char_u *s;
3823 int ret;
3824
3825 // need to copy the funcref so that we can clear rettv
3826 if (evaluate)
3827 {
3828 functv = *rettv;
3829 rettv->v_type = VAR_UNKNOWN;
3830
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003831 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003832 if (functv.v_type == VAR_PARTIAL)
3833 {
3834 pt = functv.vval.v_partial;
3835 s = partial_name(pt);
3836 }
3837 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003838 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003839 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003840 if (s == NULL || *s == NUL)
3841 {
3842 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02003843 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003844 goto theend;
3845 }
3846 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003847 }
3848 else
3849 s = (char_u *)"";
3850
Bram Moolenaara80faa82020-04-12 19:37:17 +02003851 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003852 funcexe.fe_firstline = curwin->w_cursor.lnum;
3853 funcexe.fe_lastline = curwin->w_cursor.lnum;
3854 funcexe.fe_evaluate = evaluate;
3855 funcexe.fe_partial = pt;
3856 funcexe.fe_selfdict = selfdict;
3857 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003858 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003859
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003860theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003861 // Clear the funcref afterwards, so that deleting it while
3862 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003863 if (evaluate)
3864 clear_tv(&functv);
3865
3866 return ret;
3867}
3868
3869/*
3870 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003871 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003872 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3873 */
3874 static int
3875eval_lambda(
3876 char_u **arg,
3877 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003878 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003879 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003880{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003881 int evaluate = evalarg != NULL
3882 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003883 typval_T base = *rettv;
3884 int ret;
3885
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003886 rettv->v_type = VAR_UNKNOWN;
3887
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003888 if (**arg == '{')
3889 {
3890 // ->{lambda}()
3891 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3892 }
3893 else
3894 {
3895 // ->(lambda)()
3896 ++*arg;
3897 ret = eval1(arg, rettv, evalarg);
3898 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar605ec912021-12-18 16:54:31 +00003899 if (**arg == ')')
3900 {
3901 ++*arg;
3902 }
3903 else
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003904 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003905 emsg(_(e_missing_closing_paren));
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003906 ret = FAIL;
3907 }
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003908 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003909 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003910 return FAIL;
3911 else if (**arg != '(')
3912 {
3913 if (verbose)
3914 {
3915 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00003916 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003917 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003918 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003919 }
3920 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003921 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003922 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003923 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003924 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003925
3926 // Clear the funcref afterwards, so that deleting it while
3927 // evaluating the arguments is possible (see test55).
3928 if (evaluate)
3929 clear_tv(&base);
3930
3931 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003932}
3933
3934/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003935 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003936 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02003937 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3938 */
3939 static int
3940eval_method(
3941 char_u **arg,
3942 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003943 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003944 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003945{
3946 char_u *name;
3947 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003948 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003949 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003950 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003951 int evaluate = evalarg != NULL
3952 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003953
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003954 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003955
Bram Moolenaarac92e252019-08-03 21:58:38 +02003956 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003957 len = get_name_len(arg, &alias, evaluate, TRUE);
3958 if (alias != NULL)
3959 name = alias;
3960
3961 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003962 {
3963 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00003964 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003965 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003966 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003967 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003968 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003969 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003970 if (**arg != '(')
3971 {
3972 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00003973 semsg(_(e_missing_parenthesis_str), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003974 ret = FAIL;
3975 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003976 else if (VIM_ISWHITE((*arg)[-1]))
3977 {
3978 if (verbose)
Bram Moolenaar3a846e62022-01-01 16:21:00 +00003979 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar51841322019-08-08 21:10:01 +02003980 ret = FAIL;
3981 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003982 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003983 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003984 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003985 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003986
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003987 // Clear the funcref afterwards, so that deleting it while
3988 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003989 if (evaluate)
3990 clear_tv(&base);
3991
Bram Moolenaarac92e252019-08-03 21:58:38 +02003992 return ret;
3993}
3994
3995/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003996 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3997 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003998 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3999 */
4000 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004001eval_index(
4002 char_u **arg,
4003 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004004 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004005 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004006{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004007 int evaluate = evalarg != NULL
4008 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004009 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004010 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004011 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004012 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004013 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004014 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004015
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004016 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4017 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004018
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004019 init_tv(&var1);
4020 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004021 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004022 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004023 /*
4024 * dict.name
4025 */
4026 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004027 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004028 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004029 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004030 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004031 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004032 }
4033 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004034 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004035 /*
4036 * something[idx]
4037 *
4038 * Get the (first) variable from inside the [].
4039 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004040 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004041 if (**arg == ':')
4042 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004043 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004044 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004045 else if (vim9 && **arg == ':')
4046 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004047 semsg(_(e_white_space_required_before_and_after_str_at_str),
4048 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004049 clear_tv(&var1);
4050 return FAIL;
4051 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004052 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004053 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004054 int error = FALSE;
4055
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004056#ifdef FEAT_FLOAT
4057 // allow for indexing with float
4058 if (vim9 && rettv->v_type == VAR_DICT
4059 && var1.v_type == VAR_FLOAT)
4060 {
4061 var1.vval.v_string = typval_tostring(&var1, TRUE);
4062 var1.v_type = VAR_STRING;
4063 }
4064#endif
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004065 if (vim9 && rettv->v_type == VAR_LIST)
4066 tv_get_number_chk(&var1, &error);
4067 else
4068 error = tv_get_string_chk(&var1) == NULL;
4069 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004070 {
4071 // not a number or string
4072 clear_tv(&var1);
4073 return FAIL;
4074 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004075 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004076
4077 /*
4078 * Get the second variable from inside the [:].
4079 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004080 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004081 if (**arg == ':')
4082 {
4083 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004084 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01004085 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004086 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004087 semsg(_(e_white_space_required_before_and_after_str_at_str),
4088 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004089 if (!empty1)
4090 clear_tv(&var1);
4091 return FAIL;
4092 }
4093 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004094 if (**arg == ']')
4095 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004096 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004097 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004098 if (!empty1)
4099 clear_tv(&var1);
4100 return FAIL;
4101 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004102 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004103 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004104 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004105 if (!empty1)
4106 clear_tv(&var1);
4107 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004108 return FAIL;
4109 }
4110 }
4111
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004112 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004113 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004114 if (**arg != ']')
4115 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004116 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004117 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004118 clear_tv(&var1);
4119 if (range)
4120 clear_tv(&var2);
4121 return FAIL;
4122 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004123 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004124 }
4125
4126 if (evaluate)
4127 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004128 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004129 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004130 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004131
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004132 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004133 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004134 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004135 clear_tv(&var2);
4136 return res;
4137 }
4138 return OK;
4139}
4140
4141/*
4142 * Check if "rettv" can have an [index] or [sli:ce]
4143 */
4144 int
4145check_can_index(typval_T *rettv, int evaluate, int verbose)
4146{
4147 switch (rettv->v_type)
4148 {
4149 case VAR_FUNC:
4150 case VAR_PARTIAL:
4151 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004152 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004153 return FAIL;
4154 case VAR_FLOAT:
4155#ifdef FEAT_FLOAT
4156 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004157 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004158 return FAIL;
4159#endif
4160 case VAR_BOOL:
4161 case VAR_SPECIAL:
4162 case VAR_JOB:
4163 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004164 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004165 if (verbose)
4166 emsg(_(e_cannot_index_special_variable));
4167 return FAIL;
4168 case VAR_UNKNOWN:
4169 case VAR_ANY:
4170 case VAR_VOID:
4171 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004172 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004173 emsg(_(e_cannot_index_special_variable));
4174 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004175 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004176 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004177
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004178 case VAR_STRING:
4179 case VAR_LIST:
4180 case VAR_DICT:
4181 case VAR_BLOB:
4182 break;
4183 case VAR_NUMBER:
4184 if (in_vim9script())
4185 emsg(_(e_cannot_index_number));
4186 break;
4187 }
4188 return OK;
4189}
4190
4191/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004192 * slice() function
4193 */
4194 void
4195f_slice(typval_T *argvars, typval_T *rettv)
4196{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004197 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004198 && ((argvars[0].v_type != VAR_STRING
4199 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004200 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004201 && check_for_list_arg(argvars, 0) == FAIL)
4202 || check_for_number_arg(argvars, 1) == FAIL
4203 || check_for_opt_number_arg(argvars, 2) == FAIL))
4204 return;
4205
Bram Moolenaar6601b622021-01-13 21:47:15 +01004206 if (check_can_index(argvars, TRUE, FALSE) == OK)
4207 {
4208 copy_tv(argvars, rettv);
4209 eval_index_inner(rettv, TRUE, argvars + 1,
4210 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4211 TRUE, NULL, 0, FALSE);
4212 }
4213}
4214
4215/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004216 * Apply index or range to "rettv".
4217 * "var1" is the first index, NULL for [:expr].
4218 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004219 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4220 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004221 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4222 */
4223 int
4224eval_index_inner(
4225 typval_T *rettv,
4226 int is_range,
4227 typval_T *var1,
4228 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004229 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004230 char_u *key,
4231 int keylen,
4232 int verbose)
4233{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004234 varnumber_T n1, n2 = 0;
4235 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004236
4237 n1 = 0;
4238 if (var1 != NULL && rettv->v_type != VAR_DICT)
4239 n1 = tv_get_number(var1);
4240
4241 if (is_range)
4242 {
4243 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004244 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004245 if (verbose)
4246 emsg(_(e_cannot_slice_dictionary));
4247 return FAIL;
4248 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004249 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004250 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004251 else
4252 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004253 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004254
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004255 switch (rettv->v_type)
4256 {
4257 case VAR_UNKNOWN:
4258 case VAR_ANY:
4259 case VAR_VOID:
4260 case VAR_FUNC:
4261 case VAR_PARTIAL:
4262 case VAR_FLOAT:
4263 case VAR_BOOL:
4264 case VAR_SPECIAL:
4265 case VAR_JOB:
4266 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004267 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004268 break; // not evaluating, skipping over subscript
4269
4270 case VAR_NUMBER:
4271 case VAR_STRING:
4272 {
4273 char_u *s = tv_get_string(rettv);
4274
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004275 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004276 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004277 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004278 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004279 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004280 else
4281 s = char_from_string(s, n1);
4282 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004283 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004284 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004285 // The resulting variable is a substring. If the indexes
4286 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004287 if (n1 < 0)
4288 {
4289 n1 = len + n1;
4290 if (n1 < 0)
4291 n1 = 0;
4292 }
4293 if (n2 < 0)
4294 n2 = len + n2;
4295 else if (n2 >= len)
4296 n2 = len;
4297 if (n1 >= len || n2 < 0 || n1 > n2)
4298 s = NULL;
4299 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004300 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004301 }
4302 else
4303 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004304 // The resulting variable is a string of a single
4305 // character. If the index is too big or negative the
4306 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004307 if (n1 >= len || n1 < 0)
4308 s = NULL;
4309 else
4310 s = vim_strnsave(s + n1, 1);
4311 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004312 clear_tv(rettv);
4313 rettv->v_type = VAR_STRING;
4314 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004315 }
4316 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004317
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004318 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004319 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4320 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004321 break;
4322
4323 case VAR_LIST:
4324 if (var1 == NULL)
4325 n1 = 0;
4326 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004327 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004328 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004329 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004330 return FAIL;
4331 break;
4332
4333 case VAR_DICT:
4334 {
4335 dictitem_T *item;
4336 typval_T tmp;
4337
4338 if (key == NULL)
4339 {
4340 key = tv_get_string_chk(var1);
4341 if (key == NULL)
4342 return FAIL;
4343 }
4344
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004345 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004346
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004347 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004348 {
4349 if (verbose)
4350 {
4351 if (keylen > 0)
4352 key[keylen] = NUL;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004353 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004354 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004355 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004356 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004357
4358 copy_tv(&item->di_tv, &tmp);
4359 clear_tv(rettv);
4360 *rettv = tmp;
4361 }
4362 break;
4363 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004364 return OK;
4365}
4366
4367/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004368 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004369 */
4370 char_u *
4371partial_name(partial_T *pt)
4372{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004373 if (pt != NULL)
4374 {
4375 if (pt->pt_name != NULL)
4376 return pt->pt_name;
4377 if (pt->pt_func != NULL)
4378 return pt->pt_func->uf_name;
4379 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004380 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004381}
4382
Bram Moolenaarddecc252016-04-06 22:59:37 +02004383 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004384partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004385{
4386 int i;
4387
4388 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004389 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004390 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004391 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004392 if (pt->pt_name != NULL)
4393 {
4394 func_unref(pt->pt_name);
4395 vim_free(pt->pt_name);
4396 }
4397 else
4398 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004399
Bram Moolenaar54656012021-06-09 20:50:46 +02004400 // "out_up" is no longer used, decrement refcount on partial that owns it.
4401 partial_unref(pt->pt_outer.out_up_partial);
4402
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004403 // Decrease the reference count for the context of a closure. If down
4404 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004405 if (pt->pt_funcstack != NULL)
4406 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004407 --pt->pt_funcstack->fs_refcount;
4408 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004409 }
4410
Bram Moolenaarddecc252016-04-06 22:59:37 +02004411 vim_free(pt);
4412}
4413
4414/*
4415 * Unreference a closure: decrement the reference count and free it when it
4416 * becomes zero.
4417 */
4418 void
4419partial_unref(partial_T *pt)
4420{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004421 if (pt != NULL)
4422 {
4423 if (--pt->pt_refcount <= 0)
4424 partial_free(pt);
4425
4426 // If the reference count goes down to one, the funcstack may be the
4427 // only reference and can be freed if no other partials reference it.
4428 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4429 funcstack_check_refcount(pt->pt_funcstack);
4430 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004431}
4432
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004433/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004434 * Return the next (unique) copy ID.
4435 * Used for serializing nested structures.
4436 */
4437 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004438get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004439{
4440 current_copyID += COPYID_INC;
4441 return current_copyID;
4442}
4443
4444/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004445 * Garbage collection for lists and dictionaries.
4446 *
4447 * We use reference counts to be able to free most items right away when they
4448 * are no longer used. But for composite items it's possible that it becomes
4449 * unused while the reference count is > 0: When there is a recursive
4450 * reference. Example:
4451 * :let l = [1, 2, 3]
4452 * :let d = {9: l}
4453 * :let l[1] = d
4454 *
4455 * Since this is quite unusual we handle this with garbage collection: every
4456 * once in a while find out which lists and dicts are not referenced from any
4457 * variable.
4458 *
4459 * Here is a good reference text about garbage collection (refers to Python
4460 * but it applies to all reference-counting mechanisms):
4461 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004462 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004463
4464/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004465 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004466 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004467 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004468 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004469 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004470garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004471{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004472 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004473 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004474 buf_T *buf;
4475 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004476 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004477 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004478
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004479 if (!testing)
4480 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004481 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004482 want_garbage_collect = FALSE;
4483 may_garbage_collect = FALSE;
4484 garbage_collect_at_exit = FALSE;
4485 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004486
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004487 // The execution stack can grow big, limit the size.
4488 if (exestack.ga_maxlen - exestack.ga_len > 500)
4489 {
4490 size_t new_len;
4491 char_u *pp;
4492 int n;
4493
4494 // Keep 150% of the current size, with a minimum of the growth size.
4495 n = exestack.ga_len / 2;
4496 if (n < exestack.ga_growsize)
4497 n = exestack.ga_growsize;
4498
4499 // Don't make it bigger though.
4500 if (exestack.ga_len + n < exestack.ga_maxlen)
4501 {
4502 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4503 pp = vim_realloc(exestack.ga_data, new_len);
4504 if (pp == NULL)
4505 return FAIL;
4506 exestack.ga_maxlen = exestack.ga_len + n;
4507 exestack.ga_data = pp;
4508 }
4509 }
4510
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004511 // We advance by two because we add one for items referenced through
4512 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004513 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004514
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004515 /*
4516 * 1. Go through all accessible variables and mark all lists and dicts
4517 * with copyID.
4518 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004519
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004520 // Don't free variables in the previous_funccal list unless they are only
4521 // referenced through previous_funccal. This must be first, because if
4522 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004523 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004524
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004525 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004526 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004527
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004528 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004529 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004530 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4531 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004532
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004533 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004534 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004535 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4536 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004537 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004538 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4539 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004540#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004541 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004542 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4543 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004544 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004545 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004546 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4547 NULL, NULL);
4548#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004549
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004550 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004551 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004552 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4553 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004554 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004555 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004556
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004557 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004558 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004559
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004560 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004561 abort = abort || set_ref_in_functions(copyID);
4562
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004563 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004564 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004565
Bram Moolenaar7509ad82021-12-14 18:14:37 +00004566 // funcstacks keep variables for closures
4567 abort = abort || set_ref_in_funcstacks(copyID);
4568
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004569 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004570 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004571
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004572 // callbacks in buffers
4573 abort = abort || set_ref_in_buffers(copyID);
4574
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00004575 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
4576 abort = abort || set_ref_in_insexpand_funcs(copyID);
4577
4578 // 'operatorfunc' callback
4579 abort = abort || set_ref_in_opfunc(copyID);
4580
4581 // 'tagfunc' callback
4582 abort = abort || set_ref_in_tagfunc(copyID);
4583
4584 // 'imactivatefunc' and 'imstatusfunc' callbacks
4585 abort = abort || set_ref_in_im_funcs(copyID);
4586
Bram Moolenaar1dced572012-04-05 16:54:08 +02004587#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004588 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004589#endif
4590
Bram Moolenaardb913952012-06-29 12:54:53 +02004591#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004592 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004593#endif
4594
4595#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004596 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004597#endif
4598
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004599#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004600 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004601 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004602#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004603#ifdef FEAT_NETBEANS_INTG
4604 abort = abort || set_ref_in_nb_channel(copyID);
4605#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004606
Bram Moolenaare3188e22016-05-31 21:13:04 +02004607#ifdef FEAT_TIMERS
4608 abort = abort || set_ref_in_timer(copyID);
4609#endif
4610
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004611#ifdef FEAT_QUICKFIX
4612 abort = abort || set_ref_in_quickfix(copyID);
4613#endif
4614
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004615#ifdef FEAT_TERMINAL
4616 abort = abort || set_ref_in_term(copyID);
4617#endif
4618
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004619#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004620 abort = abort || set_ref_in_popups(copyID);
4621#endif
4622
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004623 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004624 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004625 /*
4626 * 2. Free lists and dictionaries that are not referenced.
4627 */
4628 did_free = free_unref_items(copyID);
4629
4630 /*
4631 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004632 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004633 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004634 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004635 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004636 else if (p_verbose > 0)
4637 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004638 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004639 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004640
4641 return did_free;
4642}
4643
4644/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004645 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004646 */
4647 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004648free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004649{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004650 int did_free = FALSE;
4651
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004652 // Let all "free" functions know that we are here. This means no
4653 // dictionaries, lists, channels or jobs are to be freed, because we will
4654 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004655 in_free_unref_items = TRUE;
4656
4657 /*
4658 * PASS 1: free the contents of the items. We don't free the items
4659 * themselves yet, so that it is possible to decrement refcount counters
4660 */
4661
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004662 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004663 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004664
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004665 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004666 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004667
4668#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004669 // Go through the list of jobs and free items without the copyID. This
4670 // must happen before doing channels, because jobs refer to channels, but
4671 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004672 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4673
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004674 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004675 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4676#endif
4677
4678 /*
4679 * PASS 2: free the items themselves.
4680 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004681 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004682 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004683
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004684#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004685 // Go through the list of jobs and free items without the copyID. This
4686 // must happen before doing channels, because jobs refer to channels, but
4687 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004688 free_unused_jobs(copyID, COPYID_MASK);
4689
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004690 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004691 free_unused_channels(copyID, COPYID_MASK);
4692#endif
4693
4694 in_free_unref_items = FALSE;
4695
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004696 return did_free;
4697}
4698
4699/*
4700 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004701 * "list_stack" is used to add lists to be marked. Can be NULL.
4702 *
4703 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004704 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004705 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004706set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004707{
4708 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004709 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004710 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004711 hashtab_T *cur_ht;
4712 ht_stack_T *ht_stack = NULL;
4713 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004714
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004715 cur_ht = ht;
4716 for (;;)
4717 {
4718 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004719 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004720 // Mark each item in the hashtab. If the item contains a hashtab
4721 // it is added to ht_stack, if it contains a list it is added to
4722 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004723 todo = (int)cur_ht->ht_used;
4724 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4725 if (!HASHITEM_EMPTY(hi))
4726 {
4727 --todo;
4728 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4729 &ht_stack, list_stack);
4730 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004731 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004732
4733 if (ht_stack == NULL)
4734 break;
4735
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004736 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004737 cur_ht = ht_stack->ht;
4738 tempitem = ht_stack;
4739 ht_stack = ht_stack->prev;
4740 free(tempitem);
4741 }
4742
4743 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004744}
4745
Dominique Pelle748b3082022-01-08 12:41:16 +00004746#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
4747 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004748/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004749 * Mark a dict and its items with "copyID".
4750 * Returns TRUE if setting references failed somehow.
4751 */
4752 int
4753set_ref_in_dict(dict_T *d, int copyID)
4754{
4755 if (d != NULL && d->dv_copyID != copyID)
4756 {
4757 d->dv_copyID = copyID;
4758 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4759 }
4760 return FALSE;
4761}
Dominique Pelle748b3082022-01-08 12:41:16 +00004762#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004763
4764/*
4765 * Mark a list and its items with "copyID".
4766 * Returns TRUE if setting references failed somehow.
4767 */
4768 int
4769set_ref_in_list(list_T *ll, int copyID)
4770{
4771 if (ll != NULL && ll->lv_copyID != copyID)
4772 {
4773 ll->lv_copyID = copyID;
4774 return set_ref_in_list_items(ll, copyID, NULL);
4775 }
4776 return FALSE;
4777}
4778
4779/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004780 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004781 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4782 *
4783 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004784 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004785 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004786set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004787{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004788 listitem_T *li;
4789 int abort = FALSE;
4790 list_T *cur_l;
4791 list_stack_T *list_stack = NULL;
4792 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004793
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004794 cur_l = l;
4795 for (;;)
4796 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004797 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004798 // Mark each item in the list. If the item contains a hashtab
4799 // it is added to ht_stack, if it contains a list it is added to
4800 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004801 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4802 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4803 ht_stack, &list_stack);
4804 if (list_stack == NULL)
4805 break;
4806
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004807 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004808 cur_l = list_stack->list;
4809 tempitem = list_stack;
4810 list_stack = list_stack->prev;
4811 free(tempitem);
4812 }
4813
4814 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004815}
4816
4817/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00004818 * Mark the partial in callback 'cb' with "copyID".
4819 */
4820 int
4821set_ref_in_callback(callback_T *cb, int copyID)
4822{
4823 typval_T tv;
4824
4825 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
4826 return FALSE;
4827
4828 tv.v_type = VAR_PARTIAL;
4829 tv.vval.v_partial = cb->cb_partial;
4830 return set_ref_in_item(&tv, copyID, NULL, NULL);
4831}
4832
4833/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004834 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004835 * "list_stack" is used to add lists to be marked. Can be NULL.
4836 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4837 *
4838 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004839 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004840 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004841set_ref_in_item(
4842 typval_T *tv,
4843 int copyID,
4844 ht_stack_T **ht_stack,
4845 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004846{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004847 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004848
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004849 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004850 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004851 dict_T *dd = tv->vval.v_dict;
4852
Bram Moolenaara03f2332016-02-06 18:09:59 +01004853 if (dd != NULL && dd->dv_copyID != copyID)
4854 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004855 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004856 dd->dv_copyID = copyID;
4857 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004858 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004859 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4860 }
4861 else
4862 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004863 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4864
Bram Moolenaara03f2332016-02-06 18:09:59 +01004865 if (newitem == NULL)
4866 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004867 else
4868 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004869 newitem->ht = &dd->dv_hashtab;
4870 newitem->prev = *ht_stack;
4871 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004872 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004873 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004874 }
4875 }
4876 else if (tv->v_type == VAR_LIST)
4877 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004878 list_T *ll = tv->vval.v_list;
4879
Bram Moolenaara03f2332016-02-06 18:09:59 +01004880 if (ll != NULL && ll->lv_copyID != copyID)
4881 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004882 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004883 ll->lv_copyID = copyID;
4884 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004885 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004886 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004887 }
4888 else
4889 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004890 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4891
Bram Moolenaara03f2332016-02-06 18:09:59 +01004892 if (newitem == NULL)
4893 abort = TRUE;
4894 else
4895 {
4896 newitem->list = ll;
4897 newitem->prev = *list_stack;
4898 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004899 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004900 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004901 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004902 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004903 else if (tv->v_type == VAR_FUNC)
4904 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004905 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004906 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004907 else if (tv->v_type == VAR_PARTIAL)
4908 {
4909 partial_T *pt = tv->vval.v_partial;
4910 int i;
4911
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004912 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004913 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004914 // Didn't see this partial yet.
4915 pt->pt_copyID = copyID;
4916
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004917 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004918
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004919 if (pt->pt_dict != NULL)
4920 {
4921 typval_T dtv;
4922
4923 dtv.v_type = VAR_DICT;
4924 dtv.vval.v_dict = pt->pt_dict;
4925 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4926 }
4927
4928 for (i = 0; i < pt->pt_argc; ++i)
4929 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4930 ht_stack, list_stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +00004931 // pt_funcstack is handled in set_ref_in_funcstacks()
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004932 }
4933 }
4934#ifdef FEAT_JOB_CHANNEL
4935 else if (tv->v_type == VAR_JOB)
4936 {
4937 job_T *job = tv->vval.v_job;
4938 typval_T dtv;
4939
4940 if (job != NULL && job->jv_copyID != copyID)
4941 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004942 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004943 if (job->jv_channel != NULL)
4944 {
4945 dtv.v_type = VAR_CHANNEL;
4946 dtv.vval.v_channel = job->jv_channel;
4947 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4948 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004949 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004950 {
4951 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004952 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004953 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4954 }
4955 }
4956 }
4957 else if (tv->v_type == VAR_CHANNEL)
4958 {
4959 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004960 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004961 typval_T dtv;
4962 jsonq_T *jq;
4963 cbq_T *cq;
4964
4965 if (ch != NULL && ch->ch_copyID != copyID)
4966 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004967 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004968 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004969 {
4970 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4971 jq = jq->jq_next)
4972 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4973 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4974 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004975 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004976 {
4977 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004978 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004979 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4980 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004981 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004982 {
4983 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004984 dtv.vval.v_partial =
4985 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004986 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4987 }
4988 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004989 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004990 {
4991 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004992 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004993 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4994 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004995 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004996 {
4997 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004998 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004999 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5000 }
5001 }
5002 }
5003#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005004 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005005}
5006
Bram Moolenaar8c711452005-01-14 21:53:12 +00005007/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005008 * Return a string with the string representation of a variable.
5009 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005010 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005011 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005012 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005013 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005014 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005015 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5016 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005017 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005018 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005019 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005020echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005021 typval_T *tv,
5022 char_u **tofree,
5023 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005024 int copyID,
5025 int echo_style,
5026 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005027 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005028{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005029 static int recurse = 0;
5030 char_u *r = NULL;
5031
Bram Moolenaar33570922005-01-25 22:26:29 +00005032 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005033 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005034 if (!did_echo_string_emsg)
5035 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005036 // Only give this message once for a recursive call to avoid
5037 // flooding the user with errors. And stop iterating over lists
5038 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005039 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005040 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005041 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005042 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005043 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005044 }
5045 ++recurse;
5046
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005047 switch (tv->v_type)
5048 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005049 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005050 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005051 {
5052 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005053 r = tv->vval.v_string;
5054 if (r == NULL)
5055 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005056 }
5057 else
5058 {
5059 *tofree = string_quote(tv->vval.v_string, FALSE);
5060 r = *tofree;
5061 }
5062 break;
5063
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005064 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005065 if (echo_style)
5066 {
5067 *tofree = NULL;
5068 r = tv->vval.v_string;
5069 }
5070 else
5071 {
5072 *tofree = string_quote(tv->vval.v_string, TRUE);
5073 r = *tofree;
5074 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005075 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005076
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005077 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005078 {
5079 partial_T *pt = tv->vval.v_partial;
5080 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005081 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005082 garray_T ga;
5083 int i;
5084 char_u *tf;
5085
5086 ga_init2(&ga, 1, 100);
5087 ga_concat(&ga, (char_u *)"function(");
5088 if (fname != NULL)
5089 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005090 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005091 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005092 && vim_isupper(fname[1]))
5093 {
5094 ga_concat(&ga, (char_u *)"'g:");
5095 ga_concat(&ga, fname + 1);
5096 }
5097 else
5098 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005099 vim_free(fname);
5100 }
5101 if (pt != NULL && pt->pt_argc > 0)
5102 {
5103 ga_concat(&ga, (char_u *)", [");
5104 for (i = 0; i < pt->pt_argc; ++i)
5105 {
5106 if (i > 0)
5107 ga_concat(&ga, (char_u *)", ");
5108 ga_concat(&ga,
5109 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5110 vim_free(tf);
5111 }
5112 ga_concat(&ga, (char_u *)"]");
5113 }
5114 if (pt != NULL && pt->pt_dict != NULL)
5115 {
5116 typval_T dtv;
5117
5118 ga_concat(&ga, (char_u *)", ");
5119 dtv.v_type = VAR_DICT;
5120 dtv.vval.v_dict = pt->pt_dict;
5121 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5122 vim_free(tf);
5123 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005124 // terminate with ')' and a NUL
5125 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005126
5127 *tofree = ga.ga_data;
5128 r = *tofree;
5129 break;
5130 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005131
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005132 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005133 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005134 break;
5135
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005136 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005137 if (tv->vval.v_list == NULL)
5138 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005139 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005140 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005141 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005142 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005143 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5144 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005145 {
5146 *tofree = NULL;
5147 r = (char_u *)"[...]";
5148 }
5149 else
5150 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005151 int old_copyID = tv->vval.v_list->lv_copyID;
5152
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005153 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005154 *tofree = list2string(tv, copyID, restore_copyID);
5155 if (restore_copyID)
5156 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005157 r = *tofree;
5158 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005159 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005160
Bram Moolenaar8c711452005-01-14 21:53:12 +00005161 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005162 if (tv->vval.v_dict == NULL)
5163 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005164 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005165 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005166 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005167 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005168 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5169 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005170 {
5171 *tofree = NULL;
5172 r = (char_u *)"{...}";
5173 }
5174 else
5175 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005176 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005177
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005178 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005179 *tofree = dict2string(tv, copyID, restore_copyID);
5180 if (restore_copyID)
5181 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005182 r = *tofree;
5183 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005184 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005185
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005186 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005187 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005188 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005189 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005190 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005191 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005192 break;
5193
Bram Moolenaar835dc632016-02-07 14:27:38 +01005194 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005195 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005196#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005197 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005198 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5199 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005200 if (composite_val)
5201 {
5202 *tofree = string_quote(r, FALSE);
5203 r = *tofree;
5204 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005205#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005206 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005207
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005208 case VAR_INSTR:
5209 *tofree = NULL;
5210 r = (char_u *)"instructions";
5211 break;
5212
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005213 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005214#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005215 *tofree = NULL;
5216 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5217 r = numbuf;
5218 break;
5219#endif
5220
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005221 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005222 case VAR_SPECIAL:
5223 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005224 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005225 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005226 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005227
Bram Moolenaar8502c702014-06-17 12:51:16 +02005228 if (--recurse == 0)
5229 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005230 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005231}
5232
5233/*
5234 * Return a string with the string representation of a variable.
5235 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5236 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005237 * Does not put quotes around strings, as ":echo" displays values.
5238 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5239 * May return NULL.
5240 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005241 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005242echo_string(
5243 typval_T *tv,
5244 char_u **tofree,
5245 char_u *numbuf,
5246 int copyID)
5247{
5248 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5249}
5250
5251/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005252 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5253 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005254 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005255 */
5256 int
5257buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5258{
5259 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005260 char_u *t;
5261 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005262
5263 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5264 return -1;
5265
5266 if (lnum > buf->b_ml.ml_line_count)
5267 lnum = buf->b_ml.ml_line_count;
5268
5269 str = ml_get_buf(buf, lnum, FALSE);
5270 if (str == NULL)
5271 return -1;
5272
5273 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005274 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005275
Bram Moolenaar91458462021-01-13 20:08:38 +01005276 // count the number of characters
5277 t = str;
5278 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5279 t += mb_ptr2len(t);
5280
5281 // In insert mode, when the cursor is at the end of a non-empty line,
5282 // byteidx points to the NUL character immediately past the end of the
5283 // string. In this case, add one to the character count.
5284 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5285 count++;
5286
5287 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005288}
5289
5290/*
5291 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005292 * byte index. Works only for loaded buffers. Returns -1 on failure.
5293 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005294 */
5295 int
5296buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5297{
5298 char_u *str;
5299 char_u *t;
5300
5301 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5302 return -1;
5303
5304 if (lnum > buf->b_ml.ml_line_count)
5305 lnum = buf->b_ml.ml_line_count;
5306
5307 str = ml_get_buf(buf, lnum, FALSE);
5308 if (str == NULL)
5309 return -1;
5310
5311 // Convert the character offset to a byte offset
5312 t = str;
5313 while (*t != NUL && --charidx > 0)
5314 t += mb_ptr2len(t);
5315
Bram Moolenaar91458462021-01-13 20:08:38 +01005316 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005317}
5318
5319/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005321 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005323 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005324var2fpos(
5325 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005326 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005327 int *fnum, // set to fnum for '0, 'A, etc.
5328 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005330 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005332 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005334 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005335 if (varp->v_type == VAR_LIST)
5336 {
5337 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005338 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005339 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005340 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005341
5342 l = varp->vval.v_list;
5343 if (l == NULL)
5344 return NULL;
5345
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005346 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005347 pos.lnum = list_find_nr(l, 0L, &error);
5348 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005349 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005350 if (charcol)
5351 len = (long)mb_charlen(ml_get(pos.lnum));
5352 else
5353 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005354
Bram Moolenaarec65d772020-08-20 22:29:12 +02005355 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005356 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005357 li = list_find(l, 1L);
5358 if (li != NULL && li->li_tv.v_type == VAR_STRING
5359 && li->li_tv.vval.v_string != NULL
5360 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005361 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005362 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005363 }
5364 else
5365 {
5366 pos.col = list_find_nr(l, 1L, &error);
5367 if (error)
5368 return NULL;
5369 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005370
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005371 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005372 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005373 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005374 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005375
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005376 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005377 pos.coladd = list_find_nr(l, 2L, &error);
5378 if (error)
5379 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005380
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005381 return &pos;
5382 }
5383
Bram Moolenaarc5809432021-03-27 21:23:30 +01005384 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5385 return NULL;
5386
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005387 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005388 if (name == NULL)
5389 return NULL;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005390 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005391 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005392 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005393 pos = curwin->w_cursor;
5394 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005395 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005396 return &pos;
5397 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005398 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005399 {
5400 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005401 pos = VIsual;
5402 else
5403 pos = curwin->w_cursor;
5404 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005405 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005406 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005407 }
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005408 if (name[0] == '\'' && (!in_vim9script()
5409 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005411 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005412 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5414 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005415 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005416 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 return pp;
5418 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005419
Bram Moolenaara5525202006-03-02 22:52:09 +00005420 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005421
Bram Moolenaar477933c2007-07-17 14:32:23 +00005422 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005423 {
5424 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005425 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005426 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005427 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005428 // In silent Ex mode topline is zero, but that's not a valid line
5429 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005430 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005431 return &pos;
5432 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005433 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005434 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005435 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005436 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005437 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005438 return &pos;
5439 }
5440 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005441 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005443 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 {
5445 pos.lnum = curbuf->b_ml.ml_line_count;
5446 pos.col = 0;
5447 }
5448 else
5449 {
5450 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005451 if (charcol)
5452 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5453 else
5454 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 }
5456 return &pos;
5457 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005458 if (in_vim9script())
5459 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 return NULL;
5461}
5462
5463/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005464 * Convert list in "arg" into a position and optional file number.
5465 * When "fnump" is NULL there is no file number, only 3 items.
5466 * Note that the column is passed on as-is, the caller may want to decrement
5467 * it to use 1 for the first column.
5468 * Return FAIL when conversion is not possible, doesn't check the position for
5469 * validity.
5470 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005471 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005472list2fpos(
5473 typval_T *arg,
5474 pos_T *posp,
5475 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005476 colnr_T *curswantp,
5477 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005478{
5479 list_T *l = arg->vval.v_list;
5480 long i = 0;
5481 long n;
5482
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005483 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5484 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005485 if (arg->v_type != VAR_LIST
5486 || l == NULL
5487 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005488 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005489 return FAIL;
5490
5491 if (fnump != NULL)
5492 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005493 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005494 if (n < 0)
5495 return FAIL;
5496 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005497 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005498 *fnump = n;
5499 }
5500
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005501 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005502 if (n < 0)
5503 return FAIL;
5504 posp->lnum = n;
5505
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005506 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005507 if (n < 0)
5508 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005509 // If character position is specified, then convert to byte position
5510 if (charcol)
5511 {
5512 buf_T *buf;
5513
5514 // Get the text for the specified line in a loaded buffer
5515 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5516 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5517 return FAIL;
5518
Bram Moolenaar91458462021-01-13 20:08:38 +01005519 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005520 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005521 posp->col = n;
5522
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005523 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005524 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005525 posp->coladd = 0;
5526 else
5527 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005528
Bram Moolenaar493c1782014-05-28 14:34:46 +02005529 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005530 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005531
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005532 return OK;
5533}
5534
5535/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 * Get the length of an environment variable name.
5537 * Advance "arg" to the first character after the name.
5538 * Return 0 for error.
5539 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005540 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005541get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542{
5543 char_u *p;
5544 int len;
5545
5546 for (p = *arg; vim_isIDc(*p); ++p)
5547 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005548 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 return 0;
5550
5551 len = (int)(p - *arg);
5552 *arg = p;
5553 return len;
5554}
5555
5556/*
5557 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005558 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 * Return 0 if something is wrong.
5560 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005561 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005562get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563{
5564 char_u *p;
5565 int len;
5566
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005567 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005569 {
5570 if (*p == ':')
5571 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005572 // "s:" is start of "s:var", but "n:" is not and can be used in
5573 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005574 len = (int)(p - *arg);
5575 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5576 || len > 1)
5577 break;
5578 }
5579 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005580 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 return 0;
5582
5583 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005584 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585
5586 return len;
5587}
5588
5589/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005590 * Get the length of the name of a variable or function.
5591 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005593 * Return -1 if curly braces expansion failed.
5594 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 * If the name contains 'magic' {}'s, expand them and return the
5596 * expanded name in an allocated string via 'alias' - caller must free.
5597 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005598 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005599get_name_len(
5600 char_u **arg,
5601 char_u **alias,
5602 int evaluate,
5603 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604{
5605 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 char_u *p;
5607 char_u *expr_start;
5608 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005610 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611
5612 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5613 && (*arg)[2] == (int)KE_SNR)
5614 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005615 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 *arg += 3;
5617 return get_id_len(arg) + 3;
5618 }
5619 len = eval_fname_script(*arg);
5620 if (len > 0)
5621 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005622 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 *arg += len;
5624 }
5625
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005627 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005629 p = find_name_end(*arg, &expr_start, &expr_end,
5630 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 if (expr_start != NULL)
5632 {
5633 char_u *temp_string;
5634
5635 if (!evaluate)
5636 {
5637 len += (int)(p - *arg);
5638 *arg = skipwhite(p);
5639 return len;
5640 }
5641
5642 /*
5643 * Include any <SID> etc in the expanded string:
5644 * Thus the -len here.
5645 */
5646 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5647 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005648 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 *alias = temp_string;
5650 *arg = skipwhite(p);
5651 return (int)STRLEN(temp_string);
5652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653
5654 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005655 // Only give an error when there is something, otherwise it will be
5656 // reported at a higher level.
5657 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005658 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659
5660 return len;
5661}
5662
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005663/*
5664 * Find the end of a variable or function name, taking care of magic braces.
5665 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5666 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005667 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005668 * Return a pointer to just after the name. Equal to "arg" if there is no
5669 * valid name.
5670 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005671 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005672find_name_end(
5673 char_u *arg,
5674 char_u **expr_start,
5675 char_u **expr_end,
5676 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005678 int mb_nest = 0;
5679 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005681 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005682 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005684 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005686 *expr_start = NULL;
5687 *expr_end = NULL;
5688 }
5689
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005690 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005691 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5692 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005693 return arg;
5694
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005695 for (p = arg; *p != NUL
5696 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005697 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005698 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005699 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005700 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005701 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005702 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005703 if (*p == '\'')
5704 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005705 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005706 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005707 ;
5708 if (*p == NUL)
5709 break;
5710 }
5711 else if (*p == '"')
5712 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005713 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005714 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005715 if (*p == '\\' && p[1] != NUL)
5716 ++p;
5717 if (*p == NUL)
5718 break;
5719 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005720 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5721 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005722 // "s:" is start of "s:var", but "n:" is not and can be used in
5723 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005724 len = (int)(p - arg);
5725 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005726 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005727 break;
5728 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005729
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005730 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005732 if (*p == '[')
5733 ++br_nest;
5734 else if (*p == ']')
5735 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005737
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005738 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005740 if (*p == '{')
5741 {
5742 mb_nest++;
5743 if (expr_start != NULL && *expr_start == NULL)
5744 *expr_start = p;
5745 }
5746 else if (*p == '}')
5747 {
5748 mb_nest--;
5749 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5750 *expr_end = p;
5751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 }
5754
5755 return p;
5756}
5757
5758/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005759 * Expands out the 'magic' {}'s in a variable/function name.
5760 * Note that this can call itself recursively, to deal with
5761 * constructs like foo{bar}{baz}{bam}
5762 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5763 * "in_start" ^
5764 * "expr_start" ^
5765 * "expr_end" ^
5766 * "in_end" ^
5767 *
5768 * Returns a new allocated string, which the caller must free.
5769 * Returns NULL for failure.
5770 */
5771 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005772make_expanded_name(
5773 char_u *in_start,
5774 char_u *expr_start,
5775 char_u *expr_end,
5776 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005777{
5778 char_u c1;
5779 char_u *retval = NULL;
5780 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005781
5782 if (expr_end == NULL || in_end == NULL)
5783 return NULL;
5784 *expr_start = NUL;
5785 *expr_end = NUL;
5786 c1 = *in_end;
5787 *in_end = NUL;
5788
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005789 temp_result = eval_to_string(expr_start + 1, FALSE);
5790 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005791 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005792 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5793 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005794 if (retval != NULL)
5795 {
5796 STRCPY(retval, in_start);
5797 STRCAT(retval, temp_result);
5798 STRCAT(retval, expr_end + 1);
5799 }
5800 }
5801 vim_free(temp_result);
5802
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005803 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005804 *expr_start = '{';
5805 *expr_end = '}';
5806
5807 if (retval != NULL)
5808 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005809 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005810 if (expr_start != NULL)
5811 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005812 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005813 temp_result = make_expanded_name(retval, expr_start,
5814 expr_end, temp_result);
5815 vim_free(retval);
5816 retval = temp_result;
5817 }
5818 }
5819
5820 return retval;
5821}
5822
5823/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005825 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005827 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005828eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005830 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005831}
5832
5833/*
5834 * Return TRUE if character "c" can be used as the first character in a
5835 * variable or function name (excluding '{' and '}').
5836 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005837 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005838eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005839{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005840 return ASCII_ISALPHA(c) || c == '_';
5841}
5842
5843/*
5844 * Return TRUE if character "c" can be used as the first character of a
5845 * dictionary key.
5846 */
5847 int
5848eval_isdictc(int c)
5849{
5850 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851}
5852
5853/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005854 * Handle:
5855 * - expr[expr], expr[expr:expr] subscript
5856 * - ".name" lookup
5857 * - function call with Funcref variable: func(expr)
5858 * - method call: var->method()
5859 *
5860 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00005861 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005862 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005863 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005864handle_subscript(
5865 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00005866 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01005867 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005868 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005869 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005870{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005871 int evaluate = evalarg != NULL
5872 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005873 int ret = OK;
5874 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005875 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005876 int getnext;
5877 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005878
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005879 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005880 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005881 // When at the end of the line and ".name" or "->{" or "->X" follows in
5882 // the next line then consume the line break.
5883 p = eval_next_non_blank(*arg, evalarg, &getnext);
5884 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005885 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02005886 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
5887 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
5888 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005889 {
5890 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005891 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005892 check_white = FALSE;
5893 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005894
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01005895 if (rettv->v_type == VAR_ANY)
5896 {
5897 char_u *exp_name;
5898 int cc;
5899 int idx;
5900 ufunc_T *ufunc;
5901 type_T *type;
5902
Bram Moolenaard5f400c2022-01-06 21:10:28 +00005903 // Found script from "import {name} as name", script item name must
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01005904 // follow.
5905 if (**arg != '.')
5906 {
5907 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00005908 semsg(_(e_expected_dot_after_name_str),
5909 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01005910 ret = FAIL;
5911 break;
5912 }
5913 ++*arg;
5914 if (IS_WHITE_OR_NUL(**arg))
5915 {
5916 if (verbose)
5917 emsg(_(e_no_white_space_allowed_after_dot));
5918 ret = FAIL;
5919 break;
5920 }
5921
5922 // isolate the name
5923 exp_name = *arg;
5924 while (eval_isnamec(**arg))
5925 ++*arg;
5926 cc = **arg;
5927 **arg = NUL;
5928
5929 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
5930 evalarg->eval_cctx, verbose);
5931 **arg = cc;
5932 *arg = skipwhite(*arg);
5933
5934 if (idx < 0 && ufunc == NULL)
5935 {
5936 ret = FAIL;
5937 break;
5938 }
5939 if (idx >= 0)
5940 {
5941 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
5942 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
5943
5944 copy_tv(sv->sv_tv, rettv);
5945 }
5946 else
5947 {
5948 rettv->v_type = VAR_FUNC;
5949 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
5950 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00005951 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01005952 }
5953
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005954 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5955 || rettv->v_type == VAR_PARTIAL))
5956 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005957 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005958 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5959 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005960
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005961 // Stop the expression evaluation when immediately aborting on
5962 // error, or when an interrupt occurred or an exception was thrown
5963 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005964 if (aborting())
5965 {
5966 if (ret == OK)
5967 clear_tv(rettv);
5968 ret = FAIL;
5969 }
5970 dict_unref(selfdict);
5971 selfdict = NULL;
5972 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005973 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005974 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005975 if (in_vim9script())
5976 *arg = skipwhite(p + 2);
5977 else
5978 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005979 if (ret == OK)
5980 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005981 if (VIM_ISWHITE(**arg))
5982 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00005983 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005984 ret = FAIL;
5985 }
5986 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005987 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005988 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005989 else
5990 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005991 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005992 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005993 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005994 // "." is ".name" lookup when we found a dict or when evaluating and
5995 // scriptversion is at least 2, where string concatenation is "..".
5996 else if (**arg == '['
5997 || (**arg == '.' && (rettv->v_type == VAR_DICT
5998 || (!evaluate
5999 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006000 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006001 {
6002 dict_unref(selfdict);
6003 if (rettv->v_type == VAR_DICT)
6004 {
6005 selfdict = rettv->vval.v_dict;
6006 if (selfdict != NULL)
6007 ++selfdict->dv_refcount;
6008 }
6009 else
6010 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006011 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006012 {
6013 clear_tv(rettv);
6014 ret = FAIL;
6015 }
6016 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006017 else
6018 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006019 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006020
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006021 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6022 // Don't do this when "Func" is already a partial that was bound
6023 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006024 if (selfdict != NULL
6025 && (rettv->v_type == VAR_FUNC
6026 || (rettv->v_type == VAR_PARTIAL
6027 && (rettv->vval.v_partial->pt_auto
6028 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006029 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006030
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006031 dict_unref(selfdict);
6032 return ret;
6033}
6034
6035/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006036 * Make a copy of an item.
6037 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006038 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6039 * reference to an already copied list/dict can be used.
6040 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006041 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006042 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006043item_copy(
6044 typval_T *from,
6045 typval_T *to,
6046 int deep,
6047 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006048{
6049 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006050 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006051
Bram Moolenaar33570922005-01-25 22:26:29 +00006052 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006053 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006054 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006055 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006056 }
6057 ++recurse;
6058
6059 switch (from->v_type)
6060 {
6061 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006062 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006063 case VAR_STRING:
6064 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006065 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006066 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006067 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006068 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006069 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006070 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006071 copy_tv(from, to);
6072 break;
6073 case VAR_LIST:
6074 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006075 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006076 if (from->vval.v_list == NULL)
6077 to->vval.v_list = NULL;
6078 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6079 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006080 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006081 to->vval.v_list = from->vval.v_list->lv_copylist;
6082 ++to->vval.v_list->lv_refcount;
6083 }
6084 else
6085 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
6086 if (to->vval.v_list == NULL)
6087 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006088 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006089 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006090 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006091 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006092 case VAR_DICT:
6093 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006094 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006095 if (from->vval.v_dict == NULL)
6096 to->vval.v_dict = NULL;
6097 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6098 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006099 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006100 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6101 ++to->vval.v_dict->dv_refcount;
6102 }
6103 else
6104 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
6105 if (to->vval.v_dict == NULL)
6106 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006107 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006108 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006109 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006110 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006111 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006112 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006113 }
6114 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006115 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006116}
6117
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006118 void
6119echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6120{
6121 char_u *tofree;
6122 char_u numbuf[NUMBUFLEN];
6123 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6124
6125 if (*atstart)
6126 {
6127 *atstart = FALSE;
6128 // Call msg_start() after eval1(), evaluating the expression
6129 // may cause a message to appear.
6130 if (with_space)
6131 {
6132 // Mark the saved text as finishing the line, so that what
6133 // follows is displayed on a new line when scrolling back
6134 // at the more prompt.
6135 msg_sb_eol();
6136 msg_start();
6137 }
6138 }
6139 else if (with_space)
6140 msg_puts_attr(" ", echo_attr);
6141
6142 if (p != NULL)
6143 for ( ; *p != NUL && !got_int; ++p)
6144 {
6145 if (*p == '\n' || *p == '\r' || *p == TAB)
6146 {
6147 if (*p != TAB && *needclr)
6148 {
6149 // remove any text still there from the command
6150 msg_clr_eos();
6151 *needclr = FALSE;
6152 }
6153 msg_putchar_attr(*p, echo_attr);
6154 }
6155 else
6156 {
6157 if (has_mbyte)
6158 {
6159 int i = (*mb_ptr2len)(p);
6160
6161 (void)msg_outtrans_len_attr(p, i, echo_attr);
6162 p += i - 1;
6163 }
6164 else
6165 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6166 }
6167 }
6168 vim_free(tofree);
6169}
6170
Bram Moolenaare9a41262005-01-15 22:18:47 +00006171/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 * ":echo expr1 ..." print each argument separated with a space, add a
6173 * newline at the end.
6174 * ":echon expr1 ..." print each argument plain.
6175 */
6176 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006177ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178{
6179 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006180 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006181 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 int needclr = TRUE;
6183 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006184 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006185 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006186 evalarg_T evalarg;
6187
Bram Moolenaare707c882020-07-01 13:07:37 +02006188 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189
6190 if (eap->skip)
6191 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006192 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006194 // If eval1() causes an error message the text from the command may
6195 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006196 need_clr_eos = needclr;
6197
Bram Moolenaar68db9962021-05-09 23:19:22 +02006198 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006199 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 {
6201 /*
6202 * Report the invalid expression unless the expression evaluation
6203 * has been cancelled due to an aborting error, an interrupt, or an
6204 * exception.
6205 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006206 if (!aborting() && did_emsg == did_emsg_before
6207 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006208 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006209 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 break;
6211 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006212 need_clr_eos = FALSE;
6213
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006215 {
6216 if (rettv.v_type == VAR_VOID)
6217 {
6218 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6219 break;
6220 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006221 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006222 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006223
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006224 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 arg = skipwhite(arg);
6226 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006227 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006228 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229
6230 if (eap->skip)
6231 --emsg_skip;
6232 else
6233 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006234 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 if (needclr)
6236 msg_clr_eos();
6237 if (eap->cmdidx == CMD_echo)
6238 msg_end();
6239 }
6240}
6241
6242/*
6243 * ":echohl {name}".
6244 */
6245 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006246ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006248 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249}
6250
6251/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006252 * Returns the :echo attribute
6253 */
6254 int
6255get_echo_attr(void)
6256{
6257 return echo_attr;
6258}
6259
6260/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 * ":execute expr1 ..." execute the result of an expression.
6262 * ":echomsg expr1 ..." Print a message
6263 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006264 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 * Each gets spaces around each argument and a newline at the end for
6266 * echo commands
6267 */
6268 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006269ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270{
6271 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006272 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 int ret = OK;
6274 char_u *p;
6275 garray_T ga;
6276 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006277 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278
6279 ga_init2(&ga, 1, 80);
6280
6281 if (eap->skip)
6282 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006283 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006285 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006286 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288
6289 if (!eap->skip)
6290 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006291 char_u buf[NUMBUFLEN];
6292
6293 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006294 {
6295 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6296 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006297 semsg(_(e_using_invalid_value_as_string_str),
6298 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006299 p = NULL;
6300 }
6301 else
6302 p = tv_get_string_buf(&rettv, buf);
6303 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006304 else
6305 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006306 if (p == NULL)
6307 {
6308 clear_tv(&rettv);
6309 ret = FAIL;
6310 break;
6311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 len = (int)STRLEN(p);
6313 if (ga_grow(&ga, len + 2) == FAIL)
6314 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006315 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 ret = FAIL;
6317 break;
6318 }
6319 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 ga.ga_len += len;
6323 }
6324
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006325 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 arg = skipwhite(arg);
6327 }
6328
6329 if (ret != FAIL && ga.ga_data != NULL)
6330 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006331 // use the first line of continuation lines for messages
6332 SOURCING_LNUM = start_lnum;
6333
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006334 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6335 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006336 // Mark the already saved text as finishing the line, so that what
6337 // follows is displayed on a new line when scrolling back at the
6338 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006339 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006340 }
6341
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006343 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006344 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006345 out_flush();
6346 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006347 else if (eap->cmdidx == CMD_echoconsole)
6348 {
6349 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6350 ui_write((char_u *)"\r\n", 2, TRUE);
6351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 else if (eap->cmdidx == CMD_echoerr)
6353 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006354 int save_did_emsg = did_emsg;
6355
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006356 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006357 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 if (!force_abort)
6359 did_emsg = save_did_emsg;
6360 }
6361 else if (eap->cmdidx == CMD_execute)
6362 do_cmdline((char_u *)ga.ga_data,
6363 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6364 }
6365
6366 ga_clear(&ga);
6367
6368 if (eap->skip)
6369 --emsg_skip;
6370
Bram Moolenaar63b91732021-08-05 20:40:03 +02006371 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372}
6373
6374/*
6375 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6376 * "arg" points to the "&" or '+' when called, to "option" when returning.
6377 * Returns NULL when no option name found. Otherwise pointer to the char
6378 * after the option name.
6379 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006380 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006381find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382{
6383 char_u *p = *arg;
6384
6385 ++p;
6386 if (*p == 'g' && p[1] == ':')
6387 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006388 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 p += 2;
6390 }
6391 else if (*p == 'l' && p[1] == ':')
6392 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006393 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 p += 2;
6395 }
6396 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006397 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398
6399 if (!ASCII_ISALPHA(*p))
6400 return NULL;
6401 *arg = p;
6402
6403 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006404 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 else
6406 while (ASCII_ISALPHA(*p))
6407 ++p;
6408 return p;
6409}
6410
6411/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006412 * Display script name where an item was last set.
6413 * Should only be invoked when 'verbose' is non-zero.
6414 */
6415 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006416last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006417{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006418 char_u *p;
6419
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006420 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006421 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006422 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006423 if (p != NULL)
6424 {
6425 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006426 msg_puts(_("\n\tLast set from "));
6427 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006428 if (script_ctx.sc_lnum > 0)
6429 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006430 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006431 msg_outnum((long)script_ctx.sc_lnum);
6432 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006433 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006434 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006435 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006436 }
6437}
6438
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006439#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440
6441/*
6442 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006443 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 * "flags" can be "g" to do a global substitute.
6445 * Returns an allocated string, NULL for error.
6446 */
6447 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006448do_string_sub(
6449 char_u *str,
6450 char_u *pat,
6451 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006452 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006453 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454{
6455 int sublen;
6456 regmatch_T regmatch;
6457 int i;
6458 int do_all;
6459 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006460 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 garray_T ga;
6462 char_u *ret;
6463 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006464 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006466 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006468 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469
6470 ga_init2(&ga, 1, 200);
6471
6472 do_all = (flags[0] == 'g');
6473
6474 regmatch.rm_ic = p_ic;
6475 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6476 if (regmatch.regprog != NULL)
6477 {
6478 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006479 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6481 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006482 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006483 if (regmatch.startp[0] == regmatch.endp[0])
6484 {
6485 if (zero_width == regmatch.startp[0])
6486 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006487 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006488 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006489 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6490 (size_t)i);
6491 ga.ga_len += i;
6492 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006493 continue;
6494 }
6495 zero_width = regmatch.startp[0];
6496 }
6497
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 /*
6499 * Get some space for a temporary buffer to do the substitution
6500 * into. It will contain:
6501 * - The text up to where the match is.
6502 * - The substituted text.
6503 * - The text after the match.
6504 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006505 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006506 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6508 {
6509 ga_clear(&ga);
6510 break;
6511 }
6512
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006513 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 i = (int)(regmatch.startp[0] - tail);
6515 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006516 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006517 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 + ga.ga_len + i, TRUE, TRUE, FALSE);
6519 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006520 tail = regmatch.endp[0];
6521 if (*tail == NUL)
6522 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 if (!do_all)
6524 break;
6525 }
6526
6527 if (ga.ga_data != NULL)
6528 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6529
Bram Moolenaar473de612013-06-08 18:19:48 +02006530 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 }
6532
6533 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6534 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006535 if (p_cpo == empty_option)
6536 p_cpo = save_cpo;
6537 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006538 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006539 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006540 // If it's still empty it was changed and restored, need to restore in
6541 // the complicated way.
6542 if (*p_cpo == NUL)
6543 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006544 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546
6547 return ret;
6548}