blob: 08804c09243c1d55fc7425623413fe2242f56aef [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010023#define NAMESPACE_CHAR (char_u *)"abglstvw"
24
Bram Moolenaar532c7802005-01-27 14:44:31 +000025/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000026 * When recursively copying lists and dicts we need to remember which ones we
27 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000028 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000029 */
30static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020031
Bram Moolenaar071d4272004-06-13 20:20:40 +000032/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000033 * Info used by a ":for" loop.
34 */
Bram Moolenaar33570922005-01-25 22:26:29 +000035typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000036{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010037 int fi_semicolon; // TRUE if ending in '; var]'
38 int fi_varcount; // nr of variables in the list
Bram Moolenaarb7a78f72020-06-28 18:43:40 +020039 int fi_break_count; // nr of line breaks encountered
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010040 listwatch_T fi_lw; // keep an eye on the item used.
41 list_T *fi_list; // list being used
42 int fi_bi; // index of blob
43 blob_T *fi_blob; // blob being used
Bram Moolenaar74e54fc2021-03-26 20:41:29 +010044 char_u *fi_string; // copy of string being used
45 int fi_byte_idx; // byte index in fi_string
Bram Moolenaar33570922005-01-25 22:26:29 +000046} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000047
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020048static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
49static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
50static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
51static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
52static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +020053static int eval7t(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020054static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +020055static int eval7_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000056
Bram Moolenaar48e697e2016-01-23 22:17:30 +010057static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020058static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020059
Bram Moolenaare21c1582019-03-02 11:57:09 +010060/*
61 * Return "n1" divided by "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010062 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010063 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020064 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010065num_divide(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010066{
67 varnumber_T result;
68
Bram Moolenaar99880f92021-01-20 21:23:14 +010069 if (n2 == 0)
Bram Moolenaare21c1582019-03-02 11:57:09 +010070 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010071 if (in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010072 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010073 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010074 if (failed != NULL)
75 *failed = TRUE;
76 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010077 if (n1 == 0)
78 result = VARNUM_MIN; // similar to NaN
79 else if (n1 < 0)
80 result = -VARNUM_MAX;
81 else
82 result = VARNUM_MAX;
83 }
84 else
85 result = n1 / n2;
86
87 return result;
88}
89
90/*
91 * Return "n1" modulus "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010092 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010093 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020094 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010095num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010096{
Bram Moolenaar99880f92021-01-20 21:23:14 +010097 if (n2 == 0 && in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010098 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010099 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +0100100 if (failed != NULL)
101 *failed = TRUE;
102 }
Bram Moolenaare21c1582019-03-02 11:57:09 +0100103 return (n2 == 0) ? 0 : (n1 % n2);
104}
105
Bram Moolenaar33570922005-01-25 22:26:29 +0000106/*
107 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000108 */
109 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100110eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000111{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200112 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200113 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000114
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200115#ifdef EBCDIC
116 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100117 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200118 */
119 sortFunctions();
120#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000121}
122
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000123#if defined(EXITFREE) || defined(PROTO)
124 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100125eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000126{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200127 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100128 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200129 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000130
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200131 // autoloaded script names
132 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000133
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200134 // unreferenced lists and dicts
135 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200136
137 // functions not garbage collected
138 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000139}
140#endif
141
Bram Moolenaare6b53242020-07-01 17:28:33 +0200142 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200143fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
144{
Bram Moolenaar844fb642021-10-23 13:32:30 +0100145 init_evalarg(evalarg);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200146 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200147 if (eap != NULL)
Bram Moolenaar37c83712020-06-30 21:18:36 +0200148 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200149 evalarg->eval_cstack = eap->cstack;
150 if (getline_equal(eap->getline, eap->cookie, getsourceline))
151 {
152 evalarg->eval_getline = eap->getline;
153 evalarg->eval_cookie = eap->cookie;
154 }
Bram Moolenaar37c83712020-06-30 21:18:36 +0200155 }
156}
157
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158/*
159 * Top level evaluation function, returning a boolean.
160 * Sets "error" to TRUE if there was an error.
161 * Return TRUE or FALSE.
162 */
163 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100164eval_to_bool(
165 char_u *arg,
166 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200167 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100168 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169{
Bram Moolenaar33570922005-01-25 22:26:29 +0000170 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200171 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200172 evalarg_T evalarg;
173
Bram Moolenaar37c83712020-06-30 21:18:36 +0200174 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175
176 if (skip)
177 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200178 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 else
181 {
182 *error = FALSE;
183 if (!skip)
184 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200185 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200186 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200187 else
188 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000189 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 }
191 }
192 if (skip)
193 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200194 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200196 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197}
198
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100199/*
200 * Call eval1() and give an error message if not done at a lower level.
201 */
202 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200203eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100204{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100205 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100206 int ret;
207 int did_emsg_before = did_emsg;
208 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200209 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100210
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200211 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
212
213 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100214 if (ret == FAIL)
215 {
216 // Report the invalid expression unless the expression evaluation has
217 // been cancelled due to an aborting error, an interrupt, or an
218 // exception, or we already gave a more specific error.
219 // Also check called_emsg for when using assert_fails().
220 if (!aborting() && did_emsg == did_emsg_before
221 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200222 semsg(_(e_invalid_expression_str), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100223 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200224 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100225 return ret;
226}
227
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100228/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200229 * Return whether a typval is a valid expression to pass to eval_expr_typval()
230 * or eval_expr_to_bool(). An empty string returns FALSE;
231 */
232 int
233eval_expr_valid_arg(typval_T *tv)
234{
235 return tv->v_type != VAR_UNKNOWN
236 && (tv->v_type != VAR_STRING
237 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
238}
239
240/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100241 * Evaluate an expression, which can be a function, partial or string.
242 * Pass arguments "argv[argc]".
243 * Return the result in "rettv" and OK or FAIL.
244 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200245 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100246eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
247{
248 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100249 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200250 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100251
252 if (expr->v_type == VAR_FUNC)
253 {
254 s = expr->vval.v_string;
255 if (s == NULL || *s == NUL)
256 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200257 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000258 funcexe.fe_evaluate = TRUE;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200259 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100260 return FAIL;
261 }
262 else if (expr->v_type == VAR_PARTIAL)
263 {
264 partial_T *partial = expr->vval.v_partial;
265
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200266 if (partial == NULL)
267 return FAIL;
268
Bram Moolenaar822ba242020-05-24 23:00:18 +0200269 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200270 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200272 if (call_def_function(partial->pt_func, argc, argv,
273 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100274 return FAIL;
275 }
276 else
277 {
278 s = partial_name(partial);
279 if (s == NULL || *s == NUL)
280 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200281 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000282 funcexe.fe_evaluate = TRUE;
283 funcexe.fe_partial = partial;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100284 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
285 return FAIL;
286 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100287 }
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200288 else if (expr->v_type == VAR_INSTR)
289 {
290 return exe_typval_instr(expr, rettv);
291 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100292 else
293 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100294 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100295 if (s == NULL)
296 return FAIL;
297 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200298 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100299 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200300 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100301 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200302 clear_tv(rettv);
Bram Moolenaar108010a2021-06-27 22:03:33 +0200303 semsg(_(e_invalid_expression_str), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100304 return FAIL;
305 }
306 }
307 return OK;
308}
309
310/*
311 * Like eval_to_bool() but using a typval_T instead of a string.
312 * Works for string, funcref and partial.
313 */
314 int
315eval_expr_to_bool(typval_T *expr, int *error)
316{
317 typval_T rettv;
318 int res;
319
320 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
321 {
322 *error = TRUE;
323 return FALSE;
324 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200325 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100326 clear_tv(&rettv);
327 return res;
328}
329
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330/*
331 * Top level evaluation function, returning a string. If "skip" is TRUE,
332 * only parsing to "nextcmd" is done, without reporting errors. Return
333 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
334 */
335 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100336eval_to_string_skip(
337 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200338 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100339 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340{
Bram Moolenaar33570922005-01-25 22:26:29 +0000341 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200343 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344
Bram Moolenaar37c83712020-06-30 21:18:36 +0200345 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 if (skip)
347 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200348 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 retval = NULL;
350 else
351 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100352 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000353 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 }
355 if (skip)
356 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200357 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358
359 return retval;
360}
361
362/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000363 * Skip over an expression at "*pp".
364 * Return FAIL for an error, OK otherwise.
365 */
366 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200367skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000368{
Bram Moolenaar33570922005-01-25 22:26:29 +0000369 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000370
371 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200372 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000373}
374
375/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200376 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200377 * If in Vim9 script and line breaks are encountered, the lines are
378 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200379 * "arg" is advanced to just after the expression.
380 * "start" is set to the start of the expression, "end" to just after the end.
381 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200382 * Return FAIL for an error, OK otherwise.
383 */
384 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200385skip_expr_concatenate(
386 char_u **arg,
387 char_u **start,
388 char_u **end,
389 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200390{
391 typval_T rettv;
392 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200393 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200394 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200395 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200396 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200397 int evaluate = evalarg == NULL
398 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200399
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200400 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200401 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200402 {
403 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200404 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200405 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200406 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200407 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200408 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200409 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200410
411 // Don't evaluate the expression.
412 if (evalarg != NULL)
413 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200414 *arg = skipwhite(*arg);
415 res = eval1(arg, &rettv, evalarg);
416 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200417 if (evalarg != NULL)
418 evalarg->eval_flags = save_flags;
419
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200420 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200421 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200422 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200423 if (evalarg->eval_ga.ga_len == 1)
424 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200425 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200426 ga_clear(gap);
427 gap->ga_itemsize = 0;
428 }
429 else
430 {
431 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200432 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200433
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200434 // Line breaks encountered, concatenate all the lines.
435 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200436 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200437
438 // free the lines only when using getsourceline()
439 if (evalarg->eval_cookie != NULL)
440 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200441 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200442 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200443 // Do not free the last line, "arg" points into it, free it
444 // later.
445 vim_free(evalarg->eval_tofree);
446 evalarg->eval_tofree =
447 ((char_u **)gap->ga_data)[gap->ga_len - 1];
448 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200449 ga_clear_strings(gap);
450 }
451 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200452 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200453 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200454
455 // free lines that were explicitly marked for freeing
456 ga_clear_strings(freegap);
457 }
458
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200459 gap->ga_itemsize = 0;
460 if (p == NULL)
461 return FAIL;
462 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200463 vim_free(evalarg->eval_tofree_lambda);
464 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200465 // Compute "end" relative to the end.
466 *end = *start + STRLEN(*start) - endoff;
467 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200468 }
469
470 return res;
471}
472
473/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100474 * Convert "tv" to a string.
475 * When "convert" is TRUE convert a List into a sequence of lines and convert
476 * a Float to a String.
477 * Returns an allocated string (NULL when out of memory).
478 */
479 char_u *
480typval2string(typval_T *tv, int convert)
481{
482 garray_T ga;
483 char_u *retval;
484#ifdef FEAT_FLOAT
485 char_u numbuf[NUMBUFLEN];
486#endif
487
488 if (convert && tv->v_type == VAR_LIST)
489 {
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000490 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaar883cf972021-01-15 18:04:43 +0100491 if (tv->vval.v_list != NULL)
492 {
493 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
494 if (tv->vval.v_list->lv_len > 0)
495 ga_append(&ga, NL);
496 }
497 ga_append(&ga, NUL);
498 retval = (char_u *)ga.ga_data;
499 }
500#ifdef FEAT_FLOAT
501 else if (convert && tv->v_type == VAR_FLOAT)
502 {
503 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
504 retval = vim_strsave(numbuf);
505 }
506#endif
507 else
508 retval = vim_strsave(tv_get_string(tv));
509 return retval;
510}
511
512/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200513 * Top level evaluation function, returning a string. Does not handle line
514 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000515 * When "convert" is TRUE convert a List into a sequence of lines and convert
516 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 * Return pointer to allocated memory, or NULL for failure.
518 */
519 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100520eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100521 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100522 int convert,
523 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524{
Bram Moolenaar33570922005-01-25 22:26:29 +0000525 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100527 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100529 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
530 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 retval = NULL;
532 else
533 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100534 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000535 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100537 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538
539 return retval;
540}
541
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100542 char_u *
543eval_to_string(
544 char_u *arg,
545 int convert)
546{
547 return eval_to_string_eap(arg, convert, NULL);
548}
549
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000551 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200552 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200553 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 */
555 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100556eval_to_string_safe(
557 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100558 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559{
560 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200561 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200562 int save_sc_version = current_sctx.sc_version;
Christian Brabandt070ac342021-09-09 12:12:03 +0200563 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200565 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200566 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000567 if (use_sandbox)
568 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200569 ++textwinlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200570 may_garbage_collect = FALSE;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200571 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000572 if (use_sandbox)
573 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200574 --textwinlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200575 may_garbage_collect = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200576 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200577 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 return retval;
579}
580
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581/*
582 * Top level evaluation function, returning a number.
583 * Evaluates "expr" silently.
584 * Returns -1 for an error.
585 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200586 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100587eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588{
Bram Moolenaar33570922005-01-25 22:26:29 +0000589 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200590 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000591 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592
593 ++emsg_off;
594
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200595 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 retval = -1;
597 else
598 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100599 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000600 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 }
602 --emsg_off;
603
604 return retval;
605}
606
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000607/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000608 * Top level evaluation function.
609 * Returns an allocated typval_T with the result.
610 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000611 */
612 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200613eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000614{
615 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200616 evalarg_T evalarg;
617
618 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000619
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200620 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200621 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100622 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000623
Bram Moolenaar37c83712020-06-30 21:18:36 +0200624 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000625 return tv;
626}
627
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628/*
Bram 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 {
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000889 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name,
890 TRUE, NULL);
891
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000892 if (import != NULL)
893 {
894 ufunc_T *ufunc;
895 type_T *type;
896
897 lp->ll_sid = import->imp_sid;
898 lp->ll_name = skipwhite(p + 1);
899 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
900 lp->ll_name_end = p;
901
902 // check the item is exported
903 cc = *p;
904 *p = NUL;
905 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
906 NULL, TRUE) == -1)
907 {
908 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +0000909 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000910 }
911 *p = cc;
912 }
913 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100914
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100915 // Without [idx] or .key we are done.
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000916 if ((*p != '[' && *p != '.'))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000917 return p;
918
Bram Moolenaaraacc9662021-08-13 19:40:51 +0200919 if (in_vim9script() && lval_root != NULL)
920 {
921 // using local variable
922 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +0200923 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +0200924 }
925 else
926 {
927 cc = *p;
928 *p = NUL;
929 // When we would write to the variable pass &ht and prevent autoload.
930 writing = !(flags & GLV_READ_ONLY);
931 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100932 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +0200933 if (v == NULL && !quiet)
934 semsg(_(e_undefined_variable_str), lp->ll_name);
935 *p = cc;
936 if (v == NULL)
937 return NULL;
938 lp->ll_tv = &v->di_tv;
939 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000940
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +0100941 if (in_vim9script() && (flags & GLV_NO_DECL) == 0)
942 {
943 if (!quiet)
944 semsg(_(e_variable_already_declared), lp->ll_name);
945 return NULL;
946 }
947
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000948 /*
949 * Loop until no more [idx] or .key is following.
950 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100951 var1.v_type = VAR_UNKNOWN;
952 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +0200953 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000954 {
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +0200955 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
956 {
957 if (!quiet)
958 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
959 return NULL;
960 }
Bram Moolenaare65081d2021-06-27 15:04:05 +0200961 if (lp->ll_tv->v_type != VAR_LIST
962 && lp->ll_tv->v_type != VAR_DICT
963 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000964 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000965 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +0000966 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000967 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000968 }
Bram Moolenaare65081d2021-06-27 15:04:05 +0200969
970 // a NULL list/blob works like an empty list/blob, allocate one now.
971 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
972 rettv_list_alloc(lp->ll_tv);
973 else if (lp->ll_tv->v_type == VAR_BLOB
974 && lp->ll_tv->vval.v_blob == NULL)
975 rettv_blob_alloc(lp->ll_tv);
976
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000977 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000978 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000979 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +0000980 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000981 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000982 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000983
Bram Moolenaar10c65862020-10-08 21:16:42 +0200984 if (in_vim9script() && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +0200985 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +0200986 && lp->ll_tv == &v->di_tv
987 && ht != NULL && ht == get_script_local_ht())
988 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000989 svar_T *sv = find_typval_in_script(lp->ll_tv, 0);
Bram Moolenaar10c65862020-10-08 21:16:42 +0200990
991 // Vim9 script local variable: get the type
992 if (sv != NULL)
993 lp->ll_valtype = sv->sv_type;
994 }
995
Bram Moolenaar8c711452005-01-14 21:53:12 +0000996 len = -1;
997 if (*p == '.')
998 {
999 key = p + 1;
1000 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1001 ;
1002 if (len == 0)
1003 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001004 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001005 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001006 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001007 }
1008 p = key + len;
1009 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001010 else
1011 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001012 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001013 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001014 if (*p == ':')
1015 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001016 else
1017 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001018 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001019 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001020 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001021 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001022 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001023 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001024 clear_tv(&var1);
1025 return NULL;
1026 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001027 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001028 }
1029
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001030 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001031 if (*p == ':')
1032 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001033 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001034 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001035 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001036 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001037 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001038 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001039 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001040 if (rettv != NULL
1041 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001042 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001043 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001044 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001045 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001046 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001047 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001048 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001049 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001050 }
1051 p = skipwhite(p + 1);
1052 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001053 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001054 else
1055 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001056 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001057 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001058 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001059 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001060 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001061 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001062 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001063 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001064 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001065 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001066 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001067 clear_tv(&var2);
1068 return NULL;
1069 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001070 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001071 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001072 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001073 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001074 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001075
Bram Moolenaar8c711452005-01-14 21:53:12 +00001076 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001077 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001078 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001079 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001080 clear_tv(&var1);
1081 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001082 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001083 }
1084
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001085 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001086 ++p;
1087 }
1088
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001089 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001090 {
1091 if (len == -1)
1092 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001093 // "[key]": get key from "var1"
1094 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001095 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001096 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001097 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001098 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001099 }
1100 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001101 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001102
1103 // a NULL dict is equivalent with an empty dict
1104 if (lp->ll_tv->vval.v_dict == NULL)
1105 {
1106 lp->ll_tv->vval.v_dict = dict_alloc();
1107 if (lp->ll_tv->vval.v_dict == NULL)
1108 {
1109 clear_tv(&var1);
1110 return NULL;
1111 }
1112 ++lp->ll_tv->vval.v_dict->dv_refcount;
1113 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001114 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001115
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001116 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001117
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001118 // When assigning to a scope dictionary check that a function and
1119 // variable name is valid (only variable name unless it is l: or
1120 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001121 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001122 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001123 int prevval;
1124 int wrong;
1125
1126 if (len != -1)
1127 {
1128 prevval = key[len];
1129 key[len] = NUL;
1130 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001131 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001132 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001133 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1134 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001135 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001136 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001137 if (len != -1)
1138 key[len] = prevval;
1139 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001140 {
1141 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001142 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001143 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001144 }
1145
Bram Moolenaar10c65862020-10-08 21:16:42 +02001146 if (lp->ll_valtype != NULL)
1147 // use the type of the member
1148 lp->ll_valtype = lp->ll_valtype->tt_member;
1149
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001150 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001151 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001152 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001153 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001154 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001155 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001156 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001157 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001158 return NULL;
1159 }
1160
Bram Moolenaar31b81602019-02-10 22:14:27 +01001161 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001162 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001163 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001164 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001165 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001166 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001167 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001168 }
1169 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001170 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001171 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001172 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001173 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001174 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001175 p = NULL;
1176 break;
1177 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001178 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001179 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001180 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1181 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001182 {
1183 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001184 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001185 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001186
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001187 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001188 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001189 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001190 else if (lp->ll_tv->v_type == VAR_BLOB)
1191 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001192 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1193
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001194 /*
1195 * Get the number and item for the only or first index of the List.
1196 */
1197 if (empty1)
1198 lp->ll_n1 = 0;
1199 else
1200 // is number or string
1201 lp->ll_n1 = (long)tv_get_number(&var1);
1202 clear_tv(&var1);
1203
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001204 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001205 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001206 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001207 return NULL;
1208 }
1209 if (lp->ll_range && !lp->ll_empty2)
1210 {
1211 lp->ll_n2 = (long)tv_get_number(&var2);
1212 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001213 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1214 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001215 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001216 }
1217 lp->ll_blob = lp->ll_tv->vval.v_blob;
1218 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001219 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001220 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001221 else
1222 {
1223 /*
1224 * Get the number and item for the only or first index of the List.
1225 */
1226 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001227 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001228 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001229 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001230 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001231 clear_tv(&var1);
1232
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001233 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001234 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001235 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001236 if (lp->ll_li == NULL)
1237 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001238 clear_tv(&var2);
1239 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001240 }
1241
Bram Moolenaar10c65862020-10-08 21:16:42 +02001242 if (lp->ll_valtype != NULL)
1243 // use the type of the member
1244 lp->ll_valtype = lp->ll_valtype->tt_member;
1245
Bram Moolenaar8c711452005-01-14 21:53:12 +00001246 /*
1247 * May need to find the item or absolute index for the second
1248 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001249 * When no index given: "lp->ll_empty2" is TRUE.
1250 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001251 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001252 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001253 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001254 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001255 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001256 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001257 if (check_range_index_two(lp->ll_list,
1258 &lp->ll_n1, lp->ll_li,
1259 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001260 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001261 }
1262
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001263 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001264 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001265 }
1266
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001267 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001268 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001269 return p;
1270}
1271
1272/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001273 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001274 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001275 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001276clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001277{
1278 vim_free(lp->ll_exp_name);
1279 vim_free(lp->ll_newkey);
1280}
1281
1282/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001283 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001284 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001285 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1286 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001287 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001288 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001289set_var_lval(
1290 lval_T *lp,
1291 char_u *endp,
1292 typval_T *rettv,
1293 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001294 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1295 char_u *op,
1296 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001297{
1298 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001299 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001300
1301 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001302 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001303 cc = *endp;
1304 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001305 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1306 return;
1307
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001308 if (lp->ll_blob != NULL)
1309 {
1310 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001311
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001312 if (op != NULL && *op != '=')
1313 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001314 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001315 return;
1316 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001317 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001318 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001319
1320 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1321 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001322 if (lp->ll_empty2)
1323 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1324
Bram Moolenaar68452172021-04-12 21:21:02 +02001325 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1326 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001327 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001328 }
1329 else
1330 {
1331 val = (int)tv_get_number_chk(rettv, &error);
1332 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001333 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001334 }
1335 }
1336 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001337 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001338 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001339
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001340 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1341 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001342 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001343 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001344 *endp = cc;
1345 return;
1346 }
1347
Bram Moolenaarff697e62019-02-12 22:28:33 +01001348 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001349 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001350 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001351 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001352 {
1353 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001354 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1355 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001356 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001357 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001358 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001359 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001360 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001361 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001362 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001363 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001364 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1365 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001366 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001367 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001368 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001369 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001370 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001371 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001372 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001373 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001374 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001375 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001376 else if (lp->ll_range)
1377 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001378 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1379 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001380 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001381 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001382 return;
1383 }
1384
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001385 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1386 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001387 }
1388 else
1389 {
1390 /*
1391 * Assign to a List or Dictionary item.
1392 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001393 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1394 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001395 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001396 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001397 return;
1398 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001399
1400 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001401 && check_typval_arg_type(lp->ll_valtype, rettv,
1402 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001403 return;
1404
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001405 if (lp->ll_newkey != NULL)
1406 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001407 if (op != NULL && *op != '=')
1408 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001409 semsg(_(e_key_not_present_in_dictionary), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001410 return;
1411 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001412 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1413 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001414 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001415
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001416 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001417 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001418 if (di == NULL)
1419 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001420 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1421 {
1422 vim_free(di);
1423 return;
1424 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001425 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001426 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001427 else if (op != NULL && *op != '=')
1428 {
1429 tv_op(lp->ll_tv, rettv, op);
1430 return;
1431 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001432 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001433 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001434
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001435 /*
1436 * Assign the value to the variable or list item.
1437 */
1438 if (copy)
1439 copy_tv(rettv, lp->ll_tv);
1440 else
1441 {
1442 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001443 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001444 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001445 }
1446 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001447}
1448
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001449/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001450 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1451 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001452 * Returns OK or FAIL.
1453 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001454 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001455tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001456{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001457 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001458 char_u numbuf[NUMBUFLEN];
1459 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001460 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001461
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001462 // Can't do anything with a Funcref or Dict on the right.
1463 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001464 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001465 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1466 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001467 {
1468 switch (tv1->v_type)
1469 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001470 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001471 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001472 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001473 case VAR_DICT:
1474 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001475 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001476 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001477 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001478 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001479 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001480 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001481 break;
1482
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001483 case VAR_BLOB:
1484 if (*op != '+' || tv2->v_type != VAR_BLOB)
1485 break;
1486 // BLOB += BLOB
1487 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1488 {
1489 blob_T *b1 = tv1->vval.v_blob;
1490 blob_T *b2 = tv2->vval.v_blob;
1491 int i, len = blob_len(b2);
1492 for (i = 0; i < len; i++)
1493 ga_append(&b1->bv_ga, blob_get(b2, i));
1494 }
1495 return OK;
1496
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001497 case VAR_LIST:
1498 if (*op != '+' || tv2->v_type != VAR_LIST)
1499 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001500 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001501 if (tv2->vval.v_list != NULL)
1502 {
1503 if (tv1->vval.v_list == NULL)
1504 {
1505 tv1->vval.v_list = tv2->vval.v_list;
1506 ++tv1->vval.v_list->lv_refcount;
1507 }
1508 else
1509 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1510 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001511 return OK;
1512
1513 case VAR_NUMBER:
1514 case VAR_STRING:
1515 if (tv2->v_type == VAR_LIST)
1516 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001517 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001518 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001519 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001520 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001521#ifdef FEAT_FLOAT
1522 if (tv2->v_type == VAR_FLOAT)
1523 {
1524 float_T f = n;
1525
Bram Moolenaarff697e62019-02-12 22:28:33 +01001526 if (*op == '%')
1527 break;
1528 switch (*op)
1529 {
1530 case '+': f += tv2->vval.v_float; break;
1531 case '-': f -= tv2->vval.v_float; break;
1532 case '*': f *= tv2->vval.v_float; break;
1533 case '/': f /= tv2->vval.v_float; break;
1534 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001535 clear_tv(tv1);
1536 tv1->v_type = VAR_FLOAT;
1537 tv1->vval.v_float = f;
1538 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001539 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001540#endif
1541 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001542 switch (*op)
1543 {
1544 case '+': n += tv_get_number(tv2); break;
1545 case '-': n -= tv_get_number(tv2); break;
1546 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001547 case '/': n = num_divide(n, tv_get_number(tv2),
1548 &failed); break;
1549 case '%': n = num_modulus(n, tv_get_number(tv2),
1550 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001551 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001552 clear_tv(tv1);
1553 tv1->v_type = VAR_NUMBER;
1554 tv1->vval.v_number = n;
1555 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001556 }
1557 else
1558 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001559 if (tv2->v_type == VAR_FLOAT)
1560 break;
1561
Bram Moolenaarff697e62019-02-12 22:28:33 +01001562 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001563 s = tv_get_string(tv1);
1564 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001565 clear_tv(tv1);
1566 tv1->v_type = VAR_STRING;
1567 tv1->vval.v_string = s;
1568 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001569 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001570
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001571 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001572#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001573 {
1574 float_T f;
1575
Bram Moolenaarff697e62019-02-12 22:28:33 +01001576 if (*op == '%' || *op == '.'
1577 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001578 && tv2->v_type != VAR_NUMBER
1579 && tv2->v_type != VAR_STRING))
1580 break;
1581 if (tv2->v_type == VAR_FLOAT)
1582 f = tv2->vval.v_float;
1583 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001584 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001585 switch (*op)
1586 {
1587 case '+': tv1->vval.v_float += f; break;
1588 case '-': tv1->vval.v_float -= f; break;
1589 case '*': tv1->vval.v_float *= f; break;
1590 case '/': tv1->vval.v_float /= f; break;
1591 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001592 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001593#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001594 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001595 }
1596 }
1597
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001598 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001599 return FAIL;
1600}
1601
1602/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001603 * Evaluate the expression used in a ":for var in expr" command.
1604 * "arg" points to "var".
1605 * Set "*errp" to TRUE for an error, FALSE otherwise;
1606 * Return a pointer that holds the info. Null when there is an error.
1607 */
1608 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001609eval_for_line(
1610 char_u *arg,
1611 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001612 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001613 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001614{
Bram Moolenaar33570922005-01-25 22:26:29 +00001615 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001616 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001617 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001618 typval_T tv;
1619 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001620 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001621
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001622 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001623
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001624 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001625 if (fi == NULL)
1626 return NULL;
1627
Bram Moolenaar404557e2021-07-05 21:41:48 +02001628 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1629 &fi->fi_semicolon, FALSE);
1630 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001631 return fi;
1632
Bram Moolenaar404557e2021-07-05 21:41:48 +02001633 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001634 if (expr[0] != 'i' || expr[1] != 'n'
1635 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001636 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001637 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1638 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1639 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001640 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001641 return fi;
1642 }
1643
1644 if (skip)
1645 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001646 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1647 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001648 {
1649 *errp = FALSE;
1650 if (!skip)
1651 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001652 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001653 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001654 l = tv.vval.v_list;
1655 if (l == NULL)
1656 {
1657 // a null list is like an empty list: do nothing
1658 clear_tv(&tv);
1659 }
1660 else
1661 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001662 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001663 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001664
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001665 // No need to increment the refcount, it's already set for
1666 // the list being used in "tv".
1667 fi->fi_list = l;
1668 list_add_watch(l, &fi->fi_lw);
1669 fi->fi_lw.lw_item = l->lv_first;
1670 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001671 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001672 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001673 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001674 fi->fi_bi = 0;
1675 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001676 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001677 typval_T btv;
1678
1679 // Make a copy, so that the iteration still works when the
1680 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001681 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001682 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001683 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001684 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001685 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001686 else if (tv.v_type == VAR_STRING)
1687 {
1688 fi->fi_byte_idx = 0;
1689 fi->fi_string = tv.vval.v_string;
1690 tv.vval.v_string = NULL;
1691 if (fi->fi_string == NULL)
1692 fi->fi_string = vim_strsave((char_u *)"");
1693 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001694 else
1695 {
Sean Dewar80d73952021-08-04 19:25:54 +02001696 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001697 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001698 }
1699 }
1700 }
1701 if (skip)
1702 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001703 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001704
1705 return fi;
1706}
1707
1708/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001709 * Used when looping over a :for line, skip the "in expr" part.
1710 */
1711 void
1712skip_for_lines(void *fi_void, evalarg_T *evalarg)
1713{
1714 forinfo_T *fi = (forinfo_T *)fi_void;
1715 int i;
1716
1717 for (i = 0; i < fi->fi_break_count; ++i)
1718 eval_next_line(evalarg);
1719}
1720
1721/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001722 * Use the first item in a ":for" list. Advance to the next.
1723 * Assign the values to the variable (list). "arg" points to the first one.
1724 * Return TRUE when a valid item was found, FALSE when at end of list or
1725 * something wrong.
1726 */
1727 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001728next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001729{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001730 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001731 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001732 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001733 ? (ASSIGN_FINAL
1734 // first round: error if variable exists
1735 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
1736 | ASSIGN_NO_MEMBER_TYPE)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001737 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001738 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001739 int skip_assign = in_vim9script() && arg[0] == '_'
1740 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001741
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001742 if (fi->fi_blob != NULL)
1743 {
1744 typval_T tv;
1745
1746 if (fi->fi_bi >= blob_len(fi->fi_blob))
1747 return FALSE;
1748 tv.v_type = VAR_NUMBER;
1749 tv.v_lock = VAR_FIXED;
1750 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1751 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001752 if (skip_assign)
1753 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001754 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001755 fi->fi_varcount, flag, NULL) == OK;
1756 }
1757
1758 if (fi->fi_string != NULL)
1759 {
1760 typval_T tv;
1761 int len;
1762
1763 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1764 if (len == 0)
1765 return FALSE;
1766 tv.v_type = VAR_STRING;
1767 tv.v_lock = VAR_FIXED;
1768 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1769 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001770 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001771 if (skip_assign)
1772 result = TRUE;
1773 else
1774 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001775 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001776 vim_free(tv.vval.v_string);
1777 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001778 }
1779
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001780 item = fi->fi_lw.lw_item;
1781 if (item == NULL)
1782 result = FALSE;
1783 else
1784 {
1785 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001786 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001787 if (skip_assign)
1788 result = TRUE;
1789 else
1790 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001791 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001792 }
1793 return result;
1794}
1795
1796/*
1797 * Free the structure used to store info used by ":for".
1798 */
1799 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001800free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001801{
Bram Moolenaar33570922005-01-25 22:26:29 +00001802 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001803
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001804 if (fi == NULL)
1805 return;
1806 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001807 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001808 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001809 list_unref(fi->fi_list);
1810 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001811 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001812 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001813 else
1814 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001815 vim_free(fi);
1816}
1817
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001819set_context_for_expression(
1820 expand_T *xp,
1821 char_u *arg,
1822 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001824 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001826 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001828 if (cmdidx == CMD_let || cmdidx == CMD_var
1829 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001830 {
1831 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001832 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001833 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001834 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001835 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001836 {
1837 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001838 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001839 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001840 break;
1841 }
1842 return;
1843 }
1844 }
1845 else
1846 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1847 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 while ((xp->xp_pattern = vim_strpbrk(arg,
1849 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1850 {
1851 c = *xp->xp_pattern;
1852 if (c == '&')
1853 {
1854 c = xp->xp_pattern[1];
1855 if (c == '&')
1856 {
1857 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001858 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 }
1860 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001861 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001863 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1864 xp->xp_pattern += 2;
1865
1866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 }
1868 else if (c == '$')
1869 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001870 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 xp->xp_context = EXPAND_ENV_VARS;
1872 }
1873 else if (c == '=')
1874 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001875 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 xp->xp_context = EXPAND_EXPRESSION;
1877 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001878 else if (c == '#'
1879 && xp->xp_context == EXPAND_EXPRESSION)
1880 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001881 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001882 break;
1883 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001884 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 && xp->xp_context == EXPAND_FUNCTIONS
1886 && vim_strchr(xp->xp_pattern, '(') == NULL)
1887 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001888 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 break;
1890 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001891 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001893 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 {
1895 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1896 if (c == '\\' && xp->xp_pattern[1] != NUL)
1897 ++xp->xp_pattern;
1898 xp->xp_context = EXPAND_NOTHING;
1899 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001900 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001902 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1904 /* skip */ ;
1905 xp->xp_context = EXPAND_NOTHING;
1906 }
1907 else if (c == '|')
1908 {
1909 if (xp->xp_pattern[1] == '|')
1910 {
1911 ++xp->xp_pattern;
1912 xp->xp_context = EXPAND_EXPRESSION;
1913 }
1914 else
1915 xp->xp_context = EXPAND_COMMANDS;
1916 }
1917 else
1918 xp->xp_context = EXPAND_EXPRESSION;
1919 }
1920 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001921 // Doesn't look like something valid, expand as an expression
1922 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001923 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 arg = xp->xp_pattern;
1925 if (*arg != NUL)
1926 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1927 /* skip */ ;
1928 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01001929
1930 // ":exe one two" completes "two"
1931 if ((cmdidx == CMD_execute
1932 || cmdidx == CMD_echo
1933 || cmdidx == CMD_echon
1934 || cmdidx == CMD_echomsg)
1935 && xp->xp_context == EXPAND_EXPRESSION)
1936 {
1937 for (;;)
1938 {
1939 char_u *n = skiptowhite(arg);
1940
1941 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
1942 break;
1943 arg = skipwhite(n);
1944 }
1945 }
1946
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 xp->xp_pattern = arg;
1948}
1949
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001951 * Return TRUE if "pat" matches "text".
1952 * Does not use 'cpo' and always uses 'magic'.
1953 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001954 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001955pattern_match(char_u *pat, char_u *text, int ic)
1956{
1957 int matches = FALSE;
1958 char_u *save_cpo;
1959 regmatch_T regmatch;
1960
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001961 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001962 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01001963 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001964 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1965 if (regmatch.regprog != NULL)
1966 {
1967 regmatch.rm_ic = ic;
1968 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1969 vim_regfree(regmatch.regprog);
1970 }
1971 p_cpo = save_cpo;
1972 return matches;
1973}
1974
1975/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001976 * Handle a name followed by "(". Both for just "name(arg)" and for
1977 * "expr->name(arg)".
1978 * Returns OK or FAIL.
1979 */
1980 static int
1981eval_func(
1982 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02001983 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001984 char_u *name,
1985 int name_len,
1986 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001987 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001988 typval_T *basetv) // "expr" for "expr->name(arg)"
1989{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001990 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001991 char_u *s = name;
1992 int len = name_len;
1993 partial_T *partial;
1994 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001995 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001996 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001997
1998 if (!evaluate)
1999 check_vars(s, len);
2000
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002001 // If "s" is the name of a variable of type VAR_FUNC
2002 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002003 s = deref_func_name(s, &len, &partial,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002004 in_vim9script() ? &type : NULL, !evaluate, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002005
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002006 // Need to make a copy, in case evaluating the arguments makes
2007 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002008 s = vim_strsave(s);
Bram Moolenaarb3341372021-12-14 18:57:45 +00002009 if (s == NULL || (evaluate && (*s == NUL || (flags & EVAL_CONSTANT))))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002010 ret = FAIL;
2011 else
2012 {
2013 funcexe_T funcexe;
2014
2015 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002016 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002017 funcexe.fe_firstline = curwin->w_cursor.lnum;
2018 funcexe.fe_lastline = curwin->w_cursor.lnum;
2019 funcexe.fe_evaluate = evaluate;
2020 funcexe.fe_partial = partial;
2021 funcexe.fe_basetv = basetv;
2022 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002023 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002024 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002025 }
2026 vim_free(s);
2027
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002028 // If evaluate is FALSE rettv->v_type was not set in
2029 // get_func_tv, but it's needed in handle_subscript() to parse
2030 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002031 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2032 {
2033 rettv->vval.v_string = NULL;
2034 rettv->v_type = VAR_FUNC;
2035 }
2036
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002037 // Stop the expression evaluation when immediately
2038 // aborting on error, or when an interrupt occurred or
2039 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002040 if (evaluate && aborting())
2041 {
2042 if (ret == OK)
2043 clear_tv(rettv);
2044 ret = FAIL;
2045 }
2046 return ret;
2047}
2048
2049/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002050 * Get the next line source line without advancing. But do skip over comment
2051 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002052 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002053 */
2054 static char_u *
2055getline_peek_skip_comments(evalarg_T *evalarg)
2056{
2057 for (;;)
2058 {
2059 char_u *next = getline_peek(evalarg->eval_getline,
2060 evalarg->eval_cookie);
2061 char_u *p;
2062
2063 if (next == NULL)
2064 break;
2065 p = skipwhite(next);
2066 if (*p != NUL && !vim9_comment_start(p))
2067 return next;
2068 (void)eval_next_line(evalarg);
2069 }
2070 return NULL;
2071}
2072
2073/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002074 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2075 * comment) and there is a next line, return the next line (skipping blanks)
2076 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002077 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2078 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002079 * "arg" must point somewhere inside a line, not at the start.
2080 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002081 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002082eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2083{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002084 char_u *p = skipwhite(arg);
2085
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002086 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002087 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002088 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002089 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002090 && (*p == NUL || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002091 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002092 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002093
2094 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002095 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002096 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002097 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002098
Bram Moolenaar9d489562020-07-30 20:08:50 +02002099 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002100 {
2101 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002102 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002103 }
2104 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002105 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002106}
2107
2108/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002109 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002110 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002111 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002112 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002113eval_next_line(evalarg_T *evalarg)
2114{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002115 garray_T *gap = &evalarg->eval_ga;
2116 char_u *line;
2117
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002118 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002119 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2120 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002121 else
2122 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002123 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002124 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2125 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002126 char_u *p = skipwhite(line);
2127
2128 // Going to concatenate the lines after parsing. For an empty or
2129 // comment line use an empty string.
2130 if (*p == NUL || vim9_comment_start(p))
2131 {
2132 vim_free(line);
2133 line = vim_strsave((char_u *)"");
2134 }
2135
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002136 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2137 ++gap->ga_len;
2138 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002139 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002140 {
2141 vim_free(evalarg->eval_tofree);
2142 evalarg->eval_tofree = line;
2143 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002144
2145 // Advanced to the next line, "arg" no longer points into the previous
2146 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002147 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002148 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002149}
2150
Bram Moolenaare6b53242020-07-01 17:28:33 +02002151/*
2152 * Call eval_next_non_blank() and get the next line if needed.
2153 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002154 char_u *
2155skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2156{
2157 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002158 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002159
Bram Moolenaar962d7212020-07-04 14:15:00 +02002160 if (evalarg == NULL)
2161 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002162 eval_next_non_blank(p, evalarg, &getnext);
2163 if (getnext)
2164 return eval_next_line(evalarg);
2165 return p;
2166}
2167
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002168/*
Bram Moolenaar844fb642021-10-23 13:32:30 +01002169 * Initialize "evalarg" for use.
2170 */
2171 void
2172init_evalarg(evalarg_T *evalarg)
2173{
2174 CLEAR_POINTER(evalarg);
2175 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
2176}
2177
2178/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002179 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002180 */
2181 void
2182clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2183{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002184 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002185 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002186 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002187 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002188 if (eap != NULL)
2189 {
2190 // We may need to keep the original command line, e.g. for
2191 // ":let" it has the variable names. But we may also need the
2192 // new one, "nextcmd" points into it. Keep both.
2193 vim_free(eap->cmdline_tofree);
2194 eap->cmdline_tofree = *eap->cmdlinep;
2195 *eap->cmdlinep = evalarg->eval_tofree;
2196 }
2197 else
2198 vim_free(evalarg->eval_tofree);
2199 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002200 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002201
Bram Moolenaar844fb642021-10-23 13:32:30 +01002202 ga_clear_strings(&evalarg->eval_tofree_ga);
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002203 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002204 }
2205}
2206
2207/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002209 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2211 */
2212
2213/*
2214 * Handle zero level expression.
2215 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002216 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002217 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002218 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 * Return OK or FAIL.
2220 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002221 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002222eval0(
2223 char_u *arg,
2224 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002225 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002226 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002228 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2229}
2230
2231/*
2232 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2233 * expression and don't check what comes after the expression.
2234 */
2235 int
2236eval0_retarg(
2237 char_u *arg,
2238 typval_T *rettv,
2239 exarg_T *eap,
2240 evalarg_T *evalarg,
2241 char_u **retarg)
2242{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 int ret;
2244 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002245 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002246 int did_emsg_before = did_emsg;
2247 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002248 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002249 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002250 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251
2252 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002253 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002254 expr_end = p;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002255 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002256
Bram Moolenaar02929a32021-12-17 14:46:12 +00002257 // In Vim9 script a command block is not split at NL characters for
2258 // commands using an expression argument. Skip over a '#' comment to check
2259 // for a following NL. Require white space before the '#'.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002260 if (in_vim9script() && p > expr_end && retarg == NULL)
Bram Moolenaar02929a32021-12-17 14:46:12 +00002261 while (*p == '#')
2262 {
2263 char_u *nl = vim_strchr(p, NL);
2264
2265 if (nl == NULL)
2266 break;
2267 p = skipwhite(nl + 1);
2268 if (eap != NULL && *p != NUL)
2269 eap->nextcmd = p;
2270 check_for_end = FALSE;
2271 }
2272
2273 if (ret != FAIL && check_for_end)
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002274 end_error = !ends_excmd2(arg, p);
2275 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 {
2277 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002278 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 /*
2280 * Report the invalid expression unless the expression evaluation has
2281 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002282 * exception, or we already gave a more specific error.
2283 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002285 if (!aborting()
2286 && did_emsg == did_emsg_before
2287 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002288 && (flags & EVAL_CONSTANT) == 0
2289 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002290 {
2291 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002292 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002293 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002294 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002295 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002296
2297 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002298 // a next command to avoid more errors, unless "|" is following, which
2299 // could only be a command separator.
2300 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2301 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002302 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002304
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002305 if (retarg != NULL)
2306 *retarg = p;
2307 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002308 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002309
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 return ret;
2311}
2312
2313/*
2314 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002315 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002316 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 *
2318 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002319 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002321 * Note: "rettv.v_lock" is not set.
2322 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 * Return OK or FAIL.
2324 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002325 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002326eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002328 char_u *p;
2329 int getnext;
2330
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002331 CLEAR_POINTER(rettv);
2332
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 /*
2334 * Get the first variable.
2335 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002336 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 return FAIL;
2338
Bram Moolenaar793648f2020-06-26 21:28:25 +02002339 p = eval_next_non_blank(*arg, evalarg, &getnext);
2340 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002342 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002343 int result;
2344 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002345 evalarg_T *evalarg_used = evalarg;
2346 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002347 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002348 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002349 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002350
2351 if (evalarg == NULL)
2352 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002353 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002354 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002355 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002356 orig_flags = evalarg_used->eval_flags;
2357 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002358
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002359 if (getnext)
2360 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002361 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002362 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002363 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002364 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002365 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002366 clear_tv(rettv);
2367 return FAIL;
2368 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002369 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002370 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002371
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002373 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002375 int error = FALSE;
2376
Bram Moolenaar13106602020-10-04 16:06:05 +02002377 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002378 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002379 else if (vim9script)
2380 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002381 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002383 if (error || !op_falsy || !result)
2384 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002385 if (error)
2386 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 }
2388
2389 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002390 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002392 if (op_falsy)
2393 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002394 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002395 {
mityu4ac198c2021-05-28 17:52:40 +02002396 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002397 clear_tv(rettv);
2398 return FAIL;
2399 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002400 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002401 evalarg_used->eval_flags = (op_falsy ? !result : result)
2402 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2403 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002404 {
2405 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002407 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002408 if (!op_falsy || !result)
2409 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002411 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002413 /*
2414 * Check for the ":".
2415 */
2416 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2417 if (*p != ':')
2418 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002419 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002420 if (evaluate && result)
2421 clear_tv(rettv);
2422 evalarg_used->eval_flags = orig_flags;
2423 return FAIL;
2424 }
2425 if (getnext)
2426 *arg = eval_next_line(evalarg_used);
2427 else
2428 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002429 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002430 {
2431 error_white_both(p, 1);
2432 clear_tv(rettv);
2433 evalarg_used->eval_flags = orig_flags;
2434 return FAIL;
2435 }
2436 *arg = p;
2437 }
2438
2439 /*
2440 * Get the third variable. Recursive!
2441 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002442 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002443 {
mityu4ac198c2021-05-28 17:52:40 +02002444 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002445 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002446 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002447 return FAIL;
2448 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002449 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2450 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002451 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002452 if (eval1(arg, &var2, evalarg_used) == FAIL)
2453 {
2454 if (evaluate && result)
2455 clear_tv(rettv);
2456 evalarg_used->eval_flags = orig_flags;
2457 return FAIL;
2458 }
2459 if (evaluate && !result)
2460 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002462
2463 if (evalarg == NULL)
2464 clear_evalarg(&local_evalarg, NULL);
2465 else
2466 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 }
2468
2469 return OK;
2470}
2471
2472/*
2473 * Handle first level expression:
2474 * expr2 || expr2 || expr2 logical OR
2475 *
2476 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002477 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 *
2479 * Return OK or FAIL.
2480 */
2481 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002482eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002484 char_u *p;
2485 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486
2487 /*
2488 * Get the first variable.
2489 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002490 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 return FAIL;
2492
2493 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002494 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002496 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002497 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002499 evalarg_T *evalarg_used = evalarg;
2500 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002501 int evaluate;
2502 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002503 long result = FALSE;
2504 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002505 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002506 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002507
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002508 if (evalarg == NULL)
2509 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002510 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002511 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002512 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002513 orig_flags = evalarg_used->eval_flags;
2514 evaluate = orig_flags & EVAL_EVALUATE;
2515 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002516 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002517 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002518 result = tv_get_bool_chk(rettv, &error);
2519 else if (tv_get_number_chk(rettv, &error) != 0)
2520 result = TRUE;
2521 clear_tv(rettv);
2522 if (error)
2523 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 }
2525
2526 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002527 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002529 while (p[0] == '|' && p[1] == '|')
2530 {
2531 if (getnext)
2532 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002533 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002534 {
2535 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2536 {
2537 error_white_both(p, 2);
2538 clear_tv(rettv);
2539 return FAIL;
2540 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002541 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002542 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002543
2544 /*
2545 * Get the second variable.
2546 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002547 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2548 {
mityu4ac198c2021-05-28 17:52:40 +02002549 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002550 clear_tv(rettv);
2551 return FAIL;
2552 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002553 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2554 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002555 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002556 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002557 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002558
2559 /*
2560 * Compute the result.
2561 */
2562 if (evaluate && !result)
2563 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002564 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002565 result = tv_get_bool_chk(&var2, &error);
2566 else if (tv_get_number_chk(&var2, &error) != 0)
2567 result = TRUE;
2568 clear_tv(&var2);
2569 if (error)
2570 return FAIL;
2571 }
2572 if (evaluate)
2573 {
2574 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002575 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002576 rettv->v_type = VAR_BOOL;
2577 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002578 }
2579 else
2580 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002581 rettv->v_type = VAR_NUMBER;
2582 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002583 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002584 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002585
2586 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002588
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002589 if (evalarg == NULL)
2590 clear_evalarg(&local_evalarg, NULL);
2591 else
2592 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 }
2594
2595 return OK;
2596}
2597
2598/*
2599 * Handle second level expression:
2600 * expr3 && expr3 && expr3 logical AND
2601 *
2602 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002603 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 *
2605 * Return OK or FAIL.
2606 */
2607 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002608eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002610 char_u *p;
2611 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612
2613 /*
2614 * Get the first variable.
2615 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002616 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 return FAIL;
2618
2619 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002620 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002622 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002623 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002625 evalarg_T *evalarg_used = evalarg;
2626 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002627 int orig_flags;
2628 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002629 long result = TRUE;
2630 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002631 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002632 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002633
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002634 if (evalarg == NULL)
2635 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002636 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002637 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002638 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002639 orig_flags = evalarg_used->eval_flags;
2640 evaluate = orig_flags & EVAL_EVALUATE;
2641 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002642 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002643 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002644 result = tv_get_bool_chk(rettv, &error);
2645 else if (tv_get_number_chk(rettv, &error) == 0)
2646 result = FALSE;
2647 clear_tv(rettv);
2648 if (error)
2649 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 }
2651
2652 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002653 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002655 while (p[0] == '&' && p[1] == '&')
2656 {
2657 if (getnext)
2658 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002659 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002660 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002661 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002662 {
2663 error_white_both(p, 2);
2664 clear_tv(rettv);
2665 return FAIL;
2666 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002667 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002668 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002669
2670 /*
2671 * Get the second variable.
2672 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002673 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2674 {
mityu4ac198c2021-05-28 17:52:40 +02002675 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002676 clear_tv(rettv);
2677 return FAIL;
2678 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002679 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2680 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002681 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002682 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002683 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002684 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002685
2686 /*
2687 * Compute the result.
2688 */
2689 if (evaluate && result)
2690 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002691 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002692 result = tv_get_bool_chk(&var2, &error);
2693 else if (tv_get_number_chk(&var2, &error) == 0)
2694 result = FALSE;
2695 clear_tv(&var2);
2696 if (error)
2697 return FAIL;
2698 }
2699 if (evaluate)
2700 {
2701 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002702 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002703 rettv->v_type = VAR_BOOL;
2704 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002705 }
2706 else
2707 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002708 rettv->v_type = VAR_NUMBER;
2709 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002710 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002711 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002712
2713 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002715
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002716 if (evalarg == NULL)
2717 clear_evalarg(&local_evalarg, NULL);
2718 else
2719 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 }
2721
2722 return OK;
2723}
2724
2725/*
2726 * Handle third level expression:
2727 * var1 == var2
2728 * var1 =~ var2
2729 * var1 != var2
2730 * var1 !~ var2
2731 * var1 > var2
2732 * var1 >= var2
2733 * var1 < var2
2734 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002735 * var1 is var2
2736 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 *
2738 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002739 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 *
2741 * Return OK or FAIL.
2742 */
2743 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002744eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002747 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002748 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002750 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751
2752 /*
2753 * Get the first variable.
2754 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002755 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 return FAIL;
2757
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002758 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002759 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760
2761 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002762 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002764 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002766 typval_T var2;
2767 int ic;
2768 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002769 int evaluate = evalarg == NULL
2770 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002771
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002772 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002773 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002774 *arg = eval_next_line(evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002775 p = *arg;
2776 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002777 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2778 {
mityu4ac198c2021-05-28 17:52:40 +02002779 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002780 clear_tv(rettv);
2781 return FAIL;
2782 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002783
Bram Moolenaar696ba232020-07-29 21:20:41 +02002784 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2785 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002786 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002787 clear_tv(rettv);
2788 return FAIL;
2789 }
2790
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002791 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 if (p[len] == '?')
2793 {
2794 ic = TRUE;
2795 ++len;
2796 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002797 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 else if (p[len] == '#')
2799 {
2800 ic = FALSE;
2801 ++len;
2802 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002803 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002805 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806
2807 /*
2808 * Get the second variable.
2809 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002810 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2811 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002812 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002813 clear_tv(rettv);
2814 return FAIL;
2815 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002816 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002817 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002819 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 return FAIL;
2821 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002822 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002823 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002824 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002825
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002826 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002827 {
2828 ret = FAIL;
2829 clear_tv(rettv);
2830 }
2831 else
2832 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002833 clear_tv(&var2);
2834 return ret;
2835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 }
2837
2838 return OK;
2839}
2840
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002841/*
2842 * Make a copy of blob "tv1" and append blob "tv2".
2843 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002844 void
2845eval_addblob(typval_T *tv1, typval_T *tv2)
2846{
2847 blob_T *b1 = tv1->vval.v_blob;
2848 blob_T *b2 = tv2->vval.v_blob;
2849 blob_T *b = blob_alloc();
2850 int i;
2851
2852 if (b != NULL)
2853 {
2854 for (i = 0; i < blob_len(b1); i++)
2855 ga_append(&b->bv_ga, blob_get(b1, i));
2856 for (i = 0; i < blob_len(b2); i++)
2857 ga_append(&b->bv_ga, blob_get(b2, i));
2858
2859 clear_tv(tv1);
2860 rettv_blob_set(tv1, b);
2861 }
2862}
2863
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002864/*
2865 * Make a copy of list "tv1" and append list "tv2".
2866 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002867 int
2868eval_addlist(typval_T *tv1, typval_T *tv2)
2869{
2870 typval_T var3;
2871
2872 // concatenate Lists
2873 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2874 {
2875 clear_tv(tv1);
2876 clear_tv(tv2);
2877 return FAIL;
2878 }
2879 clear_tv(tv1);
2880 *tv1 = var3;
2881 return OK;
2882}
2883
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884/*
2885 * Handle fourth level expression:
2886 * + number addition
2887 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002888 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002889 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 *
2891 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002892 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 *
2894 * Return OK or FAIL.
2895 */
2896 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002897eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 /*
2900 * Get the first variable.
2901 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002902 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 return FAIL;
2904
2905 /*
2906 * Repeat computing, until no '+', '-' or '.' is following.
2907 */
2908 for (;;)
2909 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002910 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002911 int getnext;
2912 char_u *p;
2913 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002914 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002915 int concat;
2916 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002917 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002918
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002919 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002920 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002921 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002922 p = eval_next_non_blank(*arg, evalarg, &getnext);
2923 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02002924 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002925 if ((op != '+' && op != '-' && !concat) || p[1] == '='
2926 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002928 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
2929 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002930
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002931 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002932 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002933 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002934 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002935 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002936 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002937 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002938 {
mityu4ac198c2021-05-28 17:52:40 +02002939 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002940 clear_tv(rettv);
2941 return FAIL;
2942 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002943 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002944 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002945 if ((op != '+' || (rettv->v_type != VAR_LIST
2946 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002947#ifdef FEAT_FLOAT
2948 && (op == '.' || rettv->v_type != VAR_FLOAT)
2949#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002950 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002951 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002952 int error = FALSE;
2953
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002954 // For "list + ...", an illegal use of the first operand as
2955 // a number cannot be determined before evaluating the 2nd
2956 // operand: if this is also a list, all is ok.
2957 // For "something . ...", "something - ..." or "non-list + ...",
2958 // we know that the first operand needs to be a string or number
2959 // without evaluating the 2nd operand. So check before to avoid
2960 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002961 if (op != '.')
2962 tv_get_number_chk(rettv, &error);
2963 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002964 {
2965 clear_tv(rettv);
2966 return FAIL;
2967 }
2968 }
2969
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 /*
2971 * Get the second variable.
2972 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002973 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002974 {
mityu89dcb4d2021-05-28 13:50:17 +02002975 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002976 clear_tv(rettv);
2977 return FAIL;
2978 }
2979 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002980 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002982 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 return FAIL;
2984 }
2985
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002986 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 {
2988 /*
2989 * Compute the result.
2990 */
2991 if (op == '.')
2992 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002993 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2994 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002995 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002996
Bram Moolenaar2e086612020-08-25 22:37:48 +02002997 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002998 || var2.v_type == VAR_CHANNEL
2999 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003000 semsg(_(e_using_invalid_value_as_string_str),
3001 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003002#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02003003 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003004 {
3005 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3006 var2.vval.v_float);
3007 s2 = buf2;
3008 }
3009#endif
3010 else
3011 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003012 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003013 {
3014 clear_tv(rettv);
3015 clear_tv(&var2);
3016 return FAIL;
3017 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003018 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003019 clear_tv(rettv);
3020 rettv->v_type = VAR_STRING;
3021 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003023 else if (op == '+' && rettv->v_type == VAR_BLOB
3024 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003025 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003026 else if (op == '+' && rettv->v_type == VAR_LIST
3027 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003028 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003029 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003030 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 else
3033 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003034 int error = FALSE;
3035 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003036#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003037 float_T f1 = 0, f2 = 0;
3038
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003039 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003040 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003041 f1 = rettv->vval.v_float;
3042 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003043 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003044 else
3045#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003046 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003047 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003048 if (error)
3049 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003050 // This can only happen for "list + non-list" or
3051 // "blob + non-blob". For "non-list + ..." or
3052 // "something - ...", we returned before evaluating the
3053 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003054 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003055 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003056 return FAIL;
3057 }
3058#ifdef FEAT_FLOAT
3059 if (var2.v_type == VAR_FLOAT)
3060 f1 = n1;
3061#endif
3062 }
3063#ifdef FEAT_FLOAT
3064 if (var2.v_type == VAR_FLOAT)
3065 {
3066 f2 = var2.vval.v_float;
3067 n2 = 0;
3068 }
3069 else
3070#endif
3071 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003072 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003073 if (error)
3074 {
3075 clear_tv(rettv);
3076 clear_tv(&var2);
3077 return FAIL;
3078 }
3079#ifdef FEAT_FLOAT
3080 if (rettv->v_type == VAR_FLOAT)
3081 f2 = n2;
3082#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003083 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003084 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003085
3086#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003087 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003088 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3089 {
3090 if (op == '+')
3091 f1 = f1 + f2;
3092 else
3093 f1 = f1 - f2;
3094 rettv->v_type = VAR_FLOAT;
3095 rettv->vval.v_float = f1;
3096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003098#endif
3099 {
3100 if (op == '+')
3101 n1 = n1 + n2;
3102 else
3103 n1 = n1 - n2;
3104 rettv->v_type = VAR_NUMBER;
3105 rettv->vval.v_number = n1;
3106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003108 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 }
3110 }
3111 return OK;
3112}
3113
3114/*
3115 * Handle fifth level expression:
3116 * * number multiplication
3117 * / number division
3118 * % number modulo
3119 *
3120 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003121 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 *
3123 * Return OK or FAIL.
3124 */
3125 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003126eval6(
3127 char_u **arg,
3128 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003129 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003130 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003132#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003133 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003134#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135
3136 /*
3137 * Get the first variable.
3138 */
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003139 if (eval7t(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 return FAIL;
3141
3142 /*
3143 * Repeat computing, until no '*', '/' or '%' is following.
3144 */
3145 for (;;)
3146 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003147 int evaluate;
3148 int getnext;
3149 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003150 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003151 int op;
3152 varnumber_T n1, n2;
3153#ifdef FEAT_FLOAT
3154 float_T f1, f2;
3155#endif
3156 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003157
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003158 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003159 p = eval_next_non_blank(*arg, evalarg, &getnext);
3160 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003161 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003163
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003164 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003165 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003166 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003167 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003168 {
3169 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3170 {
mityu4ac198c2021-05-28 17:52:40 +02003171 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003172 clear_tv(rettv);
3173 return FAIL;
3174 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003175 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003178#ifdef FEAT_FLOAT
3179 f1 = 0;
3180 f2 = 0;
3181#endif
3182 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003183 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003185#ifdef FEAT_FLOAT
3186 if (rettv->v_type == VAR_FLOAT)
3187 {
3188 f1 = rettv->vval.v_float;
3189 use_float = TRUE;
3190 n1 = 0;
3191 }
3192 else
3193#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003194 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003195 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003196 if (error)
3197 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 }
3199 else
3200 n1 = 0;
3201
3202 /*
3203 * Get the second variable.
3204 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003205 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3206 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003207 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003208 clear_tv(rettv);
3209 return FAIL;
3210 }
3211 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003212 if (eval7t(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 return FAIL;
3214
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003215 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003217#ifdef FEAT_FLOAT
3218 if (var2.v_type == VAR_FLOAT)
3219 {
3220 if (!use_float)
3221 {
3222 f1 = n1;
3223 use_float = TRUE;
3224 }
3225 f2 = var2.vval.v_float;
3226 n2 = 0;
3227 }
3228 else
3229#endif
3230 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003231 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003232 clear_tv(&var2);
3233 if (error)
3234 return FAIL;
3235#ifdef FEAT_FLOAT
3236 if (use_float)
3237 f2 = n2;
3238#endif
3239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240
3241 /*
3242 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003243 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003245#ifdef FEAT_FLOAT
3246 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003248 if (op == '*')
3249 f1 = f1 * f2;
3250 else if (op == '/')
3251 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003252# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003253 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003254 if (f2 == 0.0)
3255 {
3256 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003257 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003258 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003259 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003260 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003261 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003262 }
3263 else
3264 f1 = f1 / f2;
3265# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003266 // We rely on the floating point library to handle divide
3267 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003268 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003269# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003272 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003273 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003274 return FAIL;
3275 }
3276 rettv->v_type = VAR_FLOAT;
3277 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 }
3279 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003280#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003282 int failed = FALSE;
3283
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003284 if (op == '*')
3285 n1 = n1 * n2;
3286 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003287 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003289 n1 = num_modulus(n1, n2, &failed);
3290 if (failed)
3291 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003292
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003293 rettv->v_type = VAR_NUMBER;
3294 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 }
3297 }
3298
3299 return OK;
3300}
3301
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003302/*
3303 * Handle a type cast before a base level expression.
3304 * "arg" must point to the first non-white of the expression.
3305 * "arg" is advanced to just after the recognized expression.
3306 * Return OK or FAIL.
3307 */
3308 static int
3309eval7t(
3310 char_u **arg,
3311 typval_T *rettv,
3312 evalarg_T *evalarg,
3313 int want_string) // after "." operator
3314{
3315 type_T *want_type = NULL;
3316 garray_T type_list; // list of pointers to allocated types
3317 int res;
3318 int evaluate = evalarg == NULL ? 0
3319 : (evalarg->eval_flags & EVAL_EVALUATE);
3320
3321 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003322 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3323 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003324 {
3325 ++*arg;
3326 ga_init2(&type_list, sizeof(type_T *), 10);
3327 want_type = parse_type(arg, &type_list, TRUE);
3328 if (want_type == NULL && (evaluate || **arg != '>'))
3329 {
3330 clear_type_list(&type_list);
3331 return FAIL;
3332 }
3333
3334 if (**arg != '>')
3335 {
3336 if (*skipwhite(*arg) == '>')
3337 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3338 else
3339 emsg(_(e_missing_gt));
3340 clear_type_list(&type_list);
3341 return FAIL;
3342 }
3343 ++*arg;
3344 *arg = skipwhite_and_linebreak(*arg, evalarg);
3345 }
3346
3347 res = eval7(arg, rettv, evalarg, want_string);
3348
3349 if (want_type != NULL && evaluate)
3350 {
3351 if (res == OK)
3352 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003353 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3354 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003355
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003356 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003357 {
3358 if (want_type == &t_bool && actual != &t_bool
3359 && (actual->tt_flags & TTFLAG_BOOL_OK))
3360 {
3361 int n = tv2bool(rettv);
3362
3363 // can use "0" and "1" for boolean in some places
3364 clear_tv(rettv);
3365 rettv->v_type = VAR_BOOL;
3366 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3367 }
3368 else
3369 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003370 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003371
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003372 where.wt_variable = TRUE;
3373 res = check_type(want_type, actual, TRUE, where);
3374 }
3375 }
3376 }
3377 clear_type_list(&type_list);
3378 }
3379
3380 return res;
3381}
3382
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003383 int
3384eval_leader(char_u **arg, int vim9)
3385{
3386 char_u *s = *arg;
3387 char_u *p = *arg;
3388
3389 while (*p == '!' || *p == '-' || *p == '+')
3390 {
3391 char_u *n = skipwhite(p + 1);
3392
3393 // ++, --, -+ and +- are not accepted in Vim9 script
3394 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3395 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003396 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003397 return FAIL;
3398 }
3399 p = n;
3400 }
3401 *arg = p;
3402 return OK;
3403}
3404
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405/*
3406 * Handle sixth level expression:
3407 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003408 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003409 * "string" string constant
3410 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 * &option-name option value
3412 * @r register contents
3413 * identifier variable value
3414 * function() function call
3415 * $VAR environment variable
3416 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003417 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003418 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003419 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003420 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 *
3422 * Also handle:
3423 * ! in front logical NOT
3424 * - in front unary minus
3425 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003426 * trailing [] subscript in String or List
3427 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003428 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 *
3430 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003431 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 *
3433 * Return OK or FAIL.
3434 */
3435 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003436eval7(
3437 char_u **arg,
3438 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003439 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003440 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003442 int evaluate = evalarg != NULL
3443 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 int len;
3445 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003446 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 char_u *start_leader, *end_leader;
3448 int ret = OK;
3449 char_u *alias;
3450
3451 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003452 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003453 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003455 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456
3457 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003458 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 */
3460 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003461 if (eval_leader(arg, in_vim9script()) == FAIL)
3462 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 end_leader = *arg;
3464
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003465 if (**arg == '.' && (!isdigit(*(*arg + 1))
3466#ifdef FEAT_FLOAT
Bram Moolenaardd9de502021-08-15 13:49:42 +02003467 || in_old_script(2)
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003468#endif
3469 ))
3470 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003471 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003472 ++*arg;
3473 return FAIL;
3474 }
3475
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 switch (**arg)
3477 {
3478 /*
3479 * Number constant.
3480 */
3481 case '0':
3482 case '1':
3483 case '2':
3484 case '3':
3485 case '4':
3486 case '5':
3487 case '6':
3488 case '7':
3489 case '8':
3490 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003491 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003492
3493 // Apply prefixed "-" and "+" now. Matters especially when
3494 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003495 if (ret == OK && evaluate && end_leader > start_leader
3496 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003497 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 break;
3499
3500 /*
3501 * String constant: "string".
3502 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003503 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 break;
3505
3506 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003507 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003509 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003510 break;
3511
3512 /*
3513 * List: [expr, expr]
3514 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003515 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 break;
3517
3518 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003519 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003520 */
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003521 case '#': if (in_vim9script())
3522 {
3523 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3524 }
3525 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003526 {
3527 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003528 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003529 }
3530 else
3531 ret = NOTDONE;
3532 break;
3533
3534 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003535 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003536 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003537 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003538 case '{': if (in_vim9script())
3539 ret = NOTDONE;
3540 else
3541 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003542 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003543 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003544 break;
3545
3546 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003547 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003549 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 break;
3551
3552 /*
3553 * Environment variable: $VAR.
3554 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003555 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 break;
3557
3558 /*
3559 * Register contents: @r.
3560 */
3561 case '@': ++*arg;
3562 if (evaluate)
3563 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003564 if (in_vim9script() && IS_WHITE_OR_NUL(**arg))
3565 semsg(_(e_syntax_error_at_str), *arg);
3566 else if (in_vim9script() && !valid_yank_reg(**arg, FALSE))
3567 emsg_invreg(**arg);
3568 else
3569 {
3570 rettv->v_type = VAR_STRING;
3571 rettv->vval.v_string = get_reg_contents(**arg,
3572 GREG_EXPR_SRC);
3573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 }
3575 if (**arg != NUL)
3576 ++*arg;
3577 break;
3578
3579 /*
3580 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003581 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003583 case '(': ret = NOTDONE;
3584 if (in_vim9script())
Bram Moolenaar06409502021-02-17 17:00:27 +01003585 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003586 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003587 if (ret == OK && evaluate)
3588 {
3589 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3590
Bram Moolenaara9931532021-06-12 15:58:16 +02003591 // Compile it here to get the return type. The return
3592 // type is optional, when it's missing use t_unknown.
3593 // This is recognized in compile_return().
3594 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3595 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003596 if (compile_def_function(ufunc,
Bram Moolenaare99d4222021-06-13 14:01:26 +02003597 FALSE, COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003598 {
3599 clear_tv(rettv);
3600 ret = FAIL;
3601 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003602 }
3603 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003604 if (ret == NOTDONE)
3605 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003606 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003607 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003608
Bram Moolenaar9215f012020-06-27 21:18:00 +02003609 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003610 if (**arg == ')')
3611 ++*arg;
3612 else if (ret == OK)
3613 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003614 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003615 clear_tv(rettv);
3616 ret = FAIL;
3617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 }
3619 break;
3620
Bram Moolenaar8c711452005-01-14 21:53:12 +00003621 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 break;
3623 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003624
3625 if (ret == NOTDONE)
3626 {
3627 /*
3628 * Must be a variable or function name.
3629 * Can also be a curly-braces kind of name: {expr}.
3630 */
3631 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003632 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003633 if (alias != NULL)
3634 s = alias;
3635
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003636 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003637 ret = FAIL;
3638 else
3639 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003640 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3641
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003642 if (evaluate && in_vim9script() && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003643 {
3644 emsg(_(e_cannot_use_underscore_here));
3645 ret = FAIL;
3646 }
3647 else if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003648 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003649 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003650 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003651 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003652 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003653 else if (flags & EVAL_CONSTANT)
3654 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003655 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003656 {
3657 // get the value of "true", "false" or a variable
3658 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3659 {
3660 rettv->v_type = VAR_BOOL;
3661 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003662 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003663 }
3664 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003665 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003666 {
3667 rettv->v_type = VAR_BOOL;
3668 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003669 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003670 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003671 else if (len == 4 && in_vim9script()
3672 && STRNCMP(s, "null", 4) == 0)
3673 {
3674 rettv->v_type = VAR_SPECIAL;
3675 rettv->vval.v_number = VVAL_NULL;
3676 ret = OK;
3677 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003678 else
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003679 {
3680 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003681 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003682 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003683 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003684 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003685 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003686 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003687 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003688 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003689 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003690 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003691 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003692 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003693 }
3694
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003695 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3696 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003697 if (ret == OK)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003698 ret = handle_subscript(arg, name_start, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699
3700 /*
3701 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3702 */
3703 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003704 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003705 return ret;
3706}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003707
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003708/*
3709 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003710 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003711 * Adjusts "end_leaderp" until it is at "start_leader".
3712 */
3713 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003714eval7_leader(
3715 typval_T *rettv,
3716 int numeric_only,
3717 char_u *start_leader,
3718 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003719{
3720 char_u *end_leader = *end_leaderp;
3721 int ret = OK;
3722 int error = FALSE;
3723 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003724 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003725#ifdef FEAT_FLOAT
3726 float_T f = 0.0;
3727
3728 if (rettv->v_type == VAR_FLOAT)
3729 f = rettv->vval.v_float;
3730 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003731#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003732 {
3733 while (VIM_ISWHITE(end_leader[-1]))
3734 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003735 if (in_vim9script() && end_leader[-1] == '!')
3736 val = tv2bool(rettv);
3737 else
3738 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003739 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003740 if (error)
3741 {
3742 clear_tv(rettv);
3743 ret = FAIL;
3744 }
3745 else
3746 {
3747 while (end_leader > start_leader)
3748 {
3749 --end_leader;
3750 if (*end_leader == '!')
3751 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003752 if (numeric_only)
3753 {
3754 ++end_leader;
3755 break;
3756 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003757#ifdef FEAT_FLOAT
3758 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003759 {
3760 if (in_vim9script())
3761 {
3762 rettv->v_type = VAR_BOOL;
3763 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3764 }
3765 else
3766 f = !f;
3767 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003768 else
3769#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003770 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003771 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003772 type = VAR_BOOL;
3773 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003774 }
3775 else if (*end_leader == '-')
3776 {
3777#ifdef FEAT_FLOAT
3778 if (rettv->v_type == VAR_FLOAT)
3779 f = -f;
3780 else
3781#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003782 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003783 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003784 type = VAR_NUMBER;
3785 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003786 }
3787 }
3788#ifdef FEAT_FLOAT
3789 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003791 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003792 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003794 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003795#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003796 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003797 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003798 if (in_vim9script())
3799 rettv->v_type = type;
3800 else
3801 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003802 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003805 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 return ret;
3807}
3808
3809/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003810 * Call the function referred to in "rettv".
3811 */
3812 static int
3813call_func_rettv(
3814 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003815 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003816 typval_T *rettv,
3817 int evaluate,
3818 dict_T *selfdict,
3819 typval_T *basetv)
3820{
3821 partial_T *pt = NULL;
3822 funcexe_T funcexe;
3823 typval_T functv;
3824 char_u *s;
3825 int ret;
3826
3827 // need to copy the funcref so that we can clear rettv
3828 if (evaluate)
3829 {
3830 functv = *rettv;
3831 rettv->v_type = VAR_UNKNOWN;
3832
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003833 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003834 if (functv.v_type == VAR_PARTIAL)
3835 {
3836 pt = functv.vval.v_partial;
3837 s = partial_name(pt);
3838 }
3839 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003840 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003841 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003842 if (s == NULL || *s == NUL)
3843 {
3844 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02003845 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003846 goto theend;
3847 }
3848 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003849 }
3850 else
3851 s = (char_u *)"";
3852
Bram Moolenaara80faa82020-04-12 19:37:17 +02003853 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003854 funcexe.fe_firstline = curwin->w_cursor.lnum;
3855 funcexe.fe_lastline = curwin->w_cursor.lnum;
3856 funcexe.fe_evaluate = evaluate;
3857 funcexe.fe_partial = pt;
3858 funcexe.fe_selfdict = selfdict;
3859 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003860 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003861
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003862theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003863 // Clear the funcref afterwards, so that deleting it while
3864 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003865 if (evaluate)
3866 clear_tv(&functv);
3867
3868 return ret;
3869}
3870
3871/*
3872 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003873 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003874 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3875 */
3876 static int
3877eval_lambda(
3878 char_u **arg,
3879 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003880 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003881 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003882{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003883 int evaluate = evalarg != NULL
3884 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003885 typval_T base = *rettv;
3886 int ret;
3887
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003888 rettv->v_type = VAR_UNKNOWN;
3889
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003890 if (**arg == '{')
3891 {
3892 // ->{lambda}()
3893 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3894 }
3895 else
3896 {
3897 // ->(lambda)()
3898 ++*arg;
3899 ret = eval1(arg, rettv, evalarg);
3900 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar605ec912021-12-18 16:54:31 +00003901 if (**arg == ')')
3902 {
3903 ++*arg;
3904 }
3905 else
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003906 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003907 emsg(_(e_missing_closing_paren));
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003908 ret = FAIL;
3909 }
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003910 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003911 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003912 return FAIL;
3913 else if (**arg != '(')
3914 {
3915 if (verbose)
3916 {
3917 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00003918 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003919 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003920 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003921 }
3922 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003923 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003924 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003925 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003926 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003927
3928 // Clear the funcref afterwards, so that deleting it while
3929 // evaluating the arguments is possible (see test55).
3930 if (evaluate)
3931 clear_tv(&base);
3932
3933 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003934}
3935
3936/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003937 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003938 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02003939 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3940 */
3941 static int
3942eval_method(
3943 char_u **arg,
3944 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003945 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003946 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003947{
3948 char_u *name;
3949 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003950 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003951 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003952 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003953 int evaluate = evalarg != NULL
3954 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003955
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003956 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003957
Bram Moolenaarac92e252019-08-03 21:58:38 +02003958 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003959 len = get_name_len(arg, &alias, evaluate, TRUE);
3960 if (alias != NULL)
3961 name = alias;
3962
3963 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003964 {
3965 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00003966 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003967 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003968 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003969 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003970 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003971 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003972 if (**arg != '(')
3973 {
3974 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00003975 semsg(_(e_missing_parenthesis_str), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003976 ret = FAIL;
3977 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003978 else if (VIM_ISWHITE((*arg)[-1]))
3979 {
3980 if (verbose)
Bram Moolenaar3a846e62022-01-01 16:21:00 +00003981 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar51841322019-08-08 21:10:01 +02003982 ret = FAIL;
3983 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003984 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003985 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003986 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003987 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003988
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003989 // Clear the funcref afterwards, so that deleting it while
3990 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003991 if (evaluate)
3992 clear_tv(&base);
3993
Bram Moolenaarac92e252019-08-03 21:58:38 +02003994 return ret;
3995}
3996
3997/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003998 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3999 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004000 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4001 */
4002 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004003eval_index(
4004 char_u **arg,
4005 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004006 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004007 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004008{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004009 int evaluate = evalarg != NULL
4010 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004011 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004012 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004013 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004014 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004015 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004016 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004017
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004018 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4019 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004020
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004021 init_tv(&var1);
4022 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004023 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004024 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004025 /*
4026 * dict.name
4027 */
4028 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004029 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004030 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004031 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004032 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004033 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004034 }
4035 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004036 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004037 /*
4038 * something[idx]
4039 *
4040 * Get the (first) variable from inside the [].
4041 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004042 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004043 if (**arg == ':')
4044 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004045 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004046 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004047 else if (vim9 && **arg == ':')
4048 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004049 semsg(_(e_white_space_required_before_and_after_str_at_str),
4050 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004051 clear_tv(&var1);
4052 return FAIL;
4053 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004054 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004055 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004056 int error = FALSE;
4057
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004058#ifdef FEAT_FLOAT
4059 // allow for indexing with float
4060 if (vim9 && rettv->v_type == VAR_DICT
4061 && var1.v_type == VAR_FLOAT)
4062 {
4063 var1.vval.v_string = typval_tostring(&var1, TRUE);
4064 var1.v_type = VAR_STRING;
4065 }
4066#endif
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004067 if (vim9 && rettv->v_type == VAR_LIST)
4068 tv_get_number_chk(&var1, &error);
4069 else
4070 error = tv_get_string_chk(&var1) == NULL;
4071 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004072 {
4073 // not a number or string
4074 clear_tv(&var1);
4075 return FAIL;
4076 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004077 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004078
4079 /*
4080 * Get the second variable from inside the [:].
4081 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004082 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004083 if (**arg == ':')
4084 {
4085 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004086 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01004087 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004088 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004089 semsg(_(e_white_space_required_before_and_after_str_at_str),
4090 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004091 if (!empty1)
4092 clear_tv(&var1);
4093 return FAIL;
4094 }
4095 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004096 if (**arg == ']')
4097 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004098 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004099 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004100 if (!empty1)
4101 clear_tv(&var1);
4102 return FAIL;
4103 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004104 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004105 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004106 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004107 if (!empty1)
4108 clear_tv(&var1);
4109 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004110 return FAIL;
4111 }
4112 }
4113
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004114 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004115 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004116 if (**arg != ']')
4117 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004118 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004119 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004120 clear_tv(&var1);
4121 if (range)
4122 clear_tv(&var2);
4123 return FAIL;
4124 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004125 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004126 }
4127
4128 if (evaluate)
4129 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004130 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004131 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004132 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004133
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004134 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004135 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004136 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004137 clear_tv(&var2);
4138 return res;
4139 }
4140 return OK;
4141}
4142
4143/*
4144 * Check if "rettv" can have an [index] or [sli:ce]
4145 */
4146 int
4147check_can_index(typval_T *rettv, int evaluate, int verbose)
4148{
4149 switch (rettv->v_type)
4150 {
4151 case VAR_FUNC:
4152 case VAR_PARTIAL:
4153 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004154 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004155 return FAIL;
4156 case VAR_FLOAT:
4157#ifdef FEAT_FLOAT
4158 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004159 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004160 return FAIL;
4161#endif
4162 case VAR_BOOL:
4163 case VAR_SPECIAL:
4164 case VAR_JOB:
4165 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004166 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004167 if (verbose)
4168 emsg(_(e_cannot_index_special_variable));
4169 return FAIL;
4170 case VAR_UNKNOWN:
4171 case VAR_ANY:
4172 case VAR_VOID:
4173 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004174 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004175 emsg(_(e_cannot_index_special_variable));
4176 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004177 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004178 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004179
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004180 case VAR_STRING:
4181 case VAR_LIST:
4182 case VAR_DICT:
4183 case VAR_BLOB:
4184 break;
4185 case VAR_NUMBER:
4186 if (in_vim9script())
4187 emsg(_(e_cannot_index_number));
4188 break;
4189 }
4190 return OK;
4191}
4192
4193/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004194 * slice() function
4195 */
4196 void
4197f_slice(typval_T *argvars, typval_T *rettv)
4198{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004199 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004200 && ((argvars[0].v_type != VAR_STRING
4201 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004202 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004203 && check_for_list_arg(argvars, 0) == FAIL)
4204 || check_for_number_arg(argvars, 1) == FAIL
4205 || check_for_opt_number_arg(argvars, 2) == FAIL))
4206 return;
4207
Bram Moolenaar6601b622021-01-13 21:47:15 +01004208 if (check_can_index(argvars, TRUE, FALSE) == OK)
4209 {
4210 copy_tv(argvars, rettv);
4211 eval_index_inner(rettv, TRUE, argvars + 1,
4212 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4213 TRUE, NULL, 0, FALSE);
4214 }
4215}
4216
4217/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004218 * Apply index or range to "rettv".
4219 * "var1" is the first index, NULL for [:expr].
4220 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004221 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4222 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004223 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4224 */
4225 int
4226eval_index_inner(
4227 typval_T *rettv,
4228 int is_range,
4229 typval_T *var1,
4230 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004231 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004232 char_u *key,
4233 int keylen,
4234 int verbose)
4235{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004236 varnumber_T n1, n2 = 0;
4237 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004238
4239 n1 = 0;
4240 if (var1 != NULL && rettv->v_type != VAR_DICT)
4241 n1 = tv_get_number(var1);
4242
4243 if (is_range)
4244 {
4245 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004246 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004247 if (verbose)
4248 emsg(_(e_cannot_slice_dictionary));
4249 return FAIL;
4250 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004251 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004252 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004253 else
4254 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004255 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004256
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004257 switch (rettv->v_type)
4258 {
4259 case VAR_UNKNOWN:
4260 case VAR_ANY:
4261 case VAR_VOID:
4262 case VAR_FUNC:
4263 case VAR_PARTIAL:
4264 case VAR_FLOAT:
4265 case VAR_BOOL:
4266 case VAR_SPECIAL:
4267 case VAR_JOB:
4268 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004269 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004270 break; // not evaluating, skipping over subscript
4271
4272 case VAR_NUMBER:
4273 case VAR_STRING:
4274 {
4275 char_u *s = tv_get_string(rettv);
4276
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004277 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004278 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004279 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004280 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004281 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004282 else
4283 s = char_from_string(s, n1);
4284 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004285 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004286 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004287 // The resulting variable is a substring. If the indexes
4288 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004289 if (n1 < 0)
4290 {
4291 n1 = len + n1;
4292 if (n1 < 0)
4293 n1 = 0;
4294 }
4295 if (n2 < 0)
4296 n2 = len + n2;
4297 else if (n2 >= len)
4298 n2 = len;
4299 if (n1 >= len || n2 < 0 || n1 > n2)
4300 s = NULL;
4301 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004302 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004303 }
4304 else
4305 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004306 // The resulting variable is a string of a single
4307 // character. If the index is too big or negative the
4308 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004309 if (n1 >= len || n1 < 0)
4310 s = NULL;
4311 else
4312 s = vim_strnsave(s + n1, 1);
4313 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004314 clear_tv(rettv);
4315 rettv->v_type = VAR_STRING;
4316 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004317 }
4318 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004319
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004320 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004321 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4322 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004323 break;
4324
4325 case VAR_LIST:
4326 if (var1 == NULL)
4327 n1 = 0;
4328 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004329 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004330 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004331 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004332 return FAIL;
4333 break;
4334
4335 case VAR_DICT:
4336 {
4337 dictitem_T *item;
4338 typval_T tmp;
4339
4340 if (key == NULL)
4341 {
4342 key = tv_get_string_chk(var1);
4343 if (key == NULL)
4344 return FAIL;
4345 }
4346
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004347 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004348
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004349 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004350 {
4351 if (verbose)
4352 {
4353 if (keylen > 0)
4354 key[keylen] = NUL;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004355 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004356 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004357 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004358 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004359
4360 copy_tv(&item->di_tv, &tmp);
4361 clear_tv(rettv);
4362 *rettv = tmp;
4363 }
4364 break;
4365 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004366 return OK;
4367}
4368
4369/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004370 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004371 */
4372 char_u *
4373partial_name(partial_T *pt)
4374{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004375 if (pt != NULL)
4376 {
4377 if (pt->pt_name != NULL)
4378 return pt->pt_name;
4379 if (pt->pt_func != NULL)
4380 return pt->pt_func->uf_name;
4381 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004382 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004383}
4384
Bram Moolenaarddecc252016-04-06 22:59:37 +02004385 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004386partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004387{
4388 int i;
4389
4390 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004391 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004392 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004393 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004394 if (pt->pt_name != NULL)
4395 {
4396 func_unref(pt->pt_name);
4397 vim_free(pt->pt_name);
4398 }
4399 else
4400 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004401
Bram Moolenaar54656012021-06-09 20:50:46 +02004402 // "out_up" is no longer used, decrement refcount on partial that owns it.
4403 partial_unref(pt->pt_outer.out_up_partial);
4404
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004405 // Decrease the reference count for the context of a closure. If down
4406 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004407 if (pt->pt_funcstack != NULL)
4408 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004409 --pt->pt_funcstack->fs_refcount;
4410 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004411 }
4412
Bram Moolenaarddecc252016-04-06 22:59:37 +02004413 vim_free(pt);
4414}
4415
4416/*
4417 * Unreference a closure: decrement the reference count and free it when it
4418 * becomes zero.
4419 */
4420 void
4421partial_unref(partial_T *pt)
4422{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004423 if (pt != NULL)
4424 {
4425 if (--pt->pt_refcount <= 0)
4426 partial_free(pt);
4427
4428 // If the reference count goes down to one, the funcstack may be the
4429 // only reference and can be freed if no other partials reference it.
4430 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4431 funcstack_check_refcount(pt->pt_funcstack);
4432 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004433}
4434
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004435/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004436 * Return the next (unique) copy ID.
4437 * Used for serializing nested structures.
4438 */
4439 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004440get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004441{
4442 current_copyID += COPYID_INC;
4443 return current_copyID;
4444}
4445
4446/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004447 * Garbage collection for lists and dictionaries.
4448 *
4449 * We use reference counts to be able to free most items right away when they
4450 * are no longer used. But for composite items it's possible that it becomes
4451 * unused while the reference count is > 0: When there is a recursive
4452 * reference. Example:
4453 * :let l = [1, 2, 3]
4454 * :let d = {9: l}
4455 * :let l[1] = d
4456 *
4457 * Since this is quite unusual we handle this with garbage collection: every
4458 * once in a while find out which lists and dicts are not referenced from any
4459 * variable.
4460 *
4461 * Here is a good reference text about garbage collection (refers to Python
4462 * but it applies to all reference-counting mechanisms):
4463 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004464 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004465
4466/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004467 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004468 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004469 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004470 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004471 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004472garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004473{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004474 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004475 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004476 buf_T *buf;
4477 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004478 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004479 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004480
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004481 if (!testing)
4482 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004483 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004484 want_garbage_collect = FALSE;
4485 may_garbage_collect = FALSE;
4486 garbage_collect_at_exit = FALSE;
4487 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004488
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004489 // The execution stack can grow big, limit the size.
4490 if (exestack.ga_maxlen - exestack.ga_len > 500)
4491 {
4492 size_t new_len;
4493 char_u *pp;
4494 int n;
4495
4496 // Keep 150% of the current size, with a minimum of the growth size.
4497 n = exestack.ga_len / 2;
4498 if (n < exestack.ga_growsize)
4499 n = exestack.ga_growsize;
4500
4501 // Don't make it bigger though.
4502 if (exestack.ga_len + n < exestack.ga_maxlen)
4503 {
4504 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4505 pp = vim_realloc(exestack.ga_data, new_len);
4506 if (pp == NULL)
4507 return FAIL;
4508 exestack.ga_maxlen = exestack.ga_len + n;
4509 exestack.ga_data = pp;
4510 }
4511 }
4512
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004513 // We advance by two because we add one for items referenced through
4514 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004515 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004516
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004517 /*
4518 * 1. Go through all accessible variables and mark all lists and dicts
4519 * with copyID.
4520 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004521
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004522 // Don't free variables in the previous_funccal list unless they are only
4523 // referenced through previous_funccal. This must be first, because if
4524 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004525 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004526
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004527 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004528 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004529
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004530 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004531 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004532 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4533 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004534
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004535 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004536 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004537 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4538 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004539 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004540 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4541 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004542#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004543 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004544 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4545 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004546 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004547 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004548 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4549 NULL, NULL);
4550#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004551
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004552 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004553 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004554 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4555 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004556 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004557 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004558
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004559 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004560 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004561
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004562 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004563 abort = abort || set_ref_in_functions(copyID);
4564
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004565 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004566 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004567
Bram Moolenaar7509ad82021-12-14 18:14:37 +00004568 // funcstacks keep variables for closures
4569 abort = abort || set_ref_in_funcstacks(copyID);
4570
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004571 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004572 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004573
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004574 // callbacks in buffers
4575 abort = abort || set_ref_in_buffers(copyID);
4576
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00004577 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
4578 abort = abort || set_ref_in_insexpand_funcs(copyID);
4579
4580 // 'operatorfunc' callback
4581 abort = abort || set_ref_in_opfunc(copyID);
4582
4583 // 'tagfunc' callback
4584 abort = abort || set_ref_in_tagfunc(copyID);
4585
4586 // 'imactivatefunc' and 'imstatusfunc' callbacks
4587 abort = abort || set_ref_in_im_funcs(copyID);
4588
Bram Moolenaar1dced572012-04-05 16:54:08 +02004589#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004590 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004591#endif
4592
Bram Moolenaardb913952012-06-29 12:54:53 +02004593#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004594 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004595#endif
4596
4597#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004598 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004599#endif
4600
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004601#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004602 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004603 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004604#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004605#ifdef FEAT_NETBEANS_INTG
4606 abort = abort || set_ref_in_nb_channel(copyID);
4607#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004608
Bram Moolenaare3188e22016-05-31 21:13:04 +02004609#ifdef FEAT_TIMERS
4610 abort = abort || set_ref_in_timer(copyID);
4611#endif
4612
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004613#ifdef FEAT_QUICKFIX
4614 abort = abort || set_ref_in_quickfix(copyID);
4615#endif
4616
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004617#ifdef FEAT_TERMINAL
4618 abort = abort || set_ref_in_term(copyID);
4619#endif
4620
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004621#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004622 abort = abort || set_ref_in_popups(copyID);
4623#endif
4624
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004625 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004626 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004627 /*
4628 * 2. Free lists and dictionaries that are not referenced.
4629 */
4630 did_free = free_unref_items(copyID);
4631
4632 /*
4633 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004634 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004635 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004636 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004637 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004638 else if (p_verbose > 0)
4639 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004640 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004641 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004642
4643 return did_free;
4644}
4645
4646/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004647 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004648 */
4649 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004650free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004651{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004652 int did_free = FALSE;
4653
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004654 // Let all "free" functions know that we are here. This means no
4655 // dictionaries, lists, channels or jobs are to be freed, because we will
4656 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004657 in_free_unref_items = TRUE;
4658
4659 /*
4660 * PASS 1: free the contents of the items. We don't free the items
4661 * themselves yet, so that it is possible to decrement refcount counters
4662 */
4663
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004664 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004665 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004666
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004667 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004668 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004669
4670#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004671 // Go through the list of jobs and free items without the copyID. This
4672 // must happen before doing channels, because jobs refer to channels, but
4673 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004674 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4675
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004676 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004677 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4678#endif
4679
4680 /*
4681 * PASS 2: free the items themselves.
4682 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004683 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004684 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004685
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004686#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004687 // Go through the list of jobs and free items without the copyID. This
4688 // must happen before doing channels, because jobs refer to channels, but
4689 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004690 free_unused_jobs(copyID, COPYID_MASK);
4691
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004692 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004693 free_unused_channels(copyID, COPYID_MASK);
4694#endif
4695
4696 in_free_unref_items = FALSE;
4697
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004698 return did_free;
4699}
4700
4701/*
4702 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004703 * "list_stack" is used to add lists to be marked. Can be NULL.
4704 *
4705 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004706 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004707 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004708set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004709{
4710 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004711 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004712 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004713 hashtab_T *cur_ht;
4714 ht_stack_T *ht_stack = NULL;
4715 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004716
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004717 cur_ht = ht;
4718 for (;;)
4719 {
4720 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004721 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004722 // Mark each item in the hashtab. If the item contains a hashtab
4723 // it is added to ht_stack, if it contains a list it is added to
4724 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004725 todo = (int)cur_ht->ht_used;
4726 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4727 if (!HASHITEM_EMPTY(hi))
4728 {
4729 --todo;
4730 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4731 &ht_stack, list_stack);
4732 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004733 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004734
4735 if (ht_stack == NULL)
4736 break;
4737
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004738 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004739 cur_ht = ht_stack->ht;
4740 tempitem = ht_stack;
4741 ht_stack = ht_stack->prev;
4742 free(tempitem);
4743 }
4744
4745 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004746}
4747
Dominique Pelle748b3082022-01-08 12:41:16 +00004748#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
4749 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004750/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004751 * Mark a dict and its items with "copyID".
4752 * Returns TRUE if setting references failed somehow.
4753 */
4754 int
4755set_ref_in_dict(dict_T *d, int copyID)
4756{
4757 if (d != NULL && d->dv_copyID != copyID)
4758 {
4759 d->dv_copyID = copyID;
4760 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4761 }
4762 return FALSE;
4763}
Dominique Pelle748b3082022-01-08 12:41:16 +00004764#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004765
4766/*
4767 * Mark a list and its items with "copyID".
4768 * Returns TRUE if setting references failed somehow.
4769 */
4770 int
4771set_ref_in_list(list_T *ll, int copyID)
4772{
4773 if (ll != NULL && ll->lv_copyID != copyID)
4774 {
4775 ll->lv_copyID = copyID;
4776 return set_ref_in_list_items(ll, copyID, NULL);
4777 }
4778 return FALSE;
4779}
4780
4781/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004782 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004783 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4784 *
4785 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004786 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004787 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004788set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004789{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004790 listitem_T *li;
4791 int abort = FALSE;
4792 list_T *cur_l;
4793 list_stack_T *list_stack = NULL;
4794 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004795
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004796 cur_l = l;
4797 for (;;)
4798 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004799 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004800 // Mark each item in the list. If the item contains a hashtab
4801 // it is added to ht_stack, if it contains a list it is added to
4802 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004803 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4804 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4805 ht_stack, &list_stack);
4806 if (list_stack == NULL)
4807 break;
4808
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004809 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004810 cur_l = list_stack->list;
4811 tempitem = list_stack;
4812 list_stack = list_stack->prev;
4813 free(tempitem);
4814 }
4815
4816 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004817}
4818
4819/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00004820 * Mark the partial in callback 'cb' with "copyID".
4821 */
4822 int
4823set_ref_in_callback(callback_T *cb, int copyID)
4824{
4825 typval_T tv;
4826
4827 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
4828 return FALSE;
4829
4830 tv.v_type = VAR_PARTIAL;
4831 tv.vval.v_partial = cb->cb_partial;
4832 return set_ref_in_item(&tv, copyID, NULL, NULL);
4833}
4834
4835/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004836 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004837 * "list_stack" is used to add lists to be marked. Can be NULL.
4838 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4839 *
4840 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004841 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004842 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004843set_ref_in_item(
4844 typval_T *tv,
4845 int copyID,
4846 ht_stack_T **ht_stack,
4847 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004848{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004849 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004850
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004851 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004852 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004853 dict_T *dd = tv->vval.v_dict;
4854
Bram Moolenaara03f2332016-02-06 18:09:59 +01004855 if (dd != NULL && dd->dv_copyID != copyID)
4856 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004857 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004858 dd->dv_copyID = copyID;
4859 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004860 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004861 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4862 }
4863 else
4864 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004865 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4866
Bram Moolenaara03f2332016-02-06 18:09:59 +01004867 if (newitem == NULL)
4868 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004869 else
4870 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004871 newitem->ht = &dd->dv_hashtab;
4872 newitem->prev = *ht_stack;
4873 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004874 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004875 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004876 }
4877 }
4878 else if (tv->v_type == VAR_LIST)
4879 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004880 list_T *ll = tv->vval.v_list;
4881
Bram Moolenaara03f2332016-02-06 18:09:59 +01004882 if (ll != NULL && ll->lv_copyID != copyID)
4883 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004884 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004885 ll->lv_copyID = copyID;
4886 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004887 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004888 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004889 }
4890 else
4891 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004892 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4893
Bram Moolenaara03f2332016-02-06 18:09:59 +01004894 if (newitem == NULL)
4895 abort = TRUE;
4896 else
4897 {
4898 newitem->list = ll;
4899 newitem->prev = *list_stack;
4900 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004901 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004902 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004903 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004904 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004905 else if (tv->v_type == VAR_FUNC)
4906 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004907 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004908 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004909 else if (tv->v_type == VAR_PARTIAL)
4910 {
4911 partial_T *pt = tv->vval.v_partial;
4912 int i;
4913
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004914 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004915 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004916 // Didn't see this partial yet.
4917 pt->pt_copyID = copyID;
4918
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004919 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004920
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004921 if (pt->pt_dict != NULL)
4922 {
4923 typval_T dtv;
4924
4925 dtv.v_type = VAR_DICT;
4926 dtv.vval.v_dict = pt->pt_dict;
4927 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4928 }
4929
4930 for (i = 0; i < pt->pt_argc; ++i)
4931 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4932 ht_stack, list_stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +00004933 // pt_funcstack is handled in set_ref_in_funcstacks()
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004934 }
4935 }
4936#ifdef FEAT_JOB_CHANNEL
4937 else if (tv->v_type == VAR_JOB)
4938 {
4939 job_T *job = tv->vval.v_job;
4940 typval_T dtv;
4941
4942 if (job != NULL && job->jv_copyID != copyID)
4943 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004944 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004945 if (job->jv_channel != NULL)
4946 {
4947 dtv.v_type = VAR_CHANNEL;
4948 dtv.vval.v_channel = job->jv_channel;
4949 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4950 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004951 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004952 {
4953 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004954 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004955 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4956 }
4957 }
4958 }
4959 else if (tv->v_type == VAR_CHANNEL)
4960 {
4961 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004962 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004963 typval_T dtv;
4964 jsonq_T *jq;
4965 cbq_T *cq;
4966
4967 if (ch != NULL && ch->ch_copyID != copyID)
4968 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004969 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004970 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004971 {
4972 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4973 jq = jq->jq_next)
4974 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4975 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4976 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004977 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004978 {
4979 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004980 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004981 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4982 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004983 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004984 {
4985 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004986 dtv.vval.v_partial =
4987 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004988 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4989 }
4990 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004991 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004992 {
4993 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004994 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004995 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4996 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004997 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004998 {
4999 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005000 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005001 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5002 }
5003 }
5004 }
5005#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005006 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005007}
5008
Bram Moolenaar8c711452005-01-14 21:53:12 +00005009/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005010 * Return a string with the string representation of a variable.
5011 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005012 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005013 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005014 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005015 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005016 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005017 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5018 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005019 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005020 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005021 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005022echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005023 typval_T *tv,
5024 char_u **tofree,
5025 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005026 int copyID,
5027 int echo_style,
5028 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005029 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005030{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005031 static int recurse = 0;
5032 char_u *r = NULL;
5033
Bram Moolenaar33570922005-01-25 22:26:29 +00005034 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005035 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005036 if (!did_echo_string_emsg)
5037 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005038 // Only give this message once for a recursive call to avoid
5039 // flooding the user with errors. And stop iterating over lists
5040 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005041 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005042 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005043 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005044 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005045 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005046 }
5047 ++recurse;
5048
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005049 switch (tv->v_type)
5050 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005051 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005052 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005053 {
5054 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005055 r = tv->vval.v_string;
5056 if (r == NULL)
5057 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005058 }
5059 else
5060 {
5061 *tofree = string_quote(tv->vval.v_string, FALSE);
5062 r = *tofree;
5063 }
5064 break;
5065
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005066 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005067 if (echo_style)
5068 {
5069 *tofree = NULL;
5070 r = tv->vval.v_string;
5071 }
5072 else
5073 {
5074 *tofree = string_quote(tv->vval.v_string, TRUE);
5075 r = *tofree;
5076 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005077 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005078
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005079 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005080 {
5081 partial_T *pt = tv->vval.v_partial;
5082 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005083 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005084 garray_T ga;
5085 int i;
5086 char_u *tf;
5087
5088 ga_init2(&ga, 1, 100);
5089 ga_concat(&ga, (char_u *)"function(");
5090 if (fname != NULL)
5091 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005092 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005093 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005094 && vim_isupper(fname[1]))
5095 {
5096 ga_concat(&ga, (char_u *)"'g:");
5097 ga_concat(&ga, fname + 1);
5098 }
5099 else
5100 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005101 vim_free(fname);
5102 }
5103 if (pt != NULL && pt->pt_argc > 0)
5104 {
5105 ga_concat(&ga, (char_u *)", [");
5106 for (i = 0; i < pt->pt_argc; ++i)
5107 {
5108 if (i > 0)
5109 ga_concat(&ga, (char_u *)", ");
5110 ga_concat(&ga,
5111 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5112 vim_free(tf);
5113 }
5114 ga_concat(&ga, (char_u *)"]");
5115 }
5116 if (pt != NULL && pt->pt_dict != NULL)
5117 {
5118 typval_T dtv;
5119
5120 ga_concat(&ga, (char_u *)", ");
5121 dtv.v_type = VAR_DICT;
5122 dtv.vval.v_dict = pt->pt_dict;
5123 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5124 vim_free(tf);
5125 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005126 // terminate with ')' and a NUL
5127 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005128
5129 *tofree = ga.ga_data;
5130 r = *tofree;
5131 break;
5132 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005133
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005134 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005135 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005136 break;
5137
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005138 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005139 if (tv->vval.v_list == NULL)
5140 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005141 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005142 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005143 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005144 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005145 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5146 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005147 {
5148 *tofree = NULL;
5149 r = (char_u *)"[...]";
5150 }
5151 else
5152 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005153 int old_copyID = tv->vval.v_list->lv_copyID;
5154
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005155 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005156 *tofree = list2string(tv, copyID, restore_copyID);
5157 if (restore_copyID)
5158 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005159 r = *tofree;
5160 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005161 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005162
Bram Moolenaar8c711452005-01-14 21:53:12 +00005163 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005164 if (tv->vval.v_dict == NULL)
5165 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005166 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005167 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005168 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005169 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005170 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5171 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005172 {
5173 *tofree = NULL;
5174 r = (char_u *)"{...}";
5175 }
5176 else
5177 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005178 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005179
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005180 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005181 *tofree = dict2string(tv, copyID, restore_copyID);
5182 if (restore_copyID)
5183 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005184 r = *tofree;
5185 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005186 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005187
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005188 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005189 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005190 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005191 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005192 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005193 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005194 break;
5195
Bram Moolenaar835dc632016-02-07 14:27:38 +01005196 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005197 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005198#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005199 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005200 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5201 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005202 if (composite_val)
5203 {
5204 *tofree = string_quote(r, FALSE);
5205 r = *tofree;
5206 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005207#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005208 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005209
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005210 case VAR_INSTR:
5211 *tofree = NULL;
5212 r = (char_u *)"instructions";
5213 break;
5214
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005215 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005216#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005217 *tofree = NULL;
5218 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5219 r = numbuf;
5220 break;
5221#endif
5222
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005223 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005224 case VAR_SPECIAL:
5225 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005226 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005227 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005228 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005229
Bram Moolenaar8502c702014-06-17 12:51:16 +02005230 if (--recurse == 0)
5231 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005232 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005233}
5234
5235/*
5236 * Return a string with the string representation of a variable.
5237 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5238 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005239 * Does not put quotes around strings, as ":echo" displays values.
5240 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5241 * May return NULL.
5242 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005243 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005244echo_string(
5245 typval_T *tv,
5246 char_u **tofree,
5247 char_u *numbuf,
5248 int copyID)
5249{
5250 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5251}
5252
5253/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005254 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5255 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005256 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005257 */
5258 int
5259buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5260{
5261 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005262 char_u *t;
5263 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005264
5265 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5266 return -1;
5267
5268 if (lnum > buf->b_ml.ml_line_count)
5269 lnum = buf->b_ml.ml_line_count;
5270
5271 str = ml_get_buf(buf, lnum, FALSE);
5272 if (str == NULL)
5273 return -1;
5274
5275 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005276 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005277
Bram Moolenaar91458462021-01-13 20:08:38 +01005278 // count the number of characters
5279 t = str;
5280 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5281 t += mb_ptr2len(t);
5282
5283 // In insert mode, when the cursor is at the end of a non-empty line,
5284 // byteidx points to the NUL character immediately past the end of the
5285 // string. In this case, add one to the character count.
5286 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5287 count++;
5288
5289 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005290}
5291
5292/*
5293 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005294 * byte index. Works only for loaded buffers. Returns -1 on failure.
5295 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005296 */
5297 int
5298buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5299{
5300 char_u *str;
5301 char_u *t;
5302
5303 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5304 return -1;
5305
5306 if (lnum > buf->b_ml.ml_line_count)
5307 lnum = buf->b_ml.ml_line_count;
5308
5309 str = ml_get_buf(buf, lnum, FALSE);
5310 if (str == NULL)
5311 return -1;
5312
5313 // Convert the character offset to a byte offset
5314 t = str;
5315 while (*t != NUL && --charidx > 0)
5316 t += mb_ptr2len(t);
5317
Bram Moolenaar91458462021-01-13 20:08:38 +01005318 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005319}
5320
5321/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005323 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005325 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005326var2fpos(
5327 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005328 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005329 int *fnum, // set to fnum for '0, 'A, etc.
5330 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005332 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005334 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005336 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005337 if (varp->v_type == VAR_LIST)
5338 {
5339 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005340 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005341 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005342 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005343
5344 l = varp->vval.v_list;
5345 if (l == NULL)
5346 return NULL;
5347
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005348 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005349 pos.lnum = list_find_nr(l, 0L, &error);
5350 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005351 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005352 if (charcol)
5353 len = (long)mb_charlen(ml_get(pos.lnum));
5354 else
5355 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005356
Bram Moolenaarec65d772020-08-20 22:29:12 +02005357 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005358 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005359 li = list_find(l, 1L);
5360 if (li != NULL && li->li_tv.v_type == VAR_STRING
5361 && li->li_tv.vval.v_string != NULL
5362 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005363 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005364 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005365 }
5366 else
5367 {
5368 pos.col = list_find_nr(l, 1L, &error);
5369 if (error)
5370 return NULL;
5371 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005372
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005373 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005374 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005375 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005376 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005377
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005378 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005379 pos.coladd = list_find_nr(l, 2L, &error);
5380 if (error)
5381 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005382
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005383 return &pos;
5384 }
5385
Bram Moolenaarc5809432021-03-27 21:23:30 +01005386 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5387 return NULL;
5388
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005389 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005390 if (name == NULL)
5391 return NULL;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005392 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005393 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005394 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005395 pos = curwin->w_cursor;
5396 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005397 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005398 return &pos;
5399 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005400 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005401 {
5402 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005403 pos = VIsual;
5404 else
5405 pos = curwin->w_cursor;
5406 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005407 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005408 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005409 }
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005410 if (name[0] == '\'' && (!in_vim9script()
5411 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005413 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005414 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5416 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005417 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005418 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 return pp;
5420 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005421
Bram Moolenaara5525202006-03-02 22:52:09 +00005422 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005423
Bram Moolenaar477933c2007-07-17 14:32:23 +00005424 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005425 {
5426 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005427 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005428 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005429 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005430 // In silent Ex mode topline is zero, but that's not a valid line
5431 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005432 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005433 return &pos;
5434 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005435 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005436 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005437 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005438 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005439 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005440 return &pos;
5441 }
5442 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005443 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005445 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 {
5447 pos.lnum = curbuf->b_ml.ml_line_count;
5448 pos.col = 0;
5449 }
5450 else
5451 {
5452 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005453 if (charcol)
5454 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5455 else
5456 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 }
5458 return &pos;
5459 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005460 if (in_vim9script())
5461 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 return NULL;
5463}
5464
5465/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005466 * Convert list in "arg" into a position and optional file number.
5467 * When "fnump" is NULL there is no file number, only 3 items.
5468 * Note that the column is passed on as-is, the caller may want to decrement
5469 * it to use 1 for the first column.
5470 * Return FAIL when conversion is not possible, doesn't check the position for
5471 * validity.
5472 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005473 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005474list2fpos(
5475 typval_T *arg,
5476 pos_T *posp,
5477 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005478 colnr_T *curswantp,
5479 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005480{
5481 list_T *l = arg->vval.v_list;
5482 long i = 0;
5483 long n;
5484
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005485 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5486 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005487 if (arg->v_type != VAR_LIST
5488 || l == NULL
5489 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005490 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005491 return FAIL;
5492
5493 if (fnump != NULL)
5494 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005495 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005496 if (n < 0)
5497 return FAIL;
5498 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005499 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005500 *fnump = n;
5501 }
5502
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005503 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005504 if (n < 0)
5505 return FAIL;
5506 posp->lnum = n;
5507
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005508 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005509 if (n < 0)
5510 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005511 // If character position is specified, then convert to byte position
5512 if (charcol)
5513 {
5514 buf_T *buf;
5515
5516 // Get the text for the specified line in a loaded buffer
5517 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5518 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5519 return FAIL;
5520
Bram Moolenaar91458462021-01-13 20:08:38 +01005521 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005522 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005523 posp->col = n;
5524
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005525 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005526 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005527 posp->coladd = 0;
5528 else
5529 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005530
Bram Moolenaar493c1782014-05-28 14:34:46 +02005531 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005532 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005533
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005534 return OK;
5535}
5536
5537/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 * Get the length of an environment variable name.
5539 * Advance "arg" to the first character after the name.
5540 * Return 0 for error.
5541 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005542 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005543get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544{
5545 char_u *p;
5546 int len;
5547
5548 for (p = *arg; vim_isIDc(*p); ++p)
5549 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005550 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 return 0;
5552
5553 len = (int)(p - *arg);
5554 *arg = p;
5555 return len;
5556}
5557
5558/*
5559 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005560 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 * Return 0 if something is wrong.
5562 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005563 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005564get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565{
5566 char_u *p;
5567 int len;
5568
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005569 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005571 {
5572 if (*p == ':')
5573 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005574 // "s:" is start of "s:var", but "n:" is not and can be used in
5575 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005576 len = (int)(p - *arg);
5577 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5578 || len > 1)
5579 break;
5580 }
5581 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005582 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 return 0;
5584
5585 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005586 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587
5588 return len;
5589}
5590
5591/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005592 * Get the length of the name of a variable or function.
5593 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005595 * Return -1 if curly braces expansion failed.
5596 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 * If the name contains 'magic' {}'s, expand them and return the
5598 * expanded name in an allocated string via 'alias' - caller must free.
5599 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005600 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005601get_name_len(
5602 char_u **arg,
5603 char_u **alias,
5604 int evaluate,
5605 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606{
5607 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 char_u *p;
5609 char_u *expr_start;
5610 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005612 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613
5614 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5615 && (*arg)[2] == (int)KE_SNR)
5616 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005617 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 *arg += 3;
5619 return get_id_len(arg) + 3;
5620 }
5621 len = eval_fname_script(*arg);
5622 if (len > 0)
5623 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005624 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 *arg += len;
5626 }
5627
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005629 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005631 p = find_name_end(*arg, &expr_start, &expr_end,
5632 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 if (expr_start != NULL)
5634 {
5635 char_u *temp_string;
5636
5637 if (!evaluate)
5638 {
5639 len += (int)(p - *arg);
5640 *arg = skipwhite(p);
5641 return len;
5642 }
5643
5644 /*
5645 * Include any <SID> etc in the expanded string:
5646 * Thus the -len here.
5647 */
5648 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5649 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005650 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 *alias = temp_string;
5652 *arg = skipwhite(p);
5653 return (int)STRLEN(temp_string);
5654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655
5656 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005657 // Only give an error when there is something, otherwise it will be
5658 // reported at a higher level.
5659 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005660 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661
5662 return len;
5663}
5664
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005665/*
5666 * Find the end of a variable or function name, taking care of magic braces.
5667 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5668 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005669 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005670 * Return a pointer to just after the name. Equal to "arg" if there is no
5671 * valid name.
5672 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005673 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005674find_name_end(
5675 char_u *arg,
5676 char_u **expr_start,
5677 char_u **expr_end,
5678 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005680 int mb_nest = 0;
5681 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005683 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005684 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005686 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005688 *expr_start = NULL;
5689 *expr_end = NULL;
5690 }
5691
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005692 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005693 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5694 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005695 return arg;
5696
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005697 for (p = arg; *p != NUL
5698 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005699 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005700 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005701 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005702 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005703 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005704 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005705 if (*p == '\'')
5706 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005707 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005708 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005709 ;
5710 if (*p == NUL)
5711 break;
5712 }
5713 else if (*p == '"')
5714 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005715 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005716 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005717 if (*p == '\\' && p[1] != NUL)
5718 ++p;
5719 if (*p == NUL)
5720 break;
5721 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005722 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5723 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005724 // "s:" is start of "s:var", but "n:" is not and can be used in
5725 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005726 len = (int)(p - arg);
5727 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005728 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005729 break;
5730 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005731
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005732 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005734 if (*p == '[')
5735 ++br_nest;
5736 else if (*p == ']')
5737 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005739
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005740 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005742 if (*p == '{')
5743 {
5744 mb_nest++;
5745 if (expr_start != NULL && *expr_start == NULL)
5746 *expr_start = p;
5747 }
5748 else if (*p == '}')
5749 {
5750 mb_nest--;
5751 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5752 *expr_end = p;
5753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 }
5756
5757 return p;
5758}
5759
5760/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005761 * Expands out the 'magic' {}'s in a variable/function name.
5762 * Note that this can call itself recursively, to deal with
5763 * constructs like foo{bar}{baz}{bam}
5764 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5765 * "in_start" ^
5766 * "expr_start" ^
5767 * "expr_end" ^
5768 * "in_end" ^
5769 *
5770 * Returns a new allocated string, which the caller must free.
5771 * Returns NULL for failure.
5772 */
5773 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005774make_expanded_name(
5775 char_u *in_start,
5776 char_u *expr_start,
5777 char_u *expr_end,
5778 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005779{
5780 char_u c1;
5781 char_u *retval = NULL;
5782 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005783
5784 if (expr_end == NULL || in_end == NULL)
5785 return NULL;
5786 *expr_start = NUL;
5787 *expr_end = NUL;
5788 c1 = *in_end;
5789 *in_end = NUL;
5790
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005791 temp_result = eval_to_string(expr_start + 1, FALSE);
5792 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005793 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005794 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5795 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005796 if (retval != NULL)
5797 {
5798 STRCPY(retval, in_start);
5799 STRCAT(retval, temp_result);
5800 STRCAT(retval, expr_end + 1);
5801 }
5802 }
5803 vim_free(temp_result);
5804
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005805 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005806 *expr_start = '{';
5807 *expr_end = '}';
5808
5809 if (retval != NULL)
5810 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005811 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005812 if (expr_start != NULL)
5813 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005814 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005815 temp_result = make_expanded_name(retval, expr_start,
5816 expr_end, temp_result);
5817 vim_free(retval);
5818 retval = temp_result;
5819 }
5820 }
5821
5822 return retval;
5823}
5824
5825/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005827 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005829 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005830eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005832 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005833}
5834
5835/*
5836 * Return TRUE if character "c" can be used as the first character in a
5837 * variable or function name (excluding '{' and '}').
5838 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005839 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005840eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005841{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005842 return ASCII_ISALPHA(c) || c == '_';
5843}
5844
5845/*
5846 * Return TRUE if character "c" can be used as the first character of a
5847 * dictionary key.
5848 */
5849 int
5850eval_isdictc(int c)
5851{
5852 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853}
5854
5855/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005856 * Handle:
5857 * - expr[expr], expr[expr:expr] subscript
5858 * - ".name" lookup
5859 * - function call with Funcref variable: func(expr)
5860 * - method call: var->method()
5861 *
5862 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00005863 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005864 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005865 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005866handle_subscript(
5867 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00005868 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01005869 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005870 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005871 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005872{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005873 int evaluate = evalarg != NULL
5874 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005875 int ret = OK;
5876 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005877 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005878 int getnext;
5879 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005880
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005881 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005882 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005883 // When at the end of the line and ".name" or "->{" or "->X" follows in
5884 // the next line then consume the line break.
5885 p = eval_next_non_blank(*arg, evalarg, &getnext);
5886 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005887 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02005888 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
5889 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
5890 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005891 {
5892 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005893 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005894 check_white = FALSE;
5895 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005896
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01005897 if (rettv->v_type == VAR_ANY)
5898 {
5899 char_u *exp_name;
5900 int cc;
5901 int idx;
5902 ufunc_T *ufunc;
5903 type_T *type;
5904
Bram Moolenaard5f400c2022-01-06 21:10:28 +00005905 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00005906 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01005907 if (**arg != '.')
5908 {
5909 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00005910 semsg(_(e_expected_dot_after_name_str),
5911 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01005912 ret = FAIL;
5913 break;
5914 }
5915 ++*arg;
5916 if (IS_WHITE_OR_NUL(**arg))
5917 {
5918 if (verbose)
5919 emsg(_(e_no_white_space_allowed_after_dot));
5920 ret = FAIL;
5921 break;
5922 }
5923
5924 // isolate the name
5925 exp_name = *arg;
5926 while (eval_isnamec(**arg))
5927 ++*arg;
5928 cc = **arg;
5929 **arg = NUL;
5930
5931 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
5932 evalarg->eval_cctx, verbose);
5933 **arg = cc;
5934 *arg = skipwhite(*arg);
5935
5936 if (idx < 0 && ufunc == NULL)
5937 {
5938 ret = FAIL;
5939 break;
5940 }
5941 if (idx >= 0)
5942 {
5943 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
5944 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
5945
5946 copy_tv(sv->sv_tv, rettv);
5947 }
5948 else
5949 {
5950 rettv->v_type = VAR_FUNC;
5951 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
5952 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00005953 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01005954 }
5955
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005956 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5957 || rettv->v_type == VAR_PARTIAL))
5958 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005959 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005960 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5961 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005962
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005963 // Stop the expression evaluation when immediately aborting on
5964 // error, or when an interrupt occurred or an exception was thrown
5965 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005966 if (aborting())
5967 {
5968 if (ret == OK)
5969 clear_tv(rettv);
5970 ret = FAIL;
5971 }
5972 dict_unref(selfdict);
5973 selfdict = NULL;
5974 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005975 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005976 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005977 if (in_vim9script())
5978 *arg = skipwhite(p + 2);
5979 else
5980 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005981 if (ret == OK)
5982 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005983 if (VIM_ISWHITE(**arg))
5984 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00005985 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02005986 ret = FAIL;
5987 }
5988 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005989 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005990 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005991 else
5992 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005993 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005994 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005995 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005996 // "." is ".name" lookup when we found a dict or when evaluating and
5997 // scriptversion is at least 2, where string concatenation is "..".
5998 else if (**arg == '['
5999 || (**arg == '.' && (rettv->v_type == VAR_DICT
6000 || (!evaluate
6001 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006002 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006003 {
6004 dict_unref(selfdict);
6005 if (rettv->v_type == VAR_DICT)
6006 {
6007 selfdict = rettv->vval.v_dict;
6008 if (selfdict != NULL)
6009 ++selfdict->dv_refcount;
6010 }
6011 else
6012 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006013 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006014 {
6015 clear_tv(rettv);
6016 ret = FAIL;
6017 }
6018 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006019 else
6020 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006021 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006022
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006023 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6024 // Don't do this when "Func" is already a partial that was bound
6025 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006026 if (selfdict != NULL
6027 && (rettv->v_type == VAR_FUNC
6028 || (rettv->v_type == VAR_PARTIAL
6029 && (rettv->vval.v_partial->pt_auto
6030 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006031 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006032
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006033 dict_unref(selfdict);
6034 return ret;
6035}
6036
6037/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006038 * Make a copy of an item.
6039 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006040 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6041 * reference to an already copied list/dict can be used.
6042 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006043 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006044 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006045item_copy(
6046 typval_T *from,
6047 typval_T *to,
6048 int deep,
6049 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006050{
6051 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006052 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006053
Bram Moolenaar33570922005-01-25 22:26:29 +00006054 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006055 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006056 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006057 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006058 }
6059 ++recurse;
6060
6061 switch (from->v_type)
6062 {
6063 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006064 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006065 case VAR_STRING:
6066 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006067 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006068 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006069 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006070 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006071 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006072 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006073 copy_tv(from, to);
6074 break;
6075 case VAR_LIST:
6076 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006077 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006078 if (from->vval.v_list == NULL)
6079 to->vval.v_list = NULL;
6080 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6081 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006082 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006083 to->vval.v_list = from->vval.v_list->lv_copylist;
6084 ++to->vval.v_list->lv_refcount;
6085 }
6086 else
6087 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
6088 if (to->vval.v_list == NULL)
6089 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006090 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006091 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006092 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006093 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006094 case VAR_DICT:
6095 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006096 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006097 if (from->vval.v_dict == NULL)
6098 to->vval.v_dict = NULL;
6099 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6100 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006101 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006102 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6103 ++to->vval.v_dict->dv_refcount;
6104 }
6105 else
6106 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
6107 if (to->vval.v_dict == NULL)
6108 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006109 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006110 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006111 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006112 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006113 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006114 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006115 }
6116 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006117 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006118}
6119
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006120 void
6121echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6122{
6123 char_u *tofree;
6124 char_u numbuf[NUMBUFLEN];
6125 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6126
6127 if (*atstart)
6128 {
6129 *atstart = FALSE;
6130 // Call msg_start() after eval1(), evaluating the expression
6131 // may cause a message to appear.
6132 if (with_space)
6133 {
6134 // Mark the saved text as finishing the line, so that what
6135 // follows is displayed on a new line when scrolling back
6136 // at the more prompt.
6137 msg_sb_eol();
6138 msg_start();
6139 }
6140 }
6141 else if (with_space)
6142 msg_puts_attr(" ", echo_attr);
6143
6144 if (p != NULL)
6145 for ( ; *p != NUL && !got_int; ++p)
6146 {
6147 if (*p == '\n' || *p == '\r' || *p == TAB)
6148 {
6149 if (*p != TAB && *needclr)
6150 {
6151 // remove any text still there from the command
6152 msg_clr_eos();
6153 *needclr = FALSE;
6154 }
6155 msg_putchar_attr(*p, echo_attr);
6156 }
6157 else
6158 {
6159 if (has_mbyte)
6160 {
6161 int i = (*mb_ptr2len)(p);
6162
6163 (void)msg_outtrans_len_attr(p, i, echo_attr);
6164 p += i - 1;
6165 }
6166 else
6167 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6168 }
6169 }
6170 vim_free(tofree);
6171}
6172
Bram Moolenaare9a41262005-01-15 22:18:47 +00006173/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 * ":echo expr1 ..." print each argument separated with a space, add a
6175 * newline at the end.
6176 * ":echon expr1 ..." print each argument plain.
6177 */
6178 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006179ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180{
6181 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006182 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006183 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 int needclr = TRUE;
6185 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006186 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006187 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006188 evalarg_T evalarg;
6189
Bram Moolenaare707c882020-07-01 13:07:37 +02006190 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191
6192 if (eap->skip)
6193 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006194 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006196 // If eval1() causes an error message the text from the command may
6197 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006198 need_clr_eos = needclr;
6199
Bram Moolenaar68db9962021-05-09 23:19:22 +02006200 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006201 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 {
6203 /*
6204 * Report the invalid expression unless the expression evaluation
6205 * has been cancelled due to an aborting error, an interrupt, or an
6206 * exception.
6207 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006208 if (!aborting() && did_emsg == did_emsg_before
6209 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006210 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006211 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 break;
6213 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006214 need_clr_eos = FALSE;
6215
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006217 {
6218 if (rettv.v_type == VAR_VOID)
6219 {
6220 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6221 break;
6222 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006223 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006224 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006225
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006226 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 arg = skipwhite(arg);
6228 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006229 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006230 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231
6232 if (eap->skip)
6233 --emsg_skip;
6234 else
6235 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006236 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 if (needclr)
6238 msg_clr_eos();
6239 if (eap->cmdidx == CMD_echo)
6240 msg_end();
6241 }
6242}
6243
6244/*
6245 * ":echohl {name}".
6246 */
6247 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006248ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006250 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251}
6252
6253/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006254 * Returns the :echo attribute
6255 */
6256 int
6257get_echo_attr(void)
6258{
6259 return echo_attr;
6260}
6261
6262/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 * ":execute expr1 ..." execute the result of an expression.
6264 * ":echomsg expr1 ..." Print a message
6265 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006266 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 * Each gets spaces around each argument and a newline at the end for
6268 * echo commands
6269 */
6270 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006271ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272{
6273 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006274 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 int ret = OK;
6276 char_u *p;
6277 garray_T ga;
6278 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006279 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280
6281 ga_init2(&ga, 1, 80);
6282
6283 if (eap->skip)
6284 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006285 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006287 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006288 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290
6291 if (!eap->skip)
6292 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006293 char_u buf[NUMBUFLEN];
6294
6295 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006296 {
6297 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6298 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006299 semsg(_(e_using_invalid_value_as_string_str),
6300 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006301 p = NULL;
6302 }
6303 else
6304 p = tv_get_string_buf(&rettv, buf);
6305 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006306 else
6307 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006308 if (p == NULL)
6309 {
6310 clear_tv(&rettv);
6311 ret = FAIL;
6312 break;
6313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 len = (int)STRLEN(p);
6315 if (ga_grow(&ga, len + 2) == FAIL)
6316 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006317 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 ret = FAIL;
6319 break;
6320 }
6321 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 ga.ga_len += len;
6325 }
6326
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006327 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 arg = skipwhite(arg);
6329 }
6330
6331 if (ret != FAIL && ga.ga_data != NULL)
6332 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006333 // use the first line of continuation lines for messages
6334 SOURCING_LNUM = start_lnum;
6335
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006336 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6337 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006338 // Mark the already saved text as finishing the line, so that what
6339 // follows is displayed on a new line when scrolling back at the
6340 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006341 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006342 }
6343
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006345 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006346 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006347 out_flush();
6348 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006349 else if (eap->cmdidx == CMD_echoconsole)
6350 {
6351 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6352 ui_write((char_u *)"\r\n", 2, TRUE);
6353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 else if (eap->cmdidx == CMD_echoerr)
6355 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006356 int save_did_emsg = did_emsg;
6357
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006358 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006359 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 if (!force_abort)
6361 did_emsg = save_did_emsg;
6362 }
6363 else if (eap->cmdidx == CMD_execute)
6364 do_cmdline((char_u *)ga.ga_data,
6365 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6366 }
6367
6368 ga_clear(&ga);
6369
6370 if (eap->skip)
6371 --emsg_skip;
6372
Bram Moolenaar63b91732021-08-05 20:40:03 +02006373 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374}
6375
6376/*
6377 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6378 * "arg" points to the "&" or '+' when called, to "option" when returning.
6379 * Returns NULL when no option name found. Otherwise pointer to the char
6380 * after the option name.
6381 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006382 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006383find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384{
6385 char_u *p = *arg;
6386
6387 ++p;
6388 if (*p == 'g' && p[1] == ':')
6389 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006390 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 p += 2;
6392 }
6393 else if (*p == 'l' && p[1] == ':')
6394 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006395 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 p += 2;
6397 }
6398 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006399 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400
6401 if (!ASCII_ISALPHA(*p))
6402 return NULL;
6403 *arg = p;
6404
6405 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006406 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 else
6408 while (ASCII_ISALPHA(*p))
6409 ++p;
6410 return p;
6411}
6412
6413/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006414 * Display script name where an item was last set.
6415 * Should only be invoked when 'verbose' is non-zero.
6416 */
6417 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006418last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006419{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006420 char_u *p;
6421
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006422 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006423 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006424 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006425 if (p != NULL)
6426 {
6427 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006428 msg_puts(_("\n\tLast set from "));
6429 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006430 if (script_ctx.sc_lnum > 0)
6431 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006432 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006433 msg_outnum((long)script_ctx.sc_lnum);
6434 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006435 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006436 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006437 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006438 }
6439}
6440
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006441#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442
6443/*
6444 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006445 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 * "flags" can be "g" to do a global substitute.
6447 * Returns an allocated string, NULL for error.
6448 */
6449 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006450do_string_sub(
6451 char_u *str,
6452 char_u *pat,
6453 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006454 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006455 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456{
6457 int sublen;
6458 regmatch_T regmatch;
6459 int i;
6460 int do_all;
6461 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006462 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 garray_T ga;
6464 char_u *ret;
6465 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006466 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006468 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006470 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471
6472 ga_init2(&ga, 1, 200);
6473
6474 do_all = (flags[0] == 'g');
6475
6476 regmatch.rm_ic = p_ic;
6477 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6478 if (regmatch.regprog != NULL)
6479 {
6480 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006481 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6483 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006484 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006485 if (regmatch.startp[0] == regmatch.endp[0])
6486 {
6487 if (zero_width == regmatch.startp[0])
6488 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006489 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006490 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006491 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6492 (size_t)i);
6493 ga.ga_len += i;
6494 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006495 continue;
6496 }
6497 zero_width = regmatch.startp[0];
6498 }
6499
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 /*
6501 * Get some space for a temporary buffer to do the substitution
6502 * into. It will contain:
6503 * - The text up to where the match is.
6504 * - The substituted text.
6505 * - The text after the match.
6506 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006507 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006508 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6510 {
6511 ga_clear(&ga);
6512 break;
6513 }
6514
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006515 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 i = (int)(regmatch.startp[0] - tail);
6517 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006518 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006519 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 + ga.ga_len + i, TRUE, TRUE, FALSE);
6521 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006522 tail = regmatch.endp[0];
6523 if (*tail == NUL)
6524 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 if (!do_all)
6526 break;
6527 }
6528
6529 if (ga.ga_data != NULL)
6530 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6531
Bram Moolenaar473de612013-06-08 18:19:48 +02006532 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 }
6534
6535 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6536 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006537 if (p_cpo == empty_option)
6538 p_cpo = save_cpo;
6539 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006540 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006541 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006542 // If it's still empty it was changed and restored, need to restore in
6543 // the complicated way.
6544 if (*p_cpo == NUL)
6545 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006546 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548
6549 return ret;
6550}