blob: fe4dbd10bb5eb42ea9bf018615de6490f1e07433 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010023#define NAMESPACE_CHAR (char_u *)"abglstvw"
24
Bram Moolenaar532c7802005-01-27 14:44:31 +000025/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000026 * When recursively copying lists and dicts we need to remember which ones we
27 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000028 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000029 */
30static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020031
Bram Moolenaar071d4272004-06-13 20:20:40 +000032/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000033 * Info used by a ":for" loop.
34 */
Bram Moolenaar33570922005-01-25 22:26:29 +000035typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000036{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010037 int fi_semicolon; // TRUE if ending in '; var]'
38 int fi_varcount; // nr of variables in the list
Bram Moolenaarb7a78f72020-06-28 18:43:40 +020039 int fi_break_count; // nr of line breaks encountered
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010040 listwatch_T fi_lw; // keep an eye on the item used.
41 list_T *fi_list; // list being used
42 int fi_bi; // index of blob
43 blob_T *fi_blob; // blob being used
Bram Moolenaar74e54fc2021-03-26 20:41:29 +010044 char_u *fi_string; // copy of string being used
45 int fi_byte_idx; // byte index in fi_string
Bram Moolenaar33570922005-01-25 22:26:29 +000046} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000047
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020048static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
49static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
50static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
51static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
52static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +020053static int eval7t(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020054static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +020055static int eval7_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000056
Bram Moolenaar48e697e2016-01-23 22:17:30 +010057static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020058static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020059
Bram Moolenaare21c1582019-03-02 11:57:09 +010060/*
61 * Return "n1" divided by "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010062 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010063 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020064 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010065num_divide(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010066{
67 varnumber_T result;
68
Bram Moolenaar99880f92021-01-20 21:23:14 +010069 if (n2 == 0)
Bram Moolenaare21c1582019-03-02 11:57:09 +010070 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010071 if (in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010072 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010073 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010074 if (failed != NULL)
75 *failed = TRUE;
76 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010077 if (n1 == 0)
78 result = VARNUM_MIN; // similar to NaN
79 else if (n1 < 0)
80 result = -VARNUM_MAX;
81 else
82 result = VARNUM_MAX;
83 }
84 else
85 result = n1 / n2;
86
87 return result;
88}
89
90/*
91 * Return "n1" modulus "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010092 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010093 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020094 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010095num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010096{
Bram Moolenaar99880f92021-01-20 21:23:14 +010097 if (n2 == 0 && in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010098 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010099 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +0100100 if (failed != NULL)
101 *failed = TRUE;
102 }
Bram Moolenaare21c1582019-03-02 11:57:09 +0100103 return (n2 == 0) ? 0 : (n1 % n2);
104}
105
Bram Moolenaar33570922005-01-25 22:26:29 +0000106/*
107 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000108 */
109 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100110eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000111{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200112 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200113 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000114
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200115#ifdef EBCDIC
116 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100117 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200118 */
119 sortFunctions();
120#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000121}
122
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000123#if defined(EXITFREE) || defined(PROTO)
124 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100125eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000126{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200127 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100128 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200129 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000130
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200131 // autoloaded script names
132 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000133
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200134 // unreferenced lists and dicts
135 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200136
137 // functions not garbage collected
138 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000139}
140#endif
141
Bram Moolenaare6b53242020-07-01 17:28:33 +0200142 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200143fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
144{
Bram Moolenaar844fb642021-10-23 13:32:30 +0100145 init_evalarg(evalarg);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200146 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200147 if (eap != NULL)
Bram Moolenaar37c83712020-06-30 21:18:36 +0200148 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200149 evalarg->eval_cstack = eap->cstack;
150 if (getline_equal(eap->getline, eap->cookie, getsourceline))
151 {
152 evalarg->eval_getline = eap->getline;
153 evalarg->eval_cookie = eap->cookie;
154 }
Bram Moolenaar37c83712020-06-30 21:18:36 +0200155 }
156}
157
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158/*
159 * Top level evaluation function, returning a boolean.
160 * Sets "error" to TRUE if there was an error.
161 * Return TRUE or FALSE.
162 */
163 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100164eval_to_bool(
165 char_u *arg,
166 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200167 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100168 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169{
Bram Moolenaar33570922005-01-25 22:26:29 +0000170 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200171 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200172 evalarg_T evalarg;
173
Bram Moolenaar37c83712020-06-30 21:18:36 +0200174 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175
176 if (skip)
177 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200178 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 else
181 {
182 *error = FALSE;
183 if (!skip)
184 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200185 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200186 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200187 else
188 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000189 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 }
191 }
192 if (skip)
193 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200194 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200196 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197}
198
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100199/*
200 * Call eval1() and give an error message if not done at a lower level.
201 */
202 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200203eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100204{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100205 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100206 int ret;
207 int did_emsg_before = did_emsg;
208 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200209 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100210
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200211 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
212
213 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100214 if (ret == FAIL)
215 {
216 // Report the invalid expression unless the expression evaluation has
217 // been cancelled due to an aborting error, an interrupt, or an
218 // exception, or we already gave a more specific error.
219 // Also check called_emsg for when using assert_fails().
220 if (!aborting() && did_emsg == did_emsg_before
221 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200222 semsg(_(e_invalid_expression_str), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100223 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200224 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100225 return ret;
226}
227
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100228/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200229 * Return whether a typval is a valid expression to pass to eval_expr_typval()
230 * or eval_expr_to_bool(). An empty string returns FALSE;
231 */
232 int
233eval_expr_valid_arg(typval_T *tv)
234{
235 return tv->v_type != VAR_UNKNOWN
236 && (tv->v_type != VAR_STRING
237 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
238}
239
240/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100241 * Evaluate an expression, which can be a function, partial or string.
242 * Pass arguments "argv[argc]".
243 * Return the result in "rettv" and OK or FAIL.
244 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200245 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100246eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
247{
248 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100249 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200250 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100251
252 if (expr->v_type == VAR_FUNC)
253 {
254 s = expr->vval.v_string;
255 if (s == NULL || *s == NUL)
256 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200257 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000258 funcexe.fe_evaluate = TRUE;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200259 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100260 return FAIL;
261 }
262 else if (expr->v_type == VAR_PARTIAL)
263 {
264 partial_T *partial = expr->vval.v_partial;
265
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200266 if (partial == NULL)
267 return FAIL;
268
Bram Moolenaar822ba242020-05-24 23:00:18 +0200269 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200270 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200272 if (call_def_function(partial->pt_func, argc, argv,
273 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100274 return FAIL;
275 }
276 else
277 {
278 s = partial_name(partial);
279 if (s == NULL || *s == NUL)
280 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200281 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000282 funcexe.fe_evaluate = TRUE;
283 funcexe.fe_partial = partial;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100284 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
285 return FAIL;
286 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100287 }
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200288 else if (expr->v_type == VAR_INSTR)
289 {
290 return exe_typval_instr(expr, rettv);
291 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100292 else
293 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100294 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100295 if (s == NULL)
296 return FAIL;
297 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200298 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100299 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200300 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100301 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200302 clear_tv(rettv);
Bram Moolenaar108010a2021-06-27 22:03:33 +0200303 semsg(_(e_invalid_expression_str), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100304 return FAIL;
305 }
306 }
307 return OK;
308}
309
310/*
311 * Like eval_to_bool() but using a typval_T instead of a string.
312 * Works for string, funcref and partial.
313 */
314 int
315eval_expr_to_bool(typval_T *expr, int *error)
316{
317 typval_T rettv;
318 int res;
319
320 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
321 {
322 *error = TRUE;
323 return FALSE;
324 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200325 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100326 clear_tv(&rettv);
327 return res;
328}
329
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330/*
331 * Top level evaluation function, returning a string. If "skip" is TRUE,
332 * only parsing to "nextcmd" is done, without reporting errors. Return
333 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
334 */
335 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100336eval_to_string_skip(
337 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200338 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100339 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340{
Bram Moolenaar33570922005-01-25 22:26:29 +0000341 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200343 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344
Bram Moolenaar37c83712020-06-30 21:18:36 +0200345 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 if (skip)
347 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200348 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 retval = NULL;
350 else
351 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100352 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000353 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 }
355 if (skip)
356 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200357 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358
359 return retval;
360}
361
362/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000363 * Skip over an expression at "*pp".
364 * Return FAIL for an error, OK otherwise.
365 */
366 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200367skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000368{
Bram Moolenaar33570922005-01-25 22:26:29 +0000369 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000370
371 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200372 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000373}
374
375/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200376 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200377 * If in Vim9 script and line breaks are encountered, the lines are
378 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200379 * "arg" is advanced to just after the expression.
380 * "start" is set to the start of the expression, "end" to just after the end.
381 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200382 * Return FAIL for an error, OK otherwise.
383 */
384 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200385skip_expr_concatenate(
386 char_u **arg,
387 char_u **start,
388 char_u **end,
389 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200390{
391 typval_T rettv;
392 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200393 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200394 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200395 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200396 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200397 int evaluate = evalarg == NULL
398 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200399
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200400 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200401 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200402 {
403 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200404 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200405 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200406 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200407 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200408 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200409 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200410
411 // Don't evaluate the expression.
412 if (evalarg != NULL)
413 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200414 *arg = skipwhite(*arg);
415 res = eval1(arg, &rettv, evalarg);
416 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200417 if (evalarg != NULL)
418 evalarg->eval_flags = save_flags;
419
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200420 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200421 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200422 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200423 if (evalarg->eval_ga.ga_len == 1)
424 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200425 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200426 ga_clear(gap);
427 gap->ga_itemsize = 0;
428 }
429 else
430 {
431 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200432 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200433
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200434 // Line breaks encountered, concatenate all the lines.
435 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200436 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200437
438 // free the lines only when using getsourceline()
439 if (evalarg->eval_cookie != NULL)
440 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200441 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200442 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200443 // Do not free the last line, "arg" points into it, free it
444 // later.
445 vim_free(evalarg->eval_tofree);
446 evalarg->eval_tofree =
447 ((char_u **)gap->ga_data)[gap->ga_len - 1];
448 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200449 ga_clear_strings(gap);
450 }
451 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200452 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200453 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200454
455 // free lines that were explicitly marked for freeing
456 ga_clear_strings(freegap);
457 }
458
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200459 gap->ga_itemsize = 0;
460 if (p == NULL)
461 return FAIL;
462 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200463 vim_free(evalarg->eval_tofree_lambda);
464 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200465 // Compute "end" relative to the end.
466 *end = *start + STRLEN(*start) - endoff;
467 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200468 }
469
470 return res;
471}
472
473/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100474 * Convert "tv" to a string.
475 * When "convert" is TRUE convert a List into a sequence of lines and convert
476 * a Float to a String.
477 * Returns an allocated string (NULL when out of memory).
478 */
479 char_u *
480typval2string(typval_T *tv, int convert)
481{
482 garray_T ga;
483 char_u *retval;
484#ifdef FEAT_FLOAT
485 char_u numbuf[NUMBUFLEN];
486#endif
487
488 if (convert && tv->v_type == VAR_LIST)
489 {
490 ga_init2(&ga, (int)sizeof(char), 80);
491 if (tv->vval.v_list != NULL)
492 {
493 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
494 if (tv->vval.v_list->lv_len > 0)
495 ga_append(&ga, NL);
496 }
497 ga_append(&ga, NUL);
498 retval = (char_u *)ga.ga_data;
499 }
500#ifdef FEAT_FLOAT
501 else if (convert && tv->v_type == VAR_FLOAT)
502 {
503 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
504 retval = vim_strsave(numbuf);
505 }
506#endif
507 else
508 retval = vim_strsave(tv_get_string(tv));
509 return retval;
510}
511
512/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200513 * Top level evaluation function, returning a string. Does not handle line
514 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000515 * When "convert" is TRUE convert a List into a sequence of lines and convert
516 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 * Return pointer to allocated memory, or NULL for failure.
518 */
519 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100520eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100521 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100522 int convert,
523 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524{
Bram Moolenaar33570922005-01-25 22:26:29 +0000525 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100527 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100529 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
530 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 retval = NULL;
532 else
533 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100534 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000535 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100537 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538
539 return retval;
540}
541
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100542 char_u *
543eval_to_string(
544 char_u *arg,
545 int convert)
546{
547 return eval_to_string_eap(arg, convert, NULL);
548}
549
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000551 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200552 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200553 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 */
555 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100556eval_to_string_safe(
557 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100558 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559{
560 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200561 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200562 int save_sc_version = current_sctx.sc_version;
Christian Brabandt070ac342021-09-09 12:12:03 +0200563 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200565 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200566 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000567 if (use_sandbox)
568 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200569 ++textwinlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200570 may_garbage_collect = FALSE;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200571 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000572 if (use_sandbox)
573 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200574 --textwinlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200575 may_garbage_collect = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200576 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200577 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 return retval;
579}
580
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581/*
582 * Top level evaluation function, returning a number.
583 * Evaluates "expr" silently.
584 * Returns -1 for an error.
585 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200586 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100587eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588{
Bram Moolenaar33570922005-01-25 22:26:29 +0000589 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200590 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000591 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592
593 ++emsg_off;
594
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200595 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 retval = -1;
597 else
598 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100599 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000600 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 }
602 --emsg_off;
603
604 return retval;
605}
606
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000607/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000608 * Top level evaluation function.
609 * Returns an allocated typval_T with the result.
610 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000611 */
612 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200613eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000614{
615 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200616 evalarg_T evalarg;
617
618 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000619
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200620 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200621 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100622 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000623
Bram Moolenaar37c83712020-06-30 21:18:36 +0200624 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000625 return tv;
626}
627
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100629 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200630 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
631 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000632 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200634 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100635call_vim_function(
636 char_u *func,
637 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200638 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200639 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000641 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200642 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100644 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200645 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000646 funcexe.fe_firstline = curwin->w_cursor.lnum;
647 funcexe.fe_lastline = curwin->w_cursor.lnum;
648 funcexe.fe_evaluate = TRUE;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200649 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000650 if (ret == FAIL)
651 clear_tv(rettv);
652
653 return ret;
654}
655
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100656/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100657 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100658 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200659 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
660 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100661 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200662 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100663call_func_retnr(
664 char_u *func,
665 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200666 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100667{
668 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200669 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100670
Bram Moolenaarded27a12018-08-01 19:06:03 +0200671 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100672 return -1;
673
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100674 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100675 clear_tv(&rettv);
676 return retval;
677}
678
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000679/*
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100680 * Call Vim script function like call_func_retnr() and drop the result.
681 * Returns FAIL when calling the function fails.
682 */
683 int
684call_func_noret(
685 char_u *func,
686 int argc,
687 typval_T *argv)
688{
689 typval_T rettv;
690
691 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
692 return FAIL;
693 clear_tv(&rettv);
694 return OK;
695}
696
697/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100698 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100699 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000700 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000701 */
702 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100703call_func_retstr(
704 char_u *func,
705 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200706 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000707{
708 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000709 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000710
Bram Moolenaarded27a12018-08-01 19:06:03 +0200711 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000712 return NULL;
713
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100714 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000715 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 return retval;
717}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000718
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000719/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100720 * Call Vim script function "func" and return the result as a List.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100721 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000722 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000723 */
724 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100725call_func_retlist(
726 char_u *func,
727 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200728 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000729{
730 typval_T rettv;
731
Bram Moolenaarded27a12018-08-01 19:06:03 +0200732 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000733 return NULL;
734
735 if (rettv.v_type != VAR_LIST)
736 {
737 clear_tv(&rettv);
738 return NULL;
739 }
740
741 return rettv.vval.v_list;
742}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744#ifdef FEAT_FOLDING
745/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100746 * Evaluate "arg", which is 'foldexpr'.
747 * Note: caller must set "curwin" to match "arg".
748 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
749 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 */
751 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100752eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753{
Bram Moolenaar33570922005-01-25 22:26:29 +0000754 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200755 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000757 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
758 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759
760 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000761 if (use_sandbox)
762 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200763 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200765 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 retval = 0;
767 else
768 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100769 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000770 if (tv.v_type == VAR_NUMBER)
771 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000772 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 retval = 0;
774 else
775 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100776 // If the result is a string, check if there is a non-digit before
777 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000778 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 if (!VIM_ISDIGIT(*s) && *s != '-')
780 *cp = *s++;
781 retval = atol((char *)s);
782 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000783 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 }
785 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000786 if (use_sandbox)
787 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200788 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200789 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200791 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792}
793#endif
794
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000796 * Get an lval: variable, Dict item or List item that can be assigned a value
797 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
798 * "name.key", "name.key[expr]" etc.
799 * Indexing only works if "name" is an existing List or Dictionary.
800 * "name" points to the start of the name.
801 * If "rettv" is not NULL it points to the value to be assigned.
802 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
803 * wrong; must end in space or cmd separator.
804 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100805 * flags:
806 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100807 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100808 * GLV_NO_AUTOLOAD: do not use script autoloading
809 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000810 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000811 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000812 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000813 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200814 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100815get_lval(
816 char_u *name,
817 typval_T *rettv,
818 lval_T *lp,
819 int unlet,
820 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100821 int flags, // GLV_ values
822 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000823{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000824 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000825 char_u *expr_start, *expr_end;
826 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000827 dictitem_T *v;
828 typval_T var1;
829 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000830 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000831 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000832 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100833 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100834 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100835 int writing;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000836
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100837 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200838 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000839
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100840 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000841 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100842 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000843 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200844 lp->ll_name_end = find_name_end(name, NULL, NULL,
845 FNE_INCL_BR | fne_flags);
846 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000847 }
848
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100849 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000850 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100851 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000852 if (expr_start != NULL)
853 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100854 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100855 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000856 && *p != '[' && *p != '.')
857 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000858 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000859 return NULL;
860 }
861
862 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
863 if (lp->ll_exp_name == NULL)
864 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100865 // Report an invalid expression in braces, unless the
866 // expression evaluation has been cancelled due to an
867 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000868 if (!aborting() && !quiet)
869 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000870 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000871 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000872 return NULL;
873 }
874 }
875 lp->ll_name = lp->ll_exp_name;
876 }
877 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100878 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000879 lp->ll_name = name;
880
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200881 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100882 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200883 // "a: type" is declaring variable "a" with a type, not "a:".
884 if (p == name + 2 && p[-1] == ':')
885 {
886 --p;
887 lp->ll_name_end = p;
888 }
889 if (*p == ':')
890 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000891 garray_T tmp_type_list;
892 garray_T *type_list;
893 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +0200894
895 if (tp == p + 1 && !quiet)
896 {
897 semsg(_(e_white_space_required_after_str_str), ":", p);
898 return NULL;
899 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100900
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000901 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
902 type_list = &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list;
903 else
904 {
905 type_list = &tmp_type_list;
906 ga_init2(type_list, sizeof(type_T), 10);
907 }
908
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200909 // parse the type after the name
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000910 lp->ll_type = parse_type(&tp, type_list, !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100911 if (lp->ll_type == NULL && !quiet)
912 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200913 lp->ll_name_end = tp;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +0000914
915 // drop the type when not in a script
916 if (type_list == &tmp_type_list)
917 {
918 lp->ll_type = NULL;
919 clear_type_list(type_list);
920 }
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200921 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100922 }
923 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000924 if (lp->ll_name == NULL)
925 return p;
926
927 if (*p == '.' && in_vim9script())
928 {
929 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, NULL);
930 if (import != NULL)
931 {
932 ufunc_T *ufunc;
933 type_T *type;
934
935 lp->ll_sid = import->imp_sid;
936 lp->ll_name = skipwhite(p + 1);
937 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
938 lp->ll_name_end = p;
939
940 // check the item is exported
941 cc = *p;
942 *p = NUL;
943 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
944 NULL, TRUE) == -1)
945 {
946 *p = cc;
947 return FAIL;
948 }
949 *p = cc;
950 }
951 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100952
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100953 // Without [idx] or .key we are done.
Bram Moolenaard5f400c2022-01-06 21:10:28 +0000954 if ((*p != '[' && *p != '.'))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000955 return p;
956
Bram Moolenaaraacc9662021-08-13 19:40:51 +0200957 if (in_vim9script() && lval_root != NULL)
958 {
959 // using local variable
960 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +0200961 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +0200962 }
963 else
964 {
965 cc = *p;
966 *p = NUL;
967 // When we would write to the variable pass &ht and prevent autoload.
968 writing = !(flags & GLV_READ_ONLY);
969 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100970 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +0200971 if (v == NULL && !quiet)
972 semsg(_(e_undefined_variable_str), lp->ll_name);
973 *p = cc;
974 if (v == NULL)
975 return NULL;
976 lp->ll_tv = &v->di_tv;
977 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000978
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +0100979 if (in_vim9script() && (flags & GLV_NO_DECL) == 0)
980 {
981 if (!quiet)
982 semsg(_(e_variable_already_declared), lp->ll_name);
983 return NULL;
984 }
985
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000986 /*
987 * Loop until no more [idx] or .key is following.
988 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100989 var1.v_type = VAR_UNKNOWN;
990 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +0200991 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000992 {
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +0200993 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
994 {
995 if (!quiet)
996 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
997 return NULL;
998 }
Bram Moolenaare65081d2021-06-27 15:04:05 +0200999 if (lp->ll_tv->v_type != VAR_LIST
1000 && lp->ll_tv->v_type != VAR_DICT
1001 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001002 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001003 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001004 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001005 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001006 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001007
1008 // a NULL list/blob works like an empty list/blob, allocate one now.
1009 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
1010 rettv_list_alloc(lp->ll_tv);
1011 else if (lp->ll_tv->v_type == VAR_BLOB
1012 && lp->ll_tv->vval.v_blob == NULL)
1013 rettv_blob_alloc(lp->ll_tv);
1014
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001015 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001016 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001017 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001018 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001019 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001020 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001021
Bram Moolenaar10c65862020-10-08 21:16:42 +02001022 if (in_vim9script() && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001023 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001024 && lp->ll_tv == &v->di_tv
1025 && ht != NULL && ht == get_script_local_ht())
1026 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001027 svar_T *sv = find_typval_in_script(lp->ll_tv, 0);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001028
1029 // Vim9 script local variable: get the type
1030 if (sv != NULL)
1031 lp->ll_valtype = sv->sv_type;
1032 }
1033
Bram Moolenaar8c711452005-01-14 21:53:12 +00001034 len = -1;
1035 if (*p == '.')
1036 {
1037 key = p + 1;
1038 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1039 ;
1040 if (len == 0)
1041 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001042 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001043 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001044 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001045 }
1046 p = key + len;
1047 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001048 else
1049 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001050 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001051 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001052 if (*p == ':')
1053 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001054 else
1055 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001056 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001057 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001058 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001059 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001060 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001061 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001062 clear_tv(&var1);
1063 return NULL;
1064 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001065 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001066 }
1067
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001068 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001069 if (*p == ':')
1070 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001071 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001072 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001073 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001074 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001075 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001076 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001077 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001078 if (rettv != NULL
1079 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001080 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001081 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001082 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001083 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001084 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001085 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001086 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001087 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001088 }
1089 p = skipwhite(p + 1);
1090 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001091 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001092 else
1093 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001094 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001095 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001096 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001097 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001098 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001099 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001100 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001101 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001102 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001103 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001104 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001105 clear_tv(&var2);
1106 return NULL;
1107 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001108 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001109 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001110 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001111 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001112 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001113
Bram Moolenaar8c711452005-01-14 21:53:12 +00001114 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001115 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001116 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001117 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001118 clear_tv(&var1);
1119 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001120 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001121 }
1122
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001123 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001124 ++p;
1125 }
1126
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001127 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001128 {
1129 if (len == -1)
1130 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001131 // "[key]": get key from "var1"
1132 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001133 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001134 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001135 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001136 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001137 }
1138 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001139 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001140
1141 // a NULL dict is equivalent with an empty dict
1142 if (lp->ll_tv->vval.v_dict == NULL)
1143 {
1144 lp->ll_tv->vval.v_dict = dict_alloc();
1145 if (lp->ll_tv->vval.v_dict == NULL)
1146 {
1147 clear_tv(&var1);
1148 return NULL;
1149 }
1150 ++lp->ll_tv->vval.v_dict->dv_refcount;
1151 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001152 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001153
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001154 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001155
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001156 // When assigning to a scope dictionary check that a function and
1157 // variable name is valid (only variable name unless it is l: or
1158 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001159 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001160 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001161 int prevval;
1162 int wrong;
1163
1164 if (len != -1)
1165 {
1166 prevval = key[len];
1167 key[len] = NUL;
1168 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001169 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001170 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001171 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1172 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001173 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001174 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001175 if (len != -1)
1176 key[len] = prevval;
1177 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001178 {
1179 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001180 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001181 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001182 }
1183
Bram Moolenaar10c65862020-10-08 21:16:42 +02001184 if (lp->ll_valtype != NULL)
1185 // use the type of the member
1186 lp->ll_valtype = lp->ll_valtype->tt_member;
1187
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001188 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001189 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001190 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001191 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001192 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001193 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001194 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001195 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001196 return NULL;
1197 }
1198
Bram Moolenaar31b81602019-02-10 22:14:27 +01001199 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001200 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001201 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001202 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001203 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001204 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001205 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001206 }
1207 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001208 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001209 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001210 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001211 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001212 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001213 p = NULL;
1214 break;
1215 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001216 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001217 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001218 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1219 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001220 {
1221 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001222 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001223 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001224
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001225 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001226 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001227 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001228 else if (lp->ll_tv->v_type == VAR_BLOB)
1229 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001230 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1231
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001232 /*
1233 * Get the number and item for the only or first index of the List.
1234 */
1235 if (empty1)
1236 lp->ll_n1 = 0;
1237 else
1238 // is number or string
1239 lp->ll_n1 = (long)tv_get_number(&var1);
1240 clear_tv(&var1);
1241
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001242 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001243 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001244 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001245 return NULL;
1246 }
1247 if (lp->ll_range && !lp->ll_empty2)
1248 {
1249 lp->ll_n2 = (long)tv_get_number(&var2);
1250 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001251 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1252 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001253 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001254 }
1255 lp->ll_blob = lp->ll_tv->vval.v_blob;
1256 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001257 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001258 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001259 else
1260 {
1261 /*
1262 * Get the number and item for the only or first index of the List.
1263 */
1264 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001265 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001266 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001267 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001268 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001269 clear_tv(&var1);
1270
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001271 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001272 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001273 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001274 if (lp->ll_li == NULL)
1275 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001276 clear_tv(&var2);
1277 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001278 }
1279
Bram Moolenaar10c65862020-10-08 21:16:42 +02001280 if (lp->ll_valtype != NULL)
1281 // use the type of the member
1282 lp->ll_valtype = lp->ll_valtype->tt_member;
1283
Bram Moolenaar8c711452005-01-14 21:53:12 +00001284 /*
1285 * May need to find the item or absolute index for the second
1286 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001287 * When no index given: "lp->ll_empty2" is TRUE.
1288 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001289 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001290 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001291 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001292 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001293 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001294 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001295 if (check_range_index_two(lp->ll_list,
1296 &lp->ll_n1, lp->ll_li,
1297 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001298 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001299 }
1300
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001301 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001302 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001303 }
1304
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001305 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001306 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001307 return p;
1308}
1309
1310/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001311 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001312 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001313 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001314clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001315{
1316 vim_free(lp->ll_exp_name);
1317 vim_free(lp->ll_newkey);
1318}
1319
1320/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001321 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001322 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001323 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1324 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001325 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001326 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001327set_var_lval(
1328 lval_T *lp,
1329 char_u *endp,
1330 typval_T *rettv,
1331 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001332 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1333 char_u *op,
1334 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001335{
1336 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001337 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001338
1339 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001340 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001341 cc = *endp;
1342 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001343 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1344 return;
1345
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001346 if (lp->ll_blob != NULL)
1347 {
1348 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001349
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001350 if (op != NULL && *op != '=')
1351 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001352 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001353 return;
1354 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001355 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001356 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001357
1358 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1359 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001360 if (lp->ll_empty2)
1361 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1362
Bram Moolenaar68452172021-04-12 21:21:02 +02001363 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1364 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001365 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001366 }
1367 else
1368 {
1369 val = (int)tv_get_number_chk(rettv, &error);
1370 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001371 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001372 }
1373 }
1374 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001375 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001376 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001377
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 Moolenaar460ae5d2022-01-01 14:19:49 +00001381 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001382 *endp = cc;
1383 return;
1384 }
1385
Bram Moolenaarff697e62019-02-12 22:28:33 +01001386 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001387 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001388 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001389 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001390 {
1391 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001392 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1393 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001394 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001395 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001396 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001397 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001398 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001399 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001400 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001401 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001402 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1403 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001404 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001405 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001406 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001407 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001408 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001409 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001410 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001411 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001412 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001413 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001414 else if (lp->ll_range)
1415 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001416 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1417 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001418 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001419 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001420 return;
1421 }
1422
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001423 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1424 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001425 }
1426 else
1427 {
1428 /*
1429 * Assign to a List or Dictionary item.
1430 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001431 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1432 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001433 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001434 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001435 return;
1436 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001437
1438 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001439 && check_typval_arg_type(lp->ll_valtype, rettv,
1440 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001441 return;
1442
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001443 if (lp->ll_newkey != NULL)
1444 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001445 if (op != NULL && *op != '=')
1446 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001447 semsg(_(e_key_not_present_in_dictionary), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001448 return;
1449 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001450 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1451 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001452 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001453
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001454 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001455 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001456 if (di == NULL)
1457 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001458 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1459 {
1460 vim_free(di);
1461 return;
1462 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001463 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001464 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001465 else if (op != NULL && *op != '=')
1466 {
1467 tv_op(lp->ll_tv, rettv, op);
1468 return;
1469 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001470 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001471 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001472
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001473 /*
1474 * Assign the value to the variable or list item.
1475 */
1476 if (copy)
1477 copy_tv(rettv, lp->ll_tv);
1478 else
1479 {
1480 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001481 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001482 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001483 }
1484 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001485}
1486
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001487/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001488 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1489 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001490 * Returns OK or FAIL.
1491 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001492 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001493tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001494{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001495 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001496 char_u numbuf[NUMBUFLEN];
1497 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001498 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001499
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001500 // Can't do anything with a Funcref or Dict on the right.
1501 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001502 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001503 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1504 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001505 {
1506 switch (tv1->v_type)
1507 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001508 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001509 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001510 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001511 case VAR_DICT:
1512 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001513 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001514 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001515 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001516 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001517 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001518 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001519 break;
1520
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001521 case VAR_BLOB:
1522 if (*op != '+' || tv2->v_type != VAR_BLOB)
1523 break;
1524 // BLOB += BLOB
1525 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1526 {
1527 blob_T *b1 = tv1->vval.v_blob;
1528 blob_T *b2 = tv2->vval.v_blob;
1529 int i, len = blob_len(b2);
1530 for (i = 0; i < len; i++)
1531 ga_append(&b1->bv_ga, blob_get(b2, i));
1532 }
1533 return OK;
1534
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001535 case VAR_LIST:
1536 if (*op != '+' || tv2->v_type != VAR_LIST)
1537 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001538 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001539 if (tv2->vval.v_list != NULL)
1540 {
1541 if (tv1->vval.v_list == NULL)
1542 {
1543 tv1->vval.v_list = tv2->vval.v_list;
1544 ++tv1->vval.v_list->lv_refcount;
1545 }
1546 else
1547 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1548 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001549 return OK;
1550
1551 case VAR_NUMBER:
1552 case VAR_STRING:
1553 if (tv2->v_type == VAR_LIST)
1554 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001555 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001556 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001557 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001558 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001559#ifdef FEAT_FLOAT
1560 if (tv2->v_type == VAR_FLOAT)
1561 {
1562 float_T f = n;
1563
Bram Moolenaarff697e62019-02-12 22:28:33 +01001564 if (*op == '%')
1565 break;
1566 switch (*op)
1567 {
1568 case '+': f += tv2->vval.v_float; break;
1569 case '-': f -= tv2->vval.v_float; break;
1570 case '*': f *= tv2->vval.v_float; break;
1571 case '/': f /= tv2->vval.v_float; break;
1572 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001573 clear_tv(tv1);
1574 tv1->v_type = VAR_FLOAT;
1575 tv1->vval.v_float = f;
1576 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001577 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001578#endif
1579 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001580 switch (*op)
1581 {
1582 case '+': n += tv_get_number(tv2); break;
1583 case '-': n -= tv_get_number(tv2); break;
1584 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001585 case '/': n = num_divide(n, tv_get_number(tv2),
1586 &failed); break;
1587 case '%': n = num_modulus(n, tv_get_number(tv2),
1588 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001589 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001590 clear_tv(tv1);
1591 tv1->v_type = VAR_NUMBER;
1592 tv1->vval.v_number = n;
1593 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001594 }
1595 else
1596 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001597 if (tv2->v_type == VAR_FLOAT)
1598 break;
1599
Bram Moolenaarff697e62019-02-12 22:28:33 +01001600 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001601 s = tv_get_string(tv1);
1602 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001603 clear_tv(tv1);
1604 tv1->v_type = VAR_STRING;
1605 tv1->vval.v_string = s;
1606 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001607 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001608
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001609 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001610#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001611 {
1612 float_T f;
1613
Bram Moolenaarff697e62019-02-12 22:28:33 +01001614 if (*op == '%' || *op == '.'
1615 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001616 && tv2->v_type != VAR_NUMBER
1617 && tv2->v_type != VAR_STRING))
1618 break;
1619 if (tv2->v_type == VAR_FLOAT)
1620 f = tv2->vval.v_float;
1621 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001622 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001623 switch (*op)
1624 {
1625 case '+': tv1->vval.v_float += f; break;
1626 case '-': tv1->vval.v_float -= f; break;
1627 case '*': tv1->vval.v_float *= f; break;
1628 case '/': tv1->vval.v_float /= f; break;
1629 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001630 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001631#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001632 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001633 }
1634 }
1635
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001636 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001637 return FAIL;
1638}
1639
1640/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001641 * Evaluate the expression used in a ":for var in expr" command.
1642 * "arg" points to "var".
1643 * Set "*errp" to TRUE for an error, FALSE otherwise;
1644 * Return a pointer that holds the info. Null when there is an error.
1645 */
1646 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001647eval_for_line(
1648 char_u *arg,
1649 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001650 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001651 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001652{
Bram Moolenaar33570922005-01-25 22:26:29 +00001653 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001654 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001655 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001656 typval_T tv;
1657 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001658 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001659
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001660 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001661
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001662 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001663 if (fi == NULL)
1664 return NULL;
1665
Bram Moolenaar404557e2021-07-05 21:41:48 +02001666 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1667 &fi->fi_semicolon, FALSE);
1668 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001669 return fi;
1670
Bram Moolenaar404557e2021-07-05 21:41:48 +02001671 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001672 if (expr[0] != 'i' || expr[1] != 'n'
1673 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001674 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001675 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1676 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1677 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001678 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001679 return fi;
1680 }
1681
1682 if (skip)
1683 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001684 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1685 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001686 {
1687 *errp = FALSE;
1688 if (!skip)
1689 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001690 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001691 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001692 l = tv.vval.v_list;
1693 if (l == NULL)
1694 {
1695 // a null list is like an empty list: do nothing
1696 clear_tv(&tv);
1697 }
1698 else
1699 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001700 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001701 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001702
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001703 // No need to increment the refcount, it's already set for
1704 // the list being used in "tv".
1705 fi->fi_list = l;
1706 list_add_watch(l, &fi->fi_lw);
1707 fi->fi_lw.lw_item = l->lv_first;
1708 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001709 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001710 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001711 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001712 fi->fi_bi = 0;
1713 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001714 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001715 typval_T btv;
1716
1717 // Make a copy, so that the iteration still works when the
1718 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001719 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001720 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001721 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001722 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001723 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001724 else if (tv.v_type == VAR_STRING)
1725 {
1726 fi->fi_byte_idx = 0;
1727 fi->fi_string = tv.vval.v_string;
1728 tv.vval.v_string = NULL;
1729 if (fi->fi_string == NULL)
1730 fi->fi_string = vim_strsave((char_u *)"");
1731 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001732 else
1733 {
Sean Dewar80d73952021-08-04 19:25:54 +02001734 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001735 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001736 }
1737 }
1738 }
1739 if (skip)
1740 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001741 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001742
1743 return fi;
1744}
1745
1746/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001747 * Used when looping over a :for line, skip the "in expr" part.
1748 */
1749 void
1750skip_for_lines(void *fi_void, evalarg_T *evalarg)
1751{
1752 forinfo_T *fi = (forinfo_T *)fi_void;
1753 int i;
1754
1755 for (i = 0; i < fi->fi_break_count; ++i)
1756 eval_next_line(evalarg);
1757}
1758
1759/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001760 * Use the first item in a ":for" list. Advance to the next.
1761 * Assign the values to the variable (list). "arg" points to the first one.
1762 * Return TRUE when a valid item was found, FALSE when at end of list or
1763 * something wrong.
1764 */
1765 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001766next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001767{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001768 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001769 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001770 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001771 ? (ASSIGN_FINAL
1772 // first round: error if variable exists
1773 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
1774 | ASSIGN_NO_MEMBER_TYPE)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001775 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001776 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001777 int skip_assign = in_vim9script() && arg[0] == '_'
1778 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001779
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001780 if (fi->fi_blob != NULL)
1781 {
1782 typval_T tv;
1783
1784 if (fi->fi_bi >= blob_len(fi->fi_blob))
1785 return FALSE;
1786 tv.v_type = VAR_NUMBER;
1787 tv.v_lock = VAR_FIXED;
1788 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1789 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001790 if (skip_assign)
1791 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001792 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001793 fi->fi_varcount, flag, NULL) == OK;
1794 }
1795
1796 if (fi->fi_string != NULL)
1797 {
1798 typval_T tv;
1799 int len;
1800
1801 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1802 if (len == 0)
1803 return FALSE;
1804 tv.v_type = VAR_STRING;
1805 tv.v_lock = VAR_FIXED;
1806 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1807 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001808 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001809 if (skip_assign)
1810 result = TRUE;
1811 else
1812 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001813 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001814 vim_free(tv.vval.v_string);
1815 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001816 }
1817
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001818 item = fi->fi_lw.lw_item;
1819 if (item == NULL)
1820 result = FALSE;
1821 else
1822 {
1823 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001824 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001825 if (skip_assign)
1826 result = TRUE;
1827 else
1828 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001829 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001830 }
1831 return result;
1832}
1833
1834/*
1835 * Free the structure used to store info used by ":for".
1836 */
1837 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001838free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001839{
Bram Moolenaar33570922005-01-25 22:26:29 +00001840 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001841
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001842 if (fi == NULL)
1843 return;
1844 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001845 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001846 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001847 list_unref(fi->fi_list);
1848 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001849 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001850 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001851 else
1852 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001853 vim_free(fi);
1854}
1855
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001857set_context_for_expression(
1858 expand_T *xp,
1859 char_u *arg,
1860 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001862 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001864 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001866 if (cmdidx == CMD_let || cmdidx == CMD_var
1867 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001868 {
1869 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001870 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001871 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001872 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001873 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001874 {
1875 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001876 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001877 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001878 break;
1879 }
1880 return;
1881 }
1882 }
1883 else
1884 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1885 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 while ((xp->xp_pattern = vim_strpbrk(arg,
1887 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1888 {
1889 c = *xp->xp_pattern;
1890 if (c == '&')
1891 {
1892 c = xp->xp_pattern[1];
1893 if (c == '&')
1894 {
1895 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001896 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 }
1898 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001899 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001901 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1902 xp->xp_pattern += 2;
1903
1904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 }
1906 else if (c == '$')
1907 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001908 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 xp->xp_context = EXPAND_ENV_VARS;
1910 }
1911 else if (c == '=')
1912 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001913 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 xp->xp_context = EXPAND_EXPRESSION;
1915 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001916 else if (c == '#'
1917 && xp->xp_context == EXPAND_EXPRESSION)
1918 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001919 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001920 break;
1921 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001922 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 && xp->xp_context == EXPAND_FUNCTIONS
1924 && vim_strchr(xp->xp_pattern, '(') == NULL)
1925 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001926 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 break;
1928 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001929 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001931 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 {
1933 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1934 if (c == '\\' && xp->xp_pattern[1] != NUL)
1935 ++xp->xp_pattern;
1936 xp->xp_context = EXPAND_NOTHING;
1937 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001938 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001940 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1942 /* skip */ ;
1943 xp->xp_context = EXPAND_NOTHING;
1944 }
1945 else if (c == '|')
1946 {
1947 if (xp->xp_pattern[1] == '|')
1948 {
1949 ++xp->xp_pattern;
1950 xp->xp_context = EXPAND_EXPRESSION;
1951 }
1952 else
1953 xp->xp_context = EXPAND_COMMANDS;
1954 }
1955 else
1956 xp->xp_context = EXPAND_EXPRESSION;
1957 }
1958 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001959 // Doesn't look like something valid, expand as an expression
1960 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001961 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 arg = xp->xp_pattern;
1963 if (*arg != NUL)
1964 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1965 /* skip */ ;
1966 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01001967
1968 // ":exe one two" completes "two"
1969 if ((cmdidx == CMD_execute
1970 || cmdidx == CMD_echo
1971 || cmdidx == CMD_echon
1972 || cmdidx == CMD_echomsg)
1973 && xp->xp_context == EXPAND_EXPRESSION)
1974 {
1975 for (;;)
1976 {
1977 char_u *n = skiptowhite(arg);
1978
1979 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
1980 break;
1981 arg = skipwhite(n);
1982 }
1983 }
1984
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 xp->xp_pattern = arg;
1986}
1987
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001989 * Return TRUE if "pat" matches "text".
1990 * Does not use 'cpo' and always uses 'magic'.
1991 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001992 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001993pattern_match(char_u *pat, char_u *text, int ic)
1994{
1995 int matches = FALSE;
1996 char_u *save_cpo;
1997 regmatch_T regmatch;
1998
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001999 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002000 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002001 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002002 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2003 if (regmatch.regprog != NULL)
2004 {
2005 regmatch.rm_ic = ic;
2006 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2007 vim_regfree(regmatch.regprog);
2008 }
2009 p_cpo = save_cpo;
2010 return matches;
2011}
2012
2013/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002014 * Handle a name followed by "(". Both for just "name(arg)" and for
2015 * "expr->name(arg)".
2016 * Returns OK or FAIL.
2017 */
2018 static int
2019eval_func(
2020 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002021 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002022 char_u *name,
2023 int name_len,
2024 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002025 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002026 typval_T *basetv) // "expr" for "expr->name(arg)"
2027{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002028 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002029 char_u *s = name;
2030 int len = name_len;
2031 partial_T *partial;
2032 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002033 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002034 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002035
2036 if (!evaluate)
2037 check_vars(s, len);
2038
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002039 // If "s" is the name of a variable of type VAR_FUNC
2040 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002041 s = deref_func_name(s, &len, &partial,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002042 in_vim9script() ? &type : NULL, !evaluate, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002043
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002044 // Need to make a copy, in case evaluating the arguments makes
2045 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002046 s = vim_strsave(s);
Bram Moolenaarb3341372021-12-14 18:57:45 +00002047 if (s == NULL || (evaluate && (*s == NUL || (flags & EVAL_CONSTANT))))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002048 ret = FAIL;
2049 else
2050 {
2051 funcexe_T funcexe;
2052
2053 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002054 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002055 funcexe.fe_firstline = curwin->w_cursor.lnum;
2056 funcexe.fe_lastline = curwin->w_cursor.lnum;
2057 funcexe.fe_evaluate = evaluate;
2058 funcexe.fe_partial = partial;
2059 funcexe.fe_basetv = basetv;
2060 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002061 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002062 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002063 }
2064 vim_free(s);
2065
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002066 // If evaluate is FALSE rettv->v_type was not set in
2067 // get_func_tv, but it's needed in handle_subscript() to parse
2068 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002069 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2070 {
2071 rettv->vval.v_string = NULL;
2072 rettv->v_type = VAR_FUNC;
2073 }
2074
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002075 // Stop the expression evaluation when immediately
2076 // aborting on error, or when an interrupt occurred or
2077 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002078 if (evaluate && aborting())
2079 {
2080 if (ret == OK)
2081 clear_tv(rettv);
2082 ret = FAIL;
2083 }
2084 return ret;
2085}
2086
2087/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002088 * Get the next line source line without advancing. But do skip over comment
2089 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002090 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002091 */
2092 static char_u *
2093getline_peek_skip_comments(evalarg_T *evalarg)
2094{
2095 for (;;)
2096 {
2097 char_u *next = getline_peek(evalarg->eval_getline,
2098 evalarg->eval_cookie);
2099 char_u *p;
2100
2101 if (next == NULL)
2102 break;
2103 p = skipwhite(next);
2104 if (*p != NUL && !vim9_comment_start(p))
2105 return next;
2106 (void)eval_next_line(evalarg);
2107 }
2108 return NULL;
2109}
2110
2111/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002112 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2113 * comment) and there is a next line, return the next line (skipping blanks)
2114 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002115 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2116 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002117 * "arg" must point somewhere inside a line, not at the start.
2118 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002119 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002120eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2121{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002122 char_u *p = skipwhite(arg);
2123
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002124 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002125 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002126 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002127 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002128 && (*p == NUL || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002129 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002130 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002131
2132 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002133 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002134 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002135 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002136
Bram Moolenaar9d489562020-07-30 20:08:50 +02002137 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002138 {
2139 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002140 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002141 }
2142 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002143 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002144}
2145
2146/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002147 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002148 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002149 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002150 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002151eval_next_line(evalarg_T *evalarg)
2152{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002153 garray_T *gap = &evalarg->eval_ga;
2154 char_u *line;
2155
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002156 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002157 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2158 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002159 else
2160 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002161 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002162 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2163 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002164 char_u *p = skipwhite(line);
2165
2166 // Going to concatenate the lines after parsing. For an empty or
2167 // comment line use an empty string.
2168 if (*p == NUL || vim9_comment_start(p))
2169 {
2170 vim_free(line);
2171 line = vim_strsave((char_u *)"");
2172 }
2173
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002174 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2175 ++gap->ga_len;
2176 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002177 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002178 {
2179 vim_free(evalarg->eval_tofree);
2180 evalarg->eval_tofree = line;
2181 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002182
2183 // Advanced to the next line, "arg" no longer points into the previous
2184 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002185 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002186 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002187}
2188
Bram Moolenaare6b53242020-07-01 17:28:33 +02002189/*
2190 * Call eval_next_non_blank() and get the next line if needed.
2191 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002192 char_u *
2193skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2194{
2195 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002196 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002197
Bram Moolenaar962d7212020-07-04 14:15:00 +02002198 if (evalarg == NULL)
2199 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002200 eval_next_non_blank(p, evalarg, &getnext);
2201 if (getnext)
2202 return eval_next_line(evalarg);
2203 return p;
2204}
2205
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002206/*
Bram Moolenaar844fb642021-10-23 13:32:30 +01002207 * Initialize "evalarg" for use.
2208 */
2209 void
2210init_evalarg(evalarg_T *evalarg)
2211{
2212 CLEAR_POINTER(evalarg);
2213 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
2214}
2215
2216/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002217 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002218 */
2219 void
2220clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2221{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002222 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002223 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002224 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002225 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002226 if (eap != NULL)
2227 {
2228 // We may need to keep the original command line, e.g. for
2229 // ":let" it has the variable names. But we may also need the
2230 // new one, "nextcmd" points into it. Keep both.
2231 vim_free(eap->cmdline_tofree);
2232 eap->cmdline_tofree = *eap->cmdlinep;
2233 *eap->cmdlinep = evalarg->eval_tofree;
2234 }
2235 else
2236 vim_free(evalarg->eval_tofree);
2237 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002238 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002239
Bram Moolenaar844fb642021-10-23 13:32:30 +01002240 ga_clear_strings(&evalarg->eval_tofree_ga);
Bram Moolenaar67da21a2021-03-21 22:12:34 +01002241 VIM_CLEAR(evalarg->eval_tofree_lambda);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002242 }
2243}
2244
2245/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002247 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2249 */
2250
2251/*
2252 * Handle zero level expression.
2253 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002255 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002256 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 * Return OK or FAIL.
2258 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002259 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002260eval0(
2261 char_u *arg,
2262 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002263 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002264 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002266 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2267}
2268
2269/*
2270 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2271 * expression and don't check what comes after the expression.
2272 */
2273 int
2274eval0_retarg(
2275 char_u *arg,
2276 typval_T *rettv,
2277 exarg_T *eap,
2278 evalarg_T *evalarg,
2279 char_u **retarg)
2280{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 int ret;
2282 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002283 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002284 int did_emsg_before = did_emsg;
2285 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002286 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002287 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002288 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289
2290 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002291 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002292 expr_end = p;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002293 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002294
Bram Moolenaar02929a32021-12-17 14:46:12 +00002295 // In Vim9 script a command block is not split at NL characters for
2296 // commands using an expression argument. Skip over a '#' comment to check
2297 // for a following NL. Require white space before the '#'.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002298 if (in_vim9script() && p > expr_end && retarg == NULL)
Bram Moolenaar02929a32021-12-17 14:46:12 +00002299 while (*p == '#')
2300 {
2301 char_u *nl = vim_strchr(p, NL);
2302
2303 if (nl == NULL)
2304 break;
2305 p = skipwhite(nl + 1);
2306 if (eap != NULL && *p != NUL)
2307 eap->nextcmd = p;
2308 check_for_end = FALSE;
2309 }
2310
2311 if (ret != FAIL && check_for_end)
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002312 end_error = !ends_excmd2(arg, p);
2313 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 {
2315 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002316 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 /*
2318 * Report the invalid expression unless the expression evaluation has
2319 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002320 * exception, or we already gave a more specific error.
2321 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002323 if (!aborting()
2324 && did_emsg == did_emsg_before
2325 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002326 && (flags & EVAL_CONSTANT) == 0
2327 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002328 {
2329 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002330 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002331 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002332 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002333 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002334
2335 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002336 // a next command to avoid more errors, unless "|" is following, which
2337 // could only be a command separator.
2338 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2339 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002340 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002342
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002343 if (retarg != NULL)
2344 *retarg = p;
2345 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002346 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002347
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 return ret;
2349}
2350
2351/*
2352 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002353 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002354 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 *
2356 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002357 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002359 * Note: "rettv.v_lock" is not set.
2360 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 * Return OK or FAIL.
2362 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002363 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002364eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002366 char_u *p;
2367 int getnext;
2368
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002369 CLEAR_POINTER(rettv);
2370
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 /*
2372 * Get the first variable.
2373 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002374 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 return FAIL;
2376
Bram Moolenaar793648f2020-06-26 21:28:25 +02002377 p = eval_next_non_blank(*arg, evalarg, &getnext);
2378 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002380 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002381 int result;
2382 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002383 evalarg_T *evalarg_used = evalarg;
2384 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002385 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002386 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002387 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002388
2389 if (evalarg == NULL)
2390 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002391 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002392 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002393 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002394 orig_flags = evalarg_used->eval_flags;
2395 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002396
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002397 if (getnext)
2398 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002399 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002400 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002401 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002402 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002403 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002404 clear_tv(rettv);
2405 return FAIL;
2406 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002407 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002408 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002409
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002411 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002413 int error = FALSE;
2414
Bram Moolenaar13106602020-10-04 16:06:05 +02002415 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002416 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002417 else if (vim9script)
2418 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002419 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002421 if (error || !op_falsy || !result)
2422 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002423 if (error)
2424 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 }
2426
2427 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002428 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002430 if (op_falsy)
2431 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002432 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002433 {
mityu4ac198c2021-05-28 17:52:40 +02002434 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002435 clear_tv(rettv);
2436 return FAIL;
2437 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002438 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002439 evalarg_used->eval_flags = (op_falsy ? !result : result)
2440 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2441 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002442 {
2443 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002445 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002446 if (!op_falsy || !result)
2447 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002449 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002451 /*
2452 * Check for the ":".
2453 */
2454 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2455 if (*p != ':')
2456 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002457 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002458 if (evaluate && result)
2459 clear_tv(rettv);
2460 evalarg_used->eval_flags = orig_flags;
2461 return FAIL;
2462 }
2463 if (getnext)
2464 *arg = eval_next_line(evalarg_used);
2465 else
2466 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002467 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002468 {
2469 error_white_both(p, 1);
2470 clear_tv(rettv);
2471 evalarg_used->eval_flags = orig_flags;
2472 return FAIL;
2473 }
2474 *arg = p;
2475 }
2476
2477 /*
2478 * Get the third variable. Recursive!
2479 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002480 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002481 {
mityu4ac198c2021-05-28 17:52:40 +02002482 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002483 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002484 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002485 return FAIL;
2486 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002487 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2488 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002489 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002490 if (eval1(arg, &var2, evalarg_used) == FAIL)
2491 {
2492 if (evaluate && result)
2493 clear_tv(rettv);
2494 evalarg_used->eval_flags = orig_flags;
2495 return FAIL;
2496 }
2497 if (evaluate && !result)
2498 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002500
2501 if (evalarg == NULL)
2502 clear_evalarg(&local_evalarg, NULL);
2503 else
2504 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 }
2506
2507 return OK;
2508}
2509
2510/*
2511 * Handle first level expression:
2512 * expr2 || expr2 || expr2 logical OR
2513 *
2514 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002515 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 *
2517 * Return OK or FAIL.
2518 */
2519 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002520eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002522 char_u *p;
2523 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524
2525 /*
2526 * Get the first variable.
2527 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002528 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 return FAIL;
2530
2531 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002532 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002534 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002535 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002537 evalarg_T *evalarg_used = evalarg;
2538 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002539 int evaluate;
2540 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002541 long result = FALSE;
2542 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002543 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002544 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002545
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002546 if (evalarg == NULL)
2547 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002548 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002549 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002550 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002551 orig_flags = evalarg_used->eval_flags;
2552 evaluate = orig_flags & EVAL_EVALUATE;
2553 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002554 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002555 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002556 result = tv_get_bool_chk(rettv, &error);
2557 else if (tv_get_number_chk(rettv, &error) != 0)
2558 result = TRUE;
2559 clear_tv(rettv);
2560 if (error)
2561 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 }
2563
2564 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002565 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002567 while (p[0] == '|' && p[1] == '|')
2568 {
2569 if (getnext)
2570 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002571 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002572 {
2573 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2574 {
2575 error_white_both(p, 2);
2576 clear_tv(rettv);
2577 return FAIL;
2578 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002579 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002580 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002581
2582 /*
2583 * Get the second variable.
2584 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002585 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2586 {
mityu4ac198c2021-05-28 17:52:40 +02002587 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002588 clear_tv(rettv);
2589 return FAIL;
2590 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002591 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2592 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002593 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002594 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002595 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002596
2597 /*
2598 * Compute the result.
2599 */
2600 if (evaluate && !result)
2601 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002602 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002603 result = tv_get_bool_chk(&var2, &error);
2604 else if (tv_get_number_chk(&var2, &error) != 0)
2605 result = TRUE;
2606 clear_tv(&var2);
2607 if (error)
2608 return FAIL;
2609 }
2610 if (evaluate)
2611 {
2612 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002613 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002614 rettv->v_type = VAR_BOOL;
2615 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002616 }
2617 else
2618 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002619 rettv->v_type = VAR_NUMBER;
2620 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002621 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002622 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002623
2624 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002626
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002627 if (evalarg == NULL)
2628 clear_evalarg(&local_evalarg, NULL);
2629 else
2630 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 }
2632
2633 return OK;
2634}
2635
2636/*
2637 * Handle second level expression:
2638 * expr3 && expr3 && expr3 logical AND
2639 *
2640 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002641 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 *
2643 * Return OK or FAIL.
2644 */
2645 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002646eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002648 char_u *p;
2649 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650
2651 /*
2652 * Get the first variable.
2653 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002654 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 return FAIL;
2656
2657 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002658 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002660 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002661 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002663 evalarg_T *evalarg_used = evalarg;
2664 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002665 int orig_flags;
2666 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002667 long result = TRUE;
2668 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002669 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002670 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002671
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002672 if (evalarg == NULL)
2673 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002674 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002675 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002676 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002677 orig_flags = evalarg_used->eval_flags;
2678 evaluate = orig_flags & EVAL_EVALUATE;
2679 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002680 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002681 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002682 result = tv_get_bool_chk(rettv, &error);
2683 else if (tv_get_number_chk(rettv, &error) == 0)
2684 result = FALSE;
2685 clear_tv(rettv);
2686 if (error)
2687 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 }
2689
2690 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002691 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002693 while (p[0] == '&' && p[1] == '&')
2694 {
2695 if (getnext)
2696 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002697 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002698 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002699 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002700 {
2701 error_white_both(p, 2);
2702 clear_tv(rettv);
2703 return FAIL;
2704 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002705 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002706 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002707
2708 /*
2709 * Get the second variable.
2710 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002711 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2712 {
mityu4ac198c2021-05-28 17:52:40 +02002713 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002714 clear_tv(rettv);
2715 return FAIL;
2716 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002717 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2718 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002719 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002720 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002721 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002722 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002723
2724 /*
2725 * Compute the result.
2726 */
2727 if (evaluate && result)
2728 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002729 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002730 result = tv_get_bool_chk(&var2, &error);
2731 else if (tv_get_number_chk(&var2, &error) == 0)
2732 result = FALSE;
2733 clear_tv(&var2);
2734 if (error)
2735 return FAIL;
2736 }
2737 if (evaluate)
2738 {
2739 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002740 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002741 rettv->v_type = VAR_BOOL;
2742 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002743 }
2744 else
2745 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002746 rettv->v_type = VAR_NUMBER;
2747 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002748 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002749 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002750
2751 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002753
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002754 if (evalarg == NULL)
2755 clear_evalarg(&local_evalarg, NULL);
2756 else
2757 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 }
2759
2760 return OK;
2761}
2762
2763/*
2764 * Handle third level expression:
2765 * var1 == var2
2766 * var1 =~ var2
2767 * var1 != var2
2768 * var1 !~ var2
2769 * var1 > var2
2770 * var1 >= var2
2771 * var1 < var2
2772 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002773 * var1 is var2
2774 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 *
2776 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002777 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 *
2779 * Return OK or FAIL.
2780 */
2781 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002782eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002785 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002786 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002788 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789
2790 /*
2791 * Get the first variable.
2792 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002793 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 return FAIL;
2795
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002796 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002797 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798
2799 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002800 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002802 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002804 typval_T var2;
2805 int ic;
2806 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002807 int evaluate = evalarg == NULL
2808 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002809
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002810 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002811 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002812 *arg = eval_next_line(evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002813 p = *arg;
2814 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002815 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2816 {
mityu4ac198c2021-05-28 17:52:40 +02002817 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002818 clear_tv(rettv);
2819 return FAIL;
2820 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002821
Bram Moolenaar696ba232020-07-29 21:20:41 +02002822 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2823 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002824 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002825 clear_tv(rettv);
2826 return FAIL;
2827 }
2828
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002829 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 if (p[len] == '?')
2831 {
2832 ic = TRUE;
2833 ++len;
2834 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002835 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 else if (p[len] == '#')
2837 {
2838 ic = FALSE;
2839 ++len;
2840 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002841 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002843 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844
2845 /*
2846 * Get the second variable.
2847 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002848 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2849 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002850 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002851 clear_tv(rettv);
2852 return FAIL;
2853 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002854 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002855 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002857 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 return FAIL;
2859 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002860 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002861 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002862 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002863
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002864 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002865 {
2866 ret = FAIL;
2867 clear_tv(rettv);
2868 }
2869 else
2870 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002871 clear_tv(&var2);
2872 return ret;
2873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 }
2875
2876 return OK;
2877}
2878
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002879/*
2880 * Make a copy of blob "tv1" and append blob "tv2".
2881 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002882 void
2883eval_addblob(typval_T *tv1, typval_T *tv2)
2884{
2885 blob_T *b1 = tv1->vval.v_blob;
2886 blob_T *b2 = tv2->vval.v_blob;
2887 blob_T *b = blob_alloc();
2888 int i;
2889
2890 if (b != NULL)
2891 {
2892 for (i = 0; i < blob_len(b1); i++)
2893 ga_append(&b->bv_ga, blob_get(b1, i));
2894 for (i = 0; i < blob_len(b2); i++)
2895 ga_append(&b->bv_ga, blob_get(b2, i));
2896
2897 clear_tv(tv1);
2898 rettv_blob_set(tv1, b);
2899 }
2900}
2901
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002902/*
2903 * Make a copy of list "tv1" and append list "tv2".
2904 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002905 int
2906eval_addlist(typval_T *tv1, typval_T *tv2)
2907{
2908 typval_T var3;
2909
2910 // concatenate Lists
2911 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2912 {
2913 clear_tv(tv1);
2914 clear_tv(tv2);
2915 return FAIL;
2916 }
2917 clear_tv(tv1);
2918 *tv1 = var3;
2919 return OK;
2920}
2921
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922/*
2923 * Handle fourth level expression:
2924 * + number addition
2925 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002926 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002927 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 *
2929 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002930 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 *
2932 * Return OK or FAIL.
2933 */
2934 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002935eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 /*
2938 * Get the first variable.
2939 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002940 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 return FAIL;
2942
2943 /*
2944 * Repeat computing, until no '+', '-' or '.' is following.
2945 */
2946 for (;;)
2947 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002948 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002949 int getnext;
2950 char_u *p;
2951 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002952 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002953 int concat;
2954 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002955 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002956
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002957 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002958 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002959 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002960 p = eval_next_non_blank(*arg, evalarg, &getnext);
2961 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02002962 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01002963 if ((op != '+' && op != '-' && !concat) || p[1] == '='
2964 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02002966 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
2967 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002968
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002969 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002970 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002971 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002972 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002973 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002974 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002975 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002976 {
mityu4ac198c2021-05-28 17:52:40 +02002977 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002978 clear_tv(rettv);
2979 return FAIL;
2980 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002981 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002982 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002983 if ((op != '+' || (rettv->v_type != VAR_LIST
2984 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002985#ifdef FEAT_FLOAT
2986 && (op == '.' || rettv->v_type != VAR_FLOAT)
2987#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002988 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002989 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002990 int error = FALSE;
2991
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002992 // For "list + ...", an illegal use of the first operand as
2993 // a number cannot be determined before evaluating the 2nd
2994 // operand: if this is also a list, all is ok.
2995 // For "something . ...", "something - ..." or "non-list + ...",
2996 // we know that the first operand needs to be a string or number
2997 // without evaluating the 2nd operand. So check before to avoid
2998 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002999 if (op != '.')
3000 tv_get_number_chk(rettv, &error);
3001 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003002 {
3003 clear_tv(rettv);
3004 return FAIL;
3005 }
3006 }
3007
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 /*
3009 * Get the second variable.
3010 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003011 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003012 {
mityu89dcb4d2021-05-28 13:50:17 +02003013 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003014 clear_tv(rettv);
3015 return FAIL;
3016 }
3017 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02003018 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003020 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 return FAIL;
3022 }
3023
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003024 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 {
3026 /*
3027 * Compute the result.
3028 */
3029 if (op == '.')
3030 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003031 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3032 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003033 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003034
Bram Moolenaar2e086612020-08-25 22:37:48 +02003035 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003036 || var2.v_type == VAR_CHANNEL
3037 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003038 semsg(_(e_using_invalid_value_as_string_str),
3039 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003040#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02003041 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003042 {
3043 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3044 var2.vval.v_float);
3045 s2 = buf2;
3046 }
3047#endif
3048 else
3049 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003050 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003051 {
3052 clear_tv(rettv);
3053 clear_tv(&var2);
3054 return FAIL;
3055 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003056 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003057 clear_tv(rettv);
3058 rettv->v_type = VAR_STRING;
3059 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003061 else if (op == '+' && rettv->v_type == VAR_BLOB
3062 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003063 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003064 else if (op == '+' && rettv->v_type == VAR_LIST
3065 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003066 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003067 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003068 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 else
3071 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003072 int error = FALSE;
3073 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003074#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003075 float_T f1 = 0, f2 = 0;
3076
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003077 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003078 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003079 f1 = rettv->vval.v_float;
3080 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003081 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003082 else
3083#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003084 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003085 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003086 if (error)
3087 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003088 // This can only happen for "list + non-list" or
3089 // "blob + non-blob". For "non-list + ..." or
3090 // "something - ...", we returned before evaluating the
3091 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003092 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003093 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003094 return FAIL;
3095 }
3096#ifdef FEAT_FLOAT
3097 if (var2.v_type == VAR_FLOAT)
3098 f1 = n1;
3099#endif
3100 }
3101#ifdef FEAT_FLOAT
3102 if (var2.v_type == VAR_FLOAT)
3103 {
3104 f2 = var2.vval.v_float;
3105 n2 = 0;
3106 }
3107 else
3108#endif
3109 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003110 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003111 if (error)
3112 {
3113 clear_tv(rettv);
3114 clear_tv(&var2);
3115 return FAIL;
3116 }
3117#ifdef FEAT_FLOAT
3118 if (rettv->v_type == VAR_FLOAT)
3119 f2 = n2;
3120#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003121 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003122 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003123
3124#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003125 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003126 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3127 {
3128 if (op == '+')
3129 f1 = f1 + f2;
3130 else
3131 f1 = f1 - f2;
3132 rettv->v_type = VAR_FLOAT;
3133 rettv->vval.v_float = f1;
3134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003136#endif
3137 {
3138 if (op == '+')
3139 n1 = n1 + n2;
3140 else
3141 n1 = n1 - n2;
3142 rettv->v_type = VAR_NUMBER;
3143 rettv->vval.v_number = n1;
3144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003146 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 }
3148 }
3149 return OK;
3150}
3151
3152/*
3153 * Handle fifth level expression:
3154 * * number multiplication
3155 * / number division
3156 * % number modulo
3157 *
3158 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003159 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 *
3161 * Return OK or FAIL.
3162 */
3163 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003164eval6(
3165 char_u **arg,
3166 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003167 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003168 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003170#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003171 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003172#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173
3174 /*
3175 * Get the first variable.
3176 */
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003177 if (eval7t(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 return FAIL;
3179
3180 /*
3181 * Repeat computing, until no '*', '/' or '%' is following.
3182 */
3183 for (;;)
3184 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003185 int evaluate;
3186 int getnext;
3187 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003188 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003189 int op;
3190 varnumber_T n1, n2;
3191#ifdef FEAT_FLOAT
3192 float_T f1, f2;
3193#endif
3194 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003195
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003196 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003197 p = eval_next_non_blank(*arg, evalarg, &getnext);
3198 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003199 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003201
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003202 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003203 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003204 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003205 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003206 {
3207 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3208 {
mityu4ac198c2021-05-28 17:52:40 +02003209 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003210 clear_tv(rettv);
3211 return FAIL;
3212 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003213 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003216#ifdef FEAT_FLOAT
3217 f1 = 0;
3218 f2 = 0;
3219#endif
3220 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003221 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003223#ifdef FEAT_FLOAT
3224 if (rettv->v_type == VAR_FLOAT)
3225 {
3226 f1 = rettv->vval.v_float;
3227 use_float = TRUE;
3228 n1 = 0;
3229 }
3230 else
3231#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003232 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003233 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003234 if (error)
3235 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 }
3237 else
3238 n1 = 0;
3239
3240 /*
3241 * Get the second variable.
3242 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003243 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3244 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003245 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003246 clear_tv(rettv);
3247 return FAIL;
3248 }
3249 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003250 if (eval7t(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 return FAIL;
3252
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003253 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003255#ifdef FEAT_FLOAT
3256 if (var2.v_type == VAR_FLOAT)
3257 {
3258 if (!use_float)
3259 {
3260 f1 = n1;
3261 use_float = TRUE;
3262 }
3263 f2 = var2.vval.v_float;
3264 n2 = 0;
3265 }
3266 else
3267#endif
3268 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003269 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003270 clear_tv(&var2);
3271 if (error)
3272 return FAIL;
3273#ifdef FEAT_FLOAT
3274 if (use_float)
3275 f2 = n2;
3276#endif
3277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278
3279 /*
3280 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003281 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003283#ifdef FEAT_FLOAT
3284 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003286 if (op == '*')
3287 f1 = f1 * f2;
3288 else if (op == '/')
3289 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003290# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003291 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003292 if (f2 == 0.0)
3293 {
3294 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003295 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003296 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003297 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003298 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003299 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003300 }
3301 else
3302 f1 = f1 / f2;
3303# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003304 // We rely on the floating point library to handle divide
3305 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003306 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003307# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003310 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003311 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003312 return FAIL;
3313 }
3314 rettv->v_type = VAR_FLOAT;
3315 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 }
3317 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003318#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003320 int failed = FALSE;
3321
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003322 if (op == '*')
3323 n1 = n1 * n2;
3324 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003325 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003327 n1 = num_modulus(n1, n2, &failed);
3328 if (failed)
3329 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003330
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003331 rettv->v_type = VAR_NUMBER;
3332 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 }
3335 }
3336
3337 return OK;
3338}
3339
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003340/*
3341 * Handle a type cast before a base level expression.
3342 * "arg" must point to the first non-white of the expression.
3343 * "arg" is advanced to just after the recognized expression.
3344 * Return OK or FAIL.
3345 */
3346 static int
3347eval7t(
3348 char_u **arg,
3349 typval_T *rettv,
3350 evalarg_T *evalarg,
3351 int want_string) // after "." operator
3352{
3353 type_T *want_type = NULL;
3354 garray_T type_list; // list of pointers to allocated types
3355 int res;
3356 int evaluate = evalarg == NULL ? 0
3357 : (evalarg->eval_flags & EVAL_EVALUATE);
3358
3359 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003360 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3361 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003362 {
3363 ++*arg;
3364 ga_init2(&type_list, sizeof(type_T *), 10);
3365 want_type = parse_type(arg, &type_list, TRUE);
3366 if (want_type == NULL && (evaluate || **arg != '>'))
3367 {
3368 clear_type_list(&type_list);
3369 return FAIL;
3370 }
3371
3372 if (**arg != '>')
3373 {
3374 if (*skipwhite(*arg) == '>')
3375 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3376 else
3377 emsg(_(e_missing_gt));
3378 clear_type_list(&type_list);
3379 return FAIL;
3380 }
3381 ++*arg;
3382 *arg = skipwhite_and_linebreak(*arg, evalarg);
3383 }
3384
3385 res = eval7(arg, rettv, evalarg, want_string);
3386
3387 if (want_type != NULL && evaluate)
3388 {
3389 if (res == OK)
3390 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003391 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3392 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003393
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003394 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003395 {
3396 if (want_type == &t_bool && actual != &t_bool
3397 && (actual->tt_flags & TTFLAG_BOOL_OK))
3398 {
3399 int n = tv2bool(rettv);
3400
3401 // can use "0" and "1" for boolean in some places
3402 clear_tv(rettv);
3403 rettv->v_type = VAR_BOOL;
3404 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3405 }
3406 else
3407 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003408 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003409
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003410 where.wt_variable = TRUE;
3411 res = check_type(want_type, actual, TRUE, where);
3412 }
3413 }
3414 }
3415 clear_type_list(&type_list);
3416 }
3417
3418 return res;
3419}
3420
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003421 int
3422eval_leader(char_u **arg, int vim9)
3423{
3424 char_u *s = *arg;
3425 char_u *p = *arg;
3426
3427 while (*p == '!' || *p == '-' || *p == '+')
3428 {
3429 char_u *n = skipwhite(p + 1);
3430
3431 // ++, --, -+ and +- are not accepted in Vim9 script
3432 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3433 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003434 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003435 return FAIL;
3436 }
3437 p = n;
3438 }
3439 *arg = p;
3440 return OK;
3441}
3442
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443/*
3444 * Handle sixth level expression:
3445 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003446 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003447 * "string" string constant
3448 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 * &option-name option value
3450 * @r register contents
3451 * identifier variable value
3452 * function() function call
3453 * $VAR environment variable
3454 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003455 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003456 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003457 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003458 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 *
3460 * Also handle:
3461 * ! in front logical NOT
3462 * - in front unary minus
3463 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003464 * trailing [] subscript in String or List
3465 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003466 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 *
3468 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003469 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 *
3471 * Return OK or FAIL.
3472 */
3473 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003474eval7(
3475 char_u **arg,
3476 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003477 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003478 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003480 int evaluate = evalarg != NULL
3481 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 int len;
3483 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 char_u *start_leader, *end_leader;
3485 int ret = OK;
3486 char_u *alias;
3487
3488 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003489 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003490 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003492 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493
3494 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003495 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 */
3497 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003498 if (eval_leader(arg, in_vim9script()) == FAIL)
3499 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 end_leader = *arg;
3501
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003502 if (**arg == '.' && (!isdigit(*(*arg + 1))
3503#ifdef FEAT_FLOAT
Bram Moolenaardd9de502021-08-15 13:49:42 +02003504 || in_old_script(2)
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003505#endif
3506 ))
3507 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003508 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003509 ++*arg;
3510 return FAIL;
3511 }
3512
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 switch (**arg)
3514 {
3515 /*
3516 * Number constant.
3517 */
3518 case '0':
3519 case '1':
3520 case '2':
3521 case '3':
3522 case '4':
3523 case '5':
3524 case '6':
3525 case '7':
3526 case '8':
3527 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003528 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003529
3530 // Apply prefixed "-" and "+" now. Matters especially when
3531 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003532 if (ret == OK && evaluate && end_leader > start_leader
3533 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003534 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 break;
3536
3537 /*
3538 * String constant: "string".
3539 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003540 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 break;
3542
3543 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003544 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003546 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003547 break;
3548
3549 /*
3550 * List: [expr, expr]
3551 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003552 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 break;
3554
3555 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003556 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003557 */
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003558 case '#': if (in_vim9script())
3559 {
3560 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3561 }
3562 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003563 {
3564 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003565 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003566 }
3567 else
3568 ret = NOTDONE;
3569 break;
3570
3571 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003572 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003573 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003574 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003575 case '{': if (in_vim9script())
3576 ret = NOTDONE;
3577 else
3578 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003579 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003580 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003581 break;
3582
3583 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003584 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003586 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 break;
3588
3589 /*
3590 * Environment variable: $VAR.
3591 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003592 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 break;
3594
3595 /*
3596 * Register contents: @r.
3597 */
3598 case '@': ++*arg;
3599 if (evaluate)
3600 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003601 if (in_vim9script() && IS_WHITE_OR_NUL(**arg))
3602 semsg(_(e_syntax_error_at_str), *arg);
3603 else if (in_vim9script() && !valid_yank_reg(**arg, FALSE))
3604 emsg_invreg(**arg);
3605 else
3606 {
3607 rettv->v_type = VAR_STRING;
3608 rettv->vval.v_string = get_reg_contents(**arg,
3609 GREG_EXPR_SRC);
3610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 }
3612 if (**arg != NUL)
3613 ++*arg;
3614 break;
3615
3616 /*
3617 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003618 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003620 case '(': ret = NOTDONE;
3621 if (in_vim9script())
Bram Moolenaar06409502021-02-17 17:00:27 +01003622 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003623 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003624 if (ret == OK && evaluate)
3625 {
3626 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3627
Bram Moolenaara9931532021-06-12 15:58:16 +02003628 // Compile it here to get the return type. The return
3629 // type is optional, when it's missing use t_unknown.
3630 // This is recognized in compile_return().
3631 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3632 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003633 if (compile_def_function(ufunc,
Bram Moolenaare99d4222021-06-13 14:01:26 +02003634 FALSE, COMPILE_TYPE(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003635 {
3636 clear_tv(rettv);
3637 ret = FAIL;
3638 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003639 }
3640 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003641 if (ret == NOTDONE)
3642 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003643 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003644 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003645
Bram Moolenaar9215f012020-06-27 21:18:00 +02003646 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003647 if (**arg == ')')
3648 ++*arg;
3649 else if (ret == OK)
3650 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003651 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003652 clear_tv(rettv);
3653 ret = FAIL;
3654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 }
3656 break;
3657
Bram Moolenaar8c711452005-01-14 21:53:12 +00003658 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 break;
3660 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003661
3662 if (ret == NOTDONE)
3663 {
3664 /*
3665 * Must be a variable or function name.
3666 * Can also be a curly-braces kind of name: {expr}.
3667 */
3668 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003669 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003670 if (alias != NULL)
3671 s = alias;
3672
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003673 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003674 ret = FAIL;
3675 else
3676 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003677 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3678
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003679 if (evaluate && in_vim9script() && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003680 {
3681 emsg(_(e_cannot_use_underscore_here));
3682 ret = FAIL;
3683 }
3684 else if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003685 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003686 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003687 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003688 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003689 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003690 else if (flags & EVAL_CONSTANT)
3691 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003692 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003693 {
3694 // get the value of "true", "false" or a variable
3695 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3696 {
3697 rettv->v_type = VAR_BOOL;
3698 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003699 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003700 }
3701 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003702 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003703 {
3704 rettv->v_type = VAR_BOOL;
3705 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003706 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003707 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003708 else if (len == 4 && in_vim9script()
3709 && STRNCMP(s, "null", 4) == 0)
3710 {
3711 rettv->v_type = VAR_SPECIAL;
3712 rettv->vval.v_number = VVAL_NULL;
3713 ret = OK;
3714 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003715 else
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003716 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003717 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003718 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003719 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003720 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003721 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003722 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003723 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003724 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003725 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003726 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003727 }
3728
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003729 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3730 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003731 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003732 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733
3734 /*
3735 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3736 */
3737 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003738 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003739 return ret;
3740}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003741
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003742/*
3743 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003744 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003745 * Adjusts "end_leaderp" until it is at "start_leader".
3746 */
3747 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003748eval7_leader(
3749 typval_T *rettv,
3750 int numeric_only,
3751 char_u *start_leader,
3752 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003753{
3754 char_u *end_leader = *end_leaderp;
3755 int ret = OK;
3756 int error = FALSE;
3757 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003758 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003759#ifdef FEAT_FLOAT
3760 float_T f = 0.0;
3761
3762 if (rettv->v_type == VAR_FLOAT)
3763 f = rettv->vval.v_float;
3764 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003765#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003766 {
3767 while (VIM_ISWHITE(end_leader[-1]))
3768 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003769 if (in_vim9script() && end_leader[-1] == '!')
3770 val = tv2bool(rettv);
3771 else
3772 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003773 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003774 if (error)
3775 {
3776 clear_tv(rettv);
3777 ret = FAIL;
3778 }
3779 else
3780 {
3781 while (end_leader > start_leader)
3782 {
3783 --end_leader;
3784 if (*end_leader == '!')
3785 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003786 if (numeric_only)
3787 {
3788 ++end_leader;
3789 break;
3790 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003791#ifdef FEAT_FLOAT
3792 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003793 {
3794 if (in_vim9script())
3795 {
3796 rettv->v_type = VAR_BOOL;
3797 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3798 }
3799 else
3800 f = !f;
3801 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003802 else
3803#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003804 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003805 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003806 type = VAR_BOOL;
3807 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003808 }
3809 else if (*end_leader == '-')
3810 {
3811#ifdef FEAT_FLOAT
3812 if (rettv->v_type == VAR_FLOAT)
3813 f = -f;
3814 else
3815#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003816 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003817 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003818 type = VAR_NUMBER;
3819 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003820 }
3821 }
3822#ifdef FEAT_FLOAT
3823 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003825 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003826 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003828 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003829#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003830 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003831 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003832 if (in_vim9script())
3833 rettv->v_type = type;
3834 else
3835 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003836 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003839 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 return ret;
3841}
3842
3843/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003844 * Call the function referred to in "rettv".
3845 */
3846 static int
3847call_func_rettv(
3848 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003849 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003850 typval_T *rettv,
3851 int evaluate,
3852 dict_T *selfdict,
3853 typval_T *basetv)
3854{
3855 partial_T *pt = NULL;
3856 funcexe_T funcexe;
3857 typval_T functv;
3858 char_u *s;
3859 int ret;
3860
3861 // need to copy the funcref so that we can clear rettv
3862 if (evaluate)
3863 {
3864 functv = *rettv;
3865 rettv->v_type = VAR_UNKNOWN;
3866
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003867 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003868 if (functv.v_type == VAR_PARTIAL)
3869 {
3870 pt = functv.vval.v_partial;
3871 s = partial_name(pt);
3872 }
3873 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003874 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003875 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003876 if (s == NULL || *s == NUL)
3877 {
3878 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02003879 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003880 goto theend;
3881 }
3882 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003883 }
3884 else
3885 s = (char_u *)"";
3886
Bram Moolenaara80faa82020-04-12 19:37:17 +02003887 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003888 funcexe.fe_firstline = curwin->w_cursor.lnum;
3889 funcexe.fe_lastline = curwin->w_cursor.lnum;
3890 funcexe.fe_evaluate = evaluate;
3891 funcexe.fe_partial = pt;
3892 funcexe.fe_selfdict = selfdict;
3893 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003894 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003895
Bram Moolenaar22db0d52021-06-12 12:16:55 +02003896theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003897 // Clear the funcref afterwards, so that deleting it while
3898 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003899 if (evaluate)
3900 clear_tv(&functv);
3901
3902 return ret;
3903}
3904
3905/*
3906 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003907 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003908 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3909 */
3910 static int
3911eval_lambda(
3912 char_u **arg,
3913 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003914 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003915 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003916{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003917 int evaluate = evalarg != NULL
3918 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003919 typval_T base = *rettv;
3920 int ret;
3921
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003922 rettv->v_type = VAR_UNKNOWN;
3923
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003924 if (**arg == '{')
3925 {
3926 // ->{lambda}()
3927 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3928 }
3929 else
3930 {
3931 // ->(lambda)()
3932 ++*arg;
3933 ret = eval1(arg, rettv, evalarg);
3934 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar605ec912021-12-18 16:54:31 +00003935 if (**arg == ')')
3936 {
3937 ++*arg;
3938 }
3939 else
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003940 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003941 emsg(_(e_missing_closing_paren));
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003942 ret = FAIL;
3943 }
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003944 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003945 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003946 return FAIL;
3947 else if (**arg != '(')
3948 {
3949 if (verbose)
3950 {
3951 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00003952 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003953 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003954 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003955 }
3956 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003957 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003958 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003959 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003960 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003961
3962 // Clear the funcref afterwards, so that deleting it while
3963 // evaluating the arguments is possible (see test55).
3964 if (evaluate)
3965 clear_tv(&base);
3966
3967 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003968}
3969
3970/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003971 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01003972 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02003973 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3974 */
3975 static int
3976eval_method(
3977 char_u **arg,
3978 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003979 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003980 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003981{
3982 char_u *name;
3983 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003984 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003985 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003986 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003987 int evaluate = evalarg != NULL
3988 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003989
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003990 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003991
Bram Moolenaarac92e252019-08-03 21:58:38 +02003992 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003993 len = get_name_len(arg, &alias, evaluate, TRUE);
3994 if (alias != NULL)
3995 name = alias;
3996
3997 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003998 {
3999 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004000 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004001 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004002 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004003 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004004 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004005 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004006 if (**arg != '(')
4007 {
4008 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004009 semsg(_(e_missing_parenthesis_str), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004010 ret = FAIL;
4011 }
Bram Moolenaar51841322019-08-08 21:10:01 +02004012 else if (VIM_ISWHITE((*arg)[-1]))
4013 {
4014 if (verbose)
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004015 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar51841322019-08-08 21:10:01 +02004016 ret = FAIL;
4017 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004018 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004019 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004020 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004021 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004022
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004023 // Clear the funcref afterwards, so that deleting it while
4024 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004025 if (evaluate)
4026 clear_tv(&base);
4027
Bram Moolenaarac92e252019-08-03 21:58:38 +02004028 return ret;
4029}
4030
4031/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004032 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4033 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004034 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4035 */
4036 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004037eval_index(
4038 char_u **arg,
4039 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004040 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004041 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004042{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004043 int evaluate = evalarg != NULL
4044 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004045 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004046 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004047 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004048 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004049 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004050 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004051
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004052 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4053 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004054
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004055 init_tv(&var1);
4056 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004057 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004058 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004059 /*
4060 * dict.name
4061 */
4062 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004063 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004064 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004065 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004066 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004067 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004068 }
4069 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004070 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004071 /*
4072 * something[idx]
4073 *
4074 * Get the (first) variable from inside the [].
4075 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004076 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004077 if (**arg == ':')
4078 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004079 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004080 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004081 else if (vim9 && **arg == ':')
4082 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004083 semsg(_(e_white_space_required_before_and_after_str_at_str),
4084 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004085 clear_tv(&var1);
4086 return FAIL;
4087 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004088 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004089 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004090 int error = FALSE;
4091
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004092#ifdef FEAT_FLOAT
4093 // allow for indexing with float
4094 if (vim9 && rettv->v_type == VAR_DICT
4095 && var1.v_type == VAR_FLOAT)
4096 {
4097 var1.vval.v_string = typval_tostring(&var1, TRUE);
4098 var1.v_type = VAR_STRING;
4099 }
4100#endif
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004101 if (vim9 && rettv->v_type == VAR_LIST)
4102 tv_get_number_chk(&var1, &error);
4103 else
4104 error = tv_get_string_chk(&var1) == NULL;
4105 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004106 {
4107 // not a number or string
4108 clear_tv(&var1);
4109 return FAIL;
4110 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004111 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004112
4113 /*
4114 * Get the second variable from inside the [:].
4115 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004116 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004117 if (**arg == ':')
4118 {
4119 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004120 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01004121 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004122 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004123 semsg(_(e_white_space_required_before_and_after_str_at_str),
4124 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004125 if (!empty1)
4126 clear_tv(&var1);
4127 return FAIL;
4128 }
4129 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004130 if (**arg == ']')
4131 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004132 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004133 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004134 if (!empty1)
4135 clear_tv(&var1);
4136 return FAIL;
4137 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004138 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004139 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004140 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004141 if (!empty1)
4142 clear_tv(&var1);
4143 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004144 return FAIL;
4145 }
4146 }
4147
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004148 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004149 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004150 if (**arg != ']')
4151 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004152 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004153 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004154 clear_tv(&var1);
4155 if (range)
4156 clear_tv(&var2);
4157 return FAIL;
4158 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004159 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004160 }
4161
4162 if (evaluate)
4163 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004164 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004165 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004166 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004167
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004168 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004169 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004170 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004171 clear_tv(&var2);
4172 return res;
4173 }
4174 return OK;
4175}
4176
4177/*
4178 * Check if "rettv" can have an [index] or [sli:ce]
4179 */
4180 int
4181check_can_index(typval_T *rettv, int evaluate, int verbose)
4182{
4183 switch (rettv->v_type)
4184 {
4185 case VAR_FUNC:
4186 case VAR_PARTIAL:
4187 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004188 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004189 return FAIL;
4190 case VAR_FLOAT:
4191#ifdef FEAT_FLOAT
4192 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004193 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004194 return FAIL;
4195#endif
4196 case VAR_BOOL:
4197 case VAR_SPECIAL:
4198 case VAR_JOB:
4199 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004200 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004201 if (verbose)
4202 emsg(_(e_cannot_index_special_variable));
4203 return FAIL;
4204 case VAR_UNKNOWN:
4205 case VAR_ANY:
4206 case VAR_VOID:
4207 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004208 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004209 emsg(_(e_cannot_index_special_variable));
4210 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004211 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004212 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004213
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004214 case VAR_STRING:
4215 case VAR_LIST:
4216 case VAR_DICT:
4217 case VAR_BLOB:
4218 break;
4219 case VAR_NUMBER:
4220 if (in_vim9script())
4221 emsg(_(e_cannot_index_number));
4222 break;
4223 }
4224 return OK;
4225}
4226
4227/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004228 * slice() function
4229 */
4230 void
4231f_slice(typval_T *argvars, typval_T *rettv)
4232{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004233 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004234 && ((argvars[0].v_type != VAR_STRING
4235 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004236 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004237 && check_for_list_arg(argvars, 0) == FAIL)
4238 || check_for_number_arg(argvars, 1) == FAIL
4239 || check_for_opt_number_arg(argvars, 2) == FAIL))
4240 return;
4241
Bram Moolenaar6601b622021-01-13 21:47:15 +01004242 if (check_can_index(argvars, TRUE, FALSE) == OK)
4243 {
4244 copy_tv(argvars, rettv);
4245 eval_index_inner(rettv, TRUE, argvars + 1,
4246 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4247 TRUE, NULL, 0, FALSE);
4248 }
4249}
4250
4251/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004252 * Apply index or range to "rettv".
4253 * "var1" is the first index, NULL for [:expr].
4254 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004255 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4256 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004257 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4258 */
4259 int
4260eval_index_inner(
4261 typval_T *rettv,
4262 int is_range,
4263 typval_T *var1,
4264 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004265 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004266 char_u *key,
4267 int keylen,
4268 int verbose)
4269{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004270 varnumber_T n1, n2 = 0;
4271 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004272
4273 n1 = 0;
4274 if (var1 != NULL && rettv->v_type != VAR_DICT)
4275 n1 = tv_get_number(var1);
4276
4277 if (is_range)
4278 {
4279 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004280 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004281 if (verbose)
4282 emsg(_(e_cannot_slice_dictionary));
4283 return FAIL;
4284 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004285 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004286 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004287 else
4288 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004289 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004290
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004291 switch (rettv->v_type)
4292 {
4293 case VAR_UNKNOWN:
4294 case VAR_ANY:
4295 case VAR_VOID:
4296 case VAR_FUNC:
4297 case VAR_PARTIAL:
4298 case VAR_FLOAT:
4299 case VAR_BOOL:
4300 case VAR_SPECIAL:
4301 case VAR_JOB:
4302 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004303 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004304 break; // not evaluating, skipping over subscript
4305
4306 case VAR_NUMBER:
4307 case VAR_STRING:
4308 {
4309 char_u *s = tv_get_string(rettv);
4310
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004311 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004312 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004313 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004314 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004315 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004316 else
4317 s = char_from_string(s, n1);
4318 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004319 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004320 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004321 // The resulting variable is a substring. If the indexes
4322 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004323 if (n1 < 0)
4324 {
4325 n1 = len + n1;
4326 if (n1 < 0)
4327 n1 = 0;
4328 }
4329 if (n2 < 0)
4330 n2 = len + n2;
4331 else if (n2 >= len)
4332 n2 = len;
4333 if (n1 >= len || n2 < 0 || n1 > n2)
4334 s = NULL;
4335 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004336 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004337 }
4338 else
4339 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004340 // The resulting variable is a string of a single
4341 // character. If the index is too big or negative the
4342 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004343 if (n1 >= len || n1 < 0)
4344 s = NULL;
4345 else
4346 s = vim_strnsave(s + n1, 1);
4347 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004348 clear_tv(rettv);
4349 rettv->v_type = VAR_STRING;
4350 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004351 }
4352 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004353
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004354 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004355 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4356 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004357 break;
4358
4359 case VAR_LIST:
4360 if (var1 == NULL)
4361 n1 = 0;
4362 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004363 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004364 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004365 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004366 return FAIL;
4367 break;
4368
4369 case VAR_DICT:
4370 {
4371 dictitem_T *item;
4372 typval_T tmp;
4373
4374 if (key == NULL)
4375 {
4376 key = tv_get_string_chk(var1);
4377 if (key == NULL)
4378 return FAIL;
4379 }
4380
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004381 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004382
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004383 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004384 {
4385 if (verbose)
4386 {
4387 if (keylen > 0)
4388 key[keylen] = NUL;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004389 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004390 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004391 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004392 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004393
4394 copy_tv(&item->di_tv, &tmp);
4395 clear_tv(rettv);
4396 *rettv = tmp;
4397 }
4398 break;
4399 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004400 return OK;
4401}
4402
4403/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004404 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004405 */
4406 char_u *
4407partial_name(partial_T *pt)
4408{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004409 if (pt != NULL)
4410 {
4411 if (pt->pt_name != NULL)
4412 return pt->pt_name;
4413 if (pt->pt_func != NULL)
4414 return pt->pt_func->uf_name;
4415 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004416 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004417}
4418
Bram Moolenaarddecc252016-04-06 22:59:37 +02004419 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004420partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004421{
4422 int i;
4423
4424 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004425 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004426 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004427 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004428 if (pt->pt_name != NULL)
4429 {
4430 func_unref(pt->pt_name);
4431 vim_free(pt->pt_name);
4432 }
4433 else
4434 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004435
Bram Moolenaar54656012021-06-09 20:50:46 +02004436 // "out_up" is no longer used, decrement refcount on partial that owns it.
4437 partial_unref(pt->pt_outer.out_up_partial);
4438
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004439 // Decrease the reference count for the context of a closure. If down
4440 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004441 if (pt->pt_funcstack != NULL)
4442 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004443 --pt->pt_funcstack->fs_refcount;
4444 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004445 }
4446
Bram Moolenaarddecc252016-04-06 22:59:37 +02004447 vim_free(pt);
4448}
4449
4450/*
4451 * Unreference a closure: decrement the reference count and free it when it
4452 * becomes zero.
4453 */
4454 void
4455partial_unref(partial_T *pt)
4456{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004457 if (pt != NULL)
4458 {
4459 if (--pt->pt_refcount <= 0)
4460 partial_free(pt);
4461
4462 // If the reference count goes down to one, the funcstack may be the
4463 // only reference and can be freed if no other partials reference it.
4464 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4465 funcstack_check_refcount(pt->pt_funcstack);
4466 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004467}
4468
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004469/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004470 * Return the next (unique) copy ID.
4471 * Used for serializing nested structures.
4472 */
4473 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004474get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004475{
4476 current_copyID += COPYID_INC;
4477 return current_copyID;
4478}
4479
4480/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004481 * Garbage collection for lists and dictionaries.
4482 *
4483 * We use reference counts to be able to free most items right away when they
4484 * are no longer used. But for composite items it's possible that it becomes
4485 * unused while the reference count is > 0: When there is a recursive
4486 * reference. Example:
4487 * :let l = [1, 2, 3]
4488 * :let d = {9: l}
4489 * :let l[1] = d
4490 *
4491 * Since this is quite unusual we handle this with garbage collection: every
4492 * once in a while find out which lists and dicts are not referenced from any
4493 * variable.
4494 *
4495 * Here is a good reference text about garbage collection (refers to Python
4496 * but it applies to all reference-counting mechanisms):
4497 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004498 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004499
4500/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004501 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004502 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004503 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004504 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004505 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004506garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004507{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004508 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004509 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004510 buf_T *buf;
4511 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004512 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004513 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004514
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004515 if (!testing)
4516 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004517 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004518 want_garbage_collect = FALSE;
4519 may_garbage_collect = FALSE;
4520 garbage_collect_at_exit = FALSE;
4521 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004522
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004523 // The execution stack can grow big, limit the size.
4524 if (exestack.ga_maxlen - exestack.ga_len > 500)
4525 {
4526 size_t new_len;
4527 char_u *pp;
4528 int n;
4529
4530 // Keep 150% of the current size, with a minimum of the growth size.
4531 n = exestack.ga_len / 2;
4532 if (n < exestack.ga_growsize)
4533 n = exestack.ga_growsize;
4534
4535 // Don't make it bigger though.
4536 if (exestack.ga_len + n < exestack.ga_maxlen)
4537 {
4538 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4539 pp = vim_realloc(exestack.ga_data, new_len);
4540 if (pp == NULL)
4541 return FAIL;
4542 exestack.ga_maxlen = exestack.ga_len + n;
4543 exestack.ga_data = pp;
4544 }
4545 }
4546
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004547 // We advance by two because we add one for items referenced through
4548 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004549 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004550
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004551 /*
4552 * 1. Go through all accessible variables and mark all lists and dicts
4553 * with copyID.
4554 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004555
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004556 // Don't free variables in the previous_funccal list unless they are only
4557 // referenced through previous_funccal. This must be first, because if
4558 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004559 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004560
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004561 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004562 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004563
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004564 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004565 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004566 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4567 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004568
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004569 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004570 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004571 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4572 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004573 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004574 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4575 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004576#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004577 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004578 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4579 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004580 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004581 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004582 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4583 NULL, NULL);
4584#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004585
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004586 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004587 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004588 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4589 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004590 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004591 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004592
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004593 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004594 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004595
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004596 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004597 abort = abort || set_ref_in_functions(copyID);
4598
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004599 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004600 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004601
Bram Moolenaar7509ad82021-12-14 18:14:37 +00004602 // funcstacks keep variables for closures
4603 abort = abort || set_ref_in_funcstacks(copyID);
4604
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004605 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004606 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004607
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004608 // callbacks in buffers
4609 abort = abort || set_ref_in_buffers(copyID);
4610
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00004611 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
4612 abort = abort || set_ref_in_insexpand_funcs(copyID);
4613
4614 // 'operatorfunc' callback
4615 abort = abort || set_ref_in_opfunc(copyID);
4616
4617 // 'tagfunc' callback
4618 abort = abort || set_ref_in_tagfunc(copyID);
4619
4620 // 'imactivatefunc' and 'imstatusfunc' callbacks
4621 abort = abort || set_ref_in_im_funcs(copyID);
4622
Bram Moolenaar1dced572012-04-05 16:54:08 +02004623#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004624 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004625#endif
4626
Bram Moolenaardb913952012-06-29 12:54:53 +02004627#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004628 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004629#endif
4630
4631#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004632 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004633#endif
4634
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004635#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004636 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004637 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004638#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004639#ifdef FEAT_NETBEANS_INTG
4640 abort = abort || set_ref_in_nb_channel(copyID);
4641#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004642
Bram Moolenaare3188e22016-05-31 21:13:04 +02004643#ifdef FEAT_TIMERS
4644 abort = abort || set_ref_in_timer(copyID);
4645#endif
4646
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004647#ifdef FEAT_QUICKFIX
4648 abort = abort || set_ref_in_quickfix(copyID);
4649#endif
4650
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004651#ifdef FEAT_TERMINAL
4652 abort = abort || set_ref_in_term(copyID);
4653#endif
4654
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004655#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004656 abort = abort || set_ref_in_popups(copyID);
4657#endif
4658
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004659 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004660 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004661 /*
4662 * 2. Free lists and dictionaries that are not referenced.
4663 */
4664 did_free = free_unref_items(copyID);
4665
4666 /*
4667 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004668 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004669 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004670 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004671 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004672 else if (p_verbose > 0)
4673 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004674 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004675 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004676
4677 return did_free;
4678}
4679
4680/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004681 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004682 */
4683 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004684free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004685{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004686 int did_free = FALSE;
4687
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004688 // Let all "free" functions know that we are here. This means no
4689 // dictionaries, lists, channels or jobs are to be freed, because we will
4690 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004691 in_free_unref_items = TRUE;
4692
4693 /*
4694 * PASS 1: free the contents of the items. We don't free the items
4695 * themselves yet, so that it is possible to decrement refcount counters
4696 */
4697
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004698 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004699 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004700
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004701 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004702 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004703
4704#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004705 // Go through the list of jobs and free items without the copyID. This
4706 // must happen before doing channels, because jobs refer to channels, but
4707 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004708 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4709
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004710 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004711 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4712#endif
4713
4714 /*
4715 * PASS 2: free the items themselves.
4716 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004717 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004718 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004719
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004720#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004721 // Go through the list of jobs and free items without the copyID. This
4722 // must happen before doing channels, because jobs refer to channels, but
4723 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004724 free_unused_jobs(copyID, COPYID_MASK);
4725
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004726 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004727 free_unused_channels(copyID, COPYID_MASK);
4728#endif
4729
4730 in_free_unref_items = FALSE;
4731
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004732 return did_free;
4733}
4734
4735/*
4736 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004737 * "list_stack" is used to add lists to be marked. Can be NULL.
4738 *
4739 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004740 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004741 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004742set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004743{
4744 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004745 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004746 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004747 hashtab_T *cur_ht;
4748 ht_stack_T *ht_stack = NULL;
4749 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004750
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004751 cur_ht = ht;
4752 for (;;)
4753 {
4754 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004755 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004756 // Mark each item in the hashtab. If the item contains a hashtab
4757 // it is added to ht_stack, if it contains a list it is added to
4758 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004759 todo = (int)cur_ht->ht_used;
4760 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4761 if (!HASHITEM_EMPTY(hi))
4762 {
4763 --todo;
4764 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4765 &ht_stack, list_stack);
4766 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004767 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004768
4769 if (ht_stack == NULL)
4770 break;
4771
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004772 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004773 cur_ht = ht_stack->ht;
4774 tempitem = ht_stack;
4775 ht_stack = ht_stack->prev;
4776 free(tempitem);
4777 }
4778
4779 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004780}
4781
4782/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004783 * Mark a dict and its items with "copyID".
4784 * Returns TRUE if setting references failed somehow.
4785 */
4786 int
4787set_ref_in_dict(dict_T *d, int copyID)
4788{
4789 if (d != NULL && d->dv_copyID != copyID)
4790 {
4791 d->dv_copyID = copyID;
4792 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4793 }
4794 return FALSE;
4795}
4796
4797/*
4798 * Mark a list and its items with "copyID".
4799 * Returns TRUE if setting references failed somehow.
4800 */
4801 int
4802set_ref_in_list(list_T *ll, int copyID)
4803{
4804 if (ll != NULL && ll->lv_copyID != copyID)
4805 {
4806 ll->lv_copyID = copyID;
4807 return set_ref_in_list_items(ll, copyID, NULL);
4808 }
4809 return FALSE;
4810}
4811
4812/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004813 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004814 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4815 *
4816 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004817 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004818 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004819set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004820{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004821 listitem_T *li;
4822 int abort = FALSE;
4823 list_T *cur_l;
4824 list_stack_T *list_stack = NULL;
4825 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004826
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004827 cur_l = l;
4828 for (;;)
4829 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004830 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004831 // Mark each item in the list. If the item contains a hashtab
4832 // it is added to ht_stack, if it contains a list it is added to
4833 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004834 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4835 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4836 ht_stack, &list_stack);
4837 if (list_stack == NULL)
4838 break;
4839
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004840 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004841 cur_l = list_stack->list;
4842 tempitem = list_stack;
4843 list_stack = list_stack->prev;
4844 free(tempitem);
4845 }
4846
4847 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004848}
4849
4850/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00004851 * Mark the partial in callback 'cb' with "copyID".
4852 */
4853 int
4854set_ref_in_callback(callback_T *cb, int copyID)
4855{
4856 typval_T tv;
4857
4858 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
4859 return FALSE;
4860
4861 tv.v_type = VAR_PARTIAL;
4862 tv.vval.v_partial = cb->cb_partial;
4863 return set_ref_in_item(&tv, copyID, NULL, NULL);
4864}
4865
4866/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004867 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004868 * "list_stack" is used to add lists to be marked. Can be NULL.
4869 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4870 *
4871 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004872 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004873 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004874set_ref_in_item(
4875 typval_T *tv,
4876 int copyID,
4877 ht_stack_T **ht_stack,
4878 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004879{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004880 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004881
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004882 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004883 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004884 dict_T *dd = tv->vval.v_dict;
4885
Bram Moolenaara03f2332016-02-06 18:09:59 +01004886 if (dd != NULL && dd->dv_copyID != copyID)
4887 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004888 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004889 dd->dv_copyID = copyID;
4890 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004891 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004892 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4893 }
4894 else
4895 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004896 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4897
Bram Moolenaara03f2332016-02-06 18:09:59 +01004898 if (newitem == NULL)
4899 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004900 else
4901 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004902 newitem->ht = &dd->dv_hashtab;
4903 newitem->prev = *ht_stack;
4904 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004905 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004906 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004907 }
4908 }
4909 else if (tv->v_type == VAR_LIST)
4910 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004911 list_T *ll = tv->vval.v_list;
4912
Bram Moolenaara03f2332016-02-06 18:09:59 +01004913 if (ll != NULL && ll->lv_copyID != copyID)
4914 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004915 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004916 ll->lv_copyID = copyID;
4917 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004918 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004919 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004920 }
4921 else
4922 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004923 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4924
Bram Moolenaara03f2332016-02-06 18:09:59 +01004925 if (newitem == NULL)
4926 abort = TRUE;
4927 else
4928 {
4929 newitem->list = ll;
4930 newitem->prev = *list_stack;
4931 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004932 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004933 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004934 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004935 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004936 else if (tv->v_type == VAR_FUNC)
4937 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004938 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004939 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004940 else if (tv->v_type == VAR_PARTIAL)
4941 {
4942 partial_T *pt = tv->vval.v_partial;
4943 int i;
4944
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004945 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004946 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004947 // Didn't see this partial yet.
4948 pt->pt_copyID = copyID;
4949
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004950 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004951
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004952 if (pt->pt_dict != NULL)
4953 {
4954 typval_T dtv;
4955
4956 dtv.v_type = VAR_DICT;
4957 dtv.vval.v_dict = pt->pt_dict;
4958 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4959 }
4960
4961 for (i = 0; i < pt->pt_argc; ++i)
4962 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4963 ht_stack, list_stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +00004964 // pt_funcstack is handled in set_ref_in_funcstacks()
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004965 }
4966 }
4967#ifdef FEAT_JOB_CHANNEL
4968 else if (tv->v_type == VAR_JOB)
4969 {
4970 job_T *job = tv->vval.v_job;
4971 typval_T dtv;
4972
4973 if (job != NULL && job->jv_copyID != copyID)
4974 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004975 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004976 if (job->jv_channel != NULL)
4977 {
4978 dtv.v_type = VAR_CHANNEL;
4979 dtv.vval.v_channel = job->jv_channel;
4980 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4981 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004982 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004983 {
4984 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004985 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004986 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4987 }
4988 }
4989 }
4990 else if (tv->v_type == VAR_CHANNEL)
4991 {
4992 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004993 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004994 typval_T dtv;
4995 jsonq_T *jq;
4996 cbq_T *cq;
4997
4998 if (ch != NULL && ch->ch_copyID != copyID)
4999 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005000 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005001 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005002 {
5003 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5004 jq = jq->jq_next)
5005 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5006 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5007 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005008 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005009 {
5010 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005011 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005012 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5013 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005014 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005015 {
5016 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005017 dtv.vval.v_partial =
5018 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005019 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5020 }
5021 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005022 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005023 {
5024 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005025 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005026 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5027 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005028 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005029 {
5030 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005031 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005032 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5033 }
5034 }
5035 }
5036#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005037 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005038}
5039
Bram Moolenaar8c711452005-01-14 21:53:12 +00005040/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005041 * Return a string with the string representation of a variable.
5042 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005043 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005044 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005045 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005046 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005047 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005048 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5049 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005050 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005051 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005052 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005053echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005054 typval_T *tv,
5055 char_u **tofree,
5056 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005057 int copyID,
5058 int echo_style,
5059 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005060 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005061{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005062 static int recurse = 0;
5063 char_u *r = NULL;
5064
Bram Moolenaar33570922005-01-25 22:26:29 +00005065 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005066 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005067 if (!did_echo_string_emsg)
5068 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005069 // Only give this message once for a recursive call to avoid
5070 // flooding the user with errors. And stop iterating over lists
5071 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005072 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005073 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005074 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005075 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005076 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005077 }
5078 ++recurse;
5079
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005080 switch (tv->v_type)
5081 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005082 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005083 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005084 {
5085 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005086 r = tv->vval.v_string;
5087 if (r == NULL)
5088 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005089 }
5090 else
5091 {
5092 *tofree = string_quote(tv->vval.v_string, FALSE);
5093 r = *tofree;
5094 }
5095 break;
5096
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005097 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005098 if (echo_style)
5099 {
5100 *tofree = NULL;
5101 r = tv->vval.v_string;
5102 }
5103 else
5104 {
5105 *tofree = string_quote(tv->vval.v_string, TRUE);
5106 r = *tofree;
5107 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005108 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005109
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005110 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005111 {
5112 partial_T *pt = tv->vval.v_partial;
5113 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005114 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005115 garray_T ga;
5116 int i;
5117 char_u *tf;
5118
5119 ga_init2(&ga, 1, 100);
5120 ga_concat(&ga, (char_u *)"function(");
5121 if (fname != NULL)
5122 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005123 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005124 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005125 && vim_isupper(fname[1]))
5126 {
5127 ga_concat(&ga, (char_u *)"'g:");
5128 ga_concat(&ga, fname + 1);
5129 }
5130 else
5131 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005132 vim_free(fname);
5133 }
5134 if (pt != NULL && pt->pt_argc > 0)
5135 {
5136 ga_concat(&ga, (char_u *)", [");
5137 for (i = 0; i < pt->pt_argc; ++i)
5138 {
5139 if (i > 0)
5140 ga_concat(&ga, (char_u *)", ");
5141 ga_concat(&ga,
5142 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5143 vim_free(tf);
5144 }
5145 ga_concat(&ga, (char_u *)"]");
5146 }
5147 if (pt != NULL && pt->pt_dict != NULL)
5148 {
5149 typval_T dtv;
5150
5151 ga_concat(&ga, (char_u *)", ");
5152 dtv.v_type = VAR_DICT;
5153 dtv.vval.v_dict = pt->pt_dict;
5154 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5155 vim_free(tf);
5156 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005157 // terminate with ')' and a NUL
5158 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005159
5160 *tofree = ga.ga_data;
5161 r = *tofree;
5162 break;
5163 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005164
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005165 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005166 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005167 break;
5168
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005169 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005170 if (tv->vval.v_list == NULL)
5171 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005172 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005173 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005174 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005175 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005176 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5177 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005178 {
5179 *tofree = NULL;
5180 r = (char_u *)"[...]";
5181 }
5182 else
5183 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005184 int old_copyID = tv->vval.v_list->lv_copyID;
5185
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005186 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005187 *tofree = list2string(tv, copyID, restore_copyID);
5188 if (restore_copyID)
5189 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005190 r = *tofree;
5191 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005192 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005193
Bram Moolenaar8c711452005-01-14 21:53:12 +00005194 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005195 if (tv->vval.v_dict == NULL)
5196 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005197 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005198 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005199 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005200 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005201 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5202 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005203 {
5204 *tofree = NULL;
5205 r = (char_u *)"{...}";
5206 }
5207 else
5208 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005209 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005210
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005211 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005212 *tofree = dict2string(tv, copyID, restore_copyID);
5213 if (restore_copyID)
5214 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005215 r = *tofree;
5216 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005217 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005218
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005219 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005220 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005221 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005222 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005223 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005224 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005225 break;
5226
Bram Moolenaar835dc632016-02-07 14:27:38 +01005227 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005228 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005229#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005230 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005231 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5232 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005233 if (composite_val)
5234 {
5235 *tofree = string_quote(r, FALSE);
5236 r = *tofree;
5237 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005238#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005239 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005240
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005241 case VAR_INSTR:
5242 *tofree = NULL;
5243 r = (char_u *)"instructions";
5244 break;
5245
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005246 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005247#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005248 *tofree = NULL;
5249 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5250 r = numbuf;
5251 break;
5252#endif
5253
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005254 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005255 case VAR_SPECIAL:
5256 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005257 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005258 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005259 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005260
Bram Moolenaar8502c702014-06-17 12:51:16 +02005261 if (--recurse == 0)
5262 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005263 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005264}
5265
5266/*
5267 * Return a string with the string representation of a variable.
5268 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5269 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005270 * Does not put quotes around strings, as ":echo" displays values.
5271 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5272 * May return NULL.
5273 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005274 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005275echo_string(
5276 typval_T *tv,
5277 char_u **tofree,
5278 char_u *numbuf,
5279 int copyID)
5280{
5281 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5282}
5283
5284/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005285 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5286 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005287 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005288 */
5289 int
5290buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5291{
5292 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005293 char_u *t;
5294 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005295
5296 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5297 return -1;
5298
5299 if (lnum > buf->b_ml.ml_line_count)
5300 lnum = buf->b_ml.ml_line_count;
5301
5302 str = ml_get_buf(buf, lnum, FALSE);
5303 if (str == NULL)
5304 return -1;
5305
5306 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005307 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005308
Bram Moolenaar91458462021-01-13 20:08:38 +01005309 // count the number of characters
5310 t = str;
5311 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5312 t += mb_ptr2len(t);
5313
5314 // In insert mode, when the cursor is at the end of a non-empty line,
5315 // byteidx points to the NUL character immediately past the end of the
5316 // string. In this case, add one to the character count.
5317 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5318 count++;
5319
5320 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005321}
5322
5323/*
5324 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005325 * byte index. Works only for loaded buffers. Returns -1 on failure.
5326 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005327 */
5328 int
5329buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5330{
5331 char_u *str;
5332 char_u *t;
5333
5334 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5335 return -1;
5336
5337 if (lnum > buf->b_ml.ml_line_count)
5338 lnum = buf->b_ml.ml_line_count;
5339
5340 str = ml_get_buf(buf, lnum, FALSE);
5341 if (str == NULL)
5342 return -1;
5343
5344 // Convert the character offset to a byte offset
5345 t = str;
5346 while (*t != NUL && --charidx > 0)
5347 t += mb_ptr2len(t);
5348
Bram Moolenaar91458462021-01-13 20:08:38 +01005349 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005350}
5351
5352/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005354 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005356 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005357var2fpos(
5358 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005359 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005360 int *fnum, // set to fnum for '0, 'A, etc.
5361 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005363 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005365 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005367 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005368 if (varp->v_type == VAR_LIST)
5369 {
5370 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005371 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005372 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005373 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005374
5375 l = varp->vval.v_list;
5376 if (l == NULL)
5377 return NULL;
5378
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005379 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005380 pos.lnum = list_find_nr(l, 0L, &error);
5381 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005382 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005383 if (charcol)
5384 len = (long)mb_charlen(ml_get(pos.lnum));
5385 else
5386 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005387
Bram Moolenaarec65d772020-08-20 22:29:12 +02005388 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005389 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005390 li = list_find(l, 1L);
5391 if (li != NULL && li->li_tv.v_type == VAR_STRING
5392 && li->li_tv.vval.v_string != NULL
5393 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005394 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005395 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005396 }
5397 else
5398 {
5399 pos.col = list_find_nr(l, 1L, &error);
5400 if (error)
5401 return NULL;
5402 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005403
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005404 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005405 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005406 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005407 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005408
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005409 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005410 pos.coladd = list_find_nr(l, 2L, &error);
5411 if (error)
5412 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005413
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005414 return &pos;
5415 }
5416
Bram Moolenaarc5809432021-03-27 21:23:30 +01005417 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5418 return NULL;
5419
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005420 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005421 if (name == NULL)
5422 return NULL;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005423 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005424 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005425 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005426 pos = curwin->w_cursor;
5427 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005428 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005429 return &pos;
5430 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005431 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005432 {
5433 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005434 pos = VIsual;
5435 else
5436 pos = curwin->w_cursor;
5437 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005438 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005439 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005440 }
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005441 if (name[0] == '\'' && (!in_vim9script()
5442 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005444 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005445 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5447 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005448 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005449 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 return pp;
5451 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005452
Bram Moolenaara5525202006-03-02 22:52:09 +00005453 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005454
Bram Moolenaar477933c2007-07-17 14:32:23 +00005455 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005456 {
5457 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005458 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005459 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005460 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005461 // In silent Ex mode topline is zero, but that's not a valid line
5462 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005463 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005464 return &pos;
5465 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005466 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005467 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005468 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005469 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005470 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005471 return &pos;
5472 }
5473 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005474 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005476 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 {
5478 pos.lnum = curbuf->b_ml.ml_line_count;
5479 pos.col = 0;
5480 }
5481 else
5482 {
5483 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005484 if (charcol)
5485 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5486 else
5487 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 }
5489 return &pos;
5490 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005491 if (in_vim9script())
5492 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 return NULL;
5494}
5495
5496/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005497 * Convert list in "arg" into a position and optional file number.
5498 * When "fnump" is NULL there is no file number, only 3 items.
5499 * Note that the column is passed on as-is, the caller may want to decrement
5500 * it to use 1 for the first column.
5501 * Return FAIL when conversion is not possible, doesn't check the position for
5502 * validity.
5503 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005504 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005505list2fpos(
5506 typval_T *arg,
5507 pos_T *posp,
5508 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005509 colnr_T *curswantp,
5510 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005511{
5512 list_T *l = arg->vval.v_list;
5513 long i = 0;
5514 long n;
5515
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005516 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5517 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005518 if (arg->v_type != VAR_LIST
5519 || l == NULL
5520 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005521 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005522 return FAIL;
5523
5524 if (fnump != NULL)
5525 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005526 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005527 if (n < 0)
5528 return FAIL;
5529 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005530 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005531 *fnump = n;
5532 }
5533
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005534 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005535 if (n < 0)
5536 return FAIL;
5537 posp->lnum = n;
5538
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005539 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005540 if (n < 0)
5541 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005542 // If character position is specified, then convert to byte position
5543 if (charcol)
5544 {
5545 buf_T *buf;
5546
5547 // Get the text for the specified line in a loaded buffer
5548 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5549 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5550 return FAIL;
5551
Bram Moolenaar91458462021-01-13 20:08:38 +01005552 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005553 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005554 posp->col = n;
5555
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005556 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005557 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005558 posp->coladd = 0;
5559 else
5560 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005561
Bram Moolenaar493c1782014-05-28 14:34:46 +02005562 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005563 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005564
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005565 return OK;
5566}
5567
5568/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 * Get the length of an environment variable name.
5570 * Advance "arg" to the first character after the name.
5571 * Return 0 for error.
5572 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005573 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005574get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575{
5576 char_u *p;
5577 int len;
5578
5579 for (p = *arg; vim_isIDc(*p); ++p)
5580 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005581 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 return 0;
5583
5584 len = (int)(p - *arg);
5585 *arg = p;
5586 return len;
5587}
5588
5589/*
5590 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005591 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 * Return 0 if something is wrong.
5593 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005594 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005595get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596{
5597 char_u *p;
5598 int len;
5599
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005600 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005602 {
5603 if (*p == ':')
5604 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005605 // "s:" is start of "s:var", but "n:" is not and can be used in
5606 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005607 len = (int)(p - *arg);
5608 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5609 || len > 1)
5610 break;
5611 }
5612 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005613 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 return 0;
5615
5616 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005617 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618
5619 return len;
5620}
5621
5622/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005623 * Get the length of the name of a variable or function.
5624 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005626 * Return -1 if curly braces expansion failed.
5627 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 * If the name contains 'magic' {}'s, expand them and return the
5629 * expanded name in an allocated string via 'alias' - caller must free.
5630 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005631 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005632get_name_len(
5633 char_u **arg,
5634 char_u **alias,
5635 int evaluate,
5636 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637{
5638 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 char_u *p;
5640 char_u *expr_start;
5641 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005643 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644
5645 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5646 && (*arg)[2] == (int)KE_SNR)
5647 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005648 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 *arg += 3;
5650 return get_id_len(arg) + 3;
5651 }
5652 len = eval_fname_script(*arg);
5653 if (len > 0)
5654 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005655 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 *arg += len;
5657 }
5658
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005660 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005662 p = find_name_end(*arg, &expr_start, &expr_end,
5663 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 if (expr_start != NULL)
5665 {
5666 char_u *temp_string;
5667
5668 if (!evaluate)
5669 {
5670 len += (int)(p - *arg);
5671 *arg = skipwhite(p);
5672 return len;
5673 }
5674
5675 /*
5676 * Include any <SID> etc in the expanded string:
5677 * Thus the -len here.
5678 */
5679 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5680 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005681 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 *alias = temp_string;
5683 *arg = skipwhite(p);
5684 return (int)STRLEN(temp_string);
5685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686
5687 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005688 // Only give an error when there is something, otherwise it will be
5689 // reported at a higher level.
5690 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005691 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692
5693 return len;
5694}
5695
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005696/*
5697 * Find the end of a variable or function name, taking care of magic braces.
5698 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5699 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005700 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005701 * Return a pointer to just after the name. Equal to "arg" if there is no
5702 * valid name.
5703 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005704 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005705find_name_end(
5706 char_u *arg,
5707 char_u **expr_start,
5708 char_u **expr_end,
5709 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005711 int mb_nest = 0;
5712 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005714 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005715 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005717 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005719 *expr_start = NULL;
5720 *expr_end = NULL;
5721 }
5722
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005723 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005724 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5725 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005726 return arg;
5727
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005728 for (p = arg; *p != NUL
5729 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005730 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005731 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005732 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005733 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005734 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005735 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005736 if (*p == '\'')
5737 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005738 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005739 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005740 ;
5741 if (*p == NUL)
5742 break;
5743 }
5744 else if (*p == '"')
5745 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005746 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005747 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005748 if (*p == '\\' && p[1] != NUL)
5749 ++p;
5750 if (*p == NUL)
5751 break;
5752 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005753 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5754 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005755 // "s:" is start of "s:var", but "n:" is not and can be used in
5756 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005757 len = (int)(p - arg);
5758 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005759 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005760 break;
5761 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005762
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005763 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005765 if (*p == '[')
5766 ++br_nest;
5767 else if (*p == ']')
5768 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005770
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005771 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005773 if (*p == '{')
5774 {
5775 mb_nest++;
5776 if (expr_start != NULL && *expr_start == NULL)
5777 *expr_start = p;
5778 }
5779 else if (*p == '}')
5780 {
5781 mb_nest--;
5782 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5783 *expr_end = p;
5784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 }
5787
5788 return p;
5789}
5790
5791/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005792 * Expands out the 'magic' {}'s in a variable/function name.
5793 * Note that this can call itself recursively, to deal with
5794 * constructs like foo{bar}{baz}{bam}
5795 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5796 * "in_start" ^
5797 * "expr_start" ^
5798 * "expr_end" ^
5799 * "in_end" ^
5800 *
5801 * Returns a new allocated string, which the caller must free.
5802 * Returns NULL for failure.
5803 */
5804 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005805make_expanded_name(
5806 char_u *in_start,
5807 char_u *expr_start,
5808 char_u *expr_end,
5809 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005810{
5811 char_u c1;
5812 char_u *retval = NULL;
5813 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005814
5815 if (expr_end == NULL || in_end == NULL)
5816 return NULL;
5817 *expr_start = NUL;
5818 *expr_end = NUL;
5819 c1 = *in_end;
5820 *in_end = NUL;
5821
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005822 temp_result = eval_to_string(expr_start + 1, FALSE);
5823 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005824 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005825 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5826 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005827 if (retval != NULL)
5828 {
5829 STRCPY(retval, in_start);
5830 STRCAT(retval, temp_result);
5831 STRCAT(retval, expr_end + 1);
5832 }
5833 }
5834 vim_free(temp_result);
5835
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005836 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005837 *expr_start = '{';
5838 *expr_end = '}';
5839
5840 if (retval != NULL)
5841 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005842 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005843 if (expr_start != NULL)
5844 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005845 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005846 temp_result = make_expanded_name(retval, expr_start,
5847 expr_end, temp_result);
5848 vim_free(retval);
5849 retval = temp_result;
5850 }
5851 }
5852
5853 return retval;
5854}
5855
5856/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005858 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005860 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005861eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005863 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005864}
5865
5866/*
5867 * Return TRUE if character "c" can be used as the first character in a
5868 * variable or function name (excluding '{' and '}').
5869 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005870 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005871eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005872{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005873 return ASCII_ISALPHA(c) || c == '_';
5874}
5875
5876/*
5877 * Return TRUE if character "c" can be used as the first character of a
5878 * dictionary key.
5879 */
5880 int
5881eval_isdictc(int c)
5882{
5883 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884}
5885
5886/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005887 * Handle:
5888 * - expr[expr], expr[expr:expr] subscript
5889 * - ".name" lookup
5890 * - function call with Funcref variable: func(expr)
5891 * - method call: var->method()
5892 *
5893 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005894 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005895 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005896handle_subscript(
5897 char_u **arg,
5898 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005899 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005900 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005901{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005902 int evaluate = evalarg != NULL
5903 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005904 int ret = OK;
5905 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005906 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005907 int getnext;
5908 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005909
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005910 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005911 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005912 // When at the end of the line and ".name" or "->{" or "->X" follows in
5913 // the next line then consume the line break.
5914 p = eval_next_non_blank(*arg, evalarg, &getnext);
5915 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005916 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02005917 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
5918 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
5919 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005920 {
5921 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005922 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005923 check_white = FALSE;
5924 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005925
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01005926 if (rettv->v_type == VAR_ANY)
5927 {
5928 char_u *exp_name;
5929 int cc;
5930 int idx;
5931 ufunc_T *ufunc;
5932 type_T *type;
5933
Bram Moolenaard5f400c2022-01-06 21:10:28 +00005934 // Found script from "import {name} as name", script item name must
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01005935 // follow.
5936 if (**arg != '.')
5937 {
5938 if (verbose)
5939 semsg(_(e_expected_str_but_got_str), "'.'", *arg);
5940 ret = FAIL;
5941 break;
5942 }
5943 ++*arg;
5944 if (IS_WHITE_OR_NUL(**arg))
5945 {
5946 if (verbose)
5947 emsg(_(e_no_white_space_allowed_after_dot));
5948 ret = FAIL;
5949 break;
5950 }
5951
5952 // isolate the name
5953 exp_name = *arg;
5954 while (eval_isnamec(**arg))
5955 ++*arg;
5956 cc = **arg;
5957 **arg = NUL;
5958
5959 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
5960 evalarg->eval_cctx, verbose);
5961 **arg = cc;
5962 *arg = skipwhite(*arg);
5963
5964 if (idx < 0 && ufunc == NULL)
5965 {
5966 ret = FAIL;
5967 break;
5968 }
5969 if (idx >= 0)
5970 {
5971 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
5972 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
5973
5974 copy_tv(sv->sv_tv, rettv);
5975 }
5976 else
5977 {
5978 rettv->v_type = VAR_FUNC;
5979 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
5980 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00005981 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01005982 }
5983
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005984 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5985 || rettv->v_type == VAR_PARTIAL))
5986 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005987 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005988 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5989 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005990
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005991 // Stop the expression evaluation when immediately aborting on
5992 // error, or when an interrupt occurred or an exception was thrown
5993 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005994 if (aborting())
5995 {
5996 if (ret == OK)
5997 clear_tv(rettv);
5998 ret = FAIL;
5999 }
6000 dict_unref(selfdict);
6001 selfdict = NULL;
6002 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006003 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006004 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006005 if (in_vim9script())
6006 *arg = skipwhite(p + 2);
6007 else
6008 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006009 if (ret == OK)
6010 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006011 if (VIM_ISWHITE(**arg))
6012 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00006013 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006014 ret = FAIL;
6015 }
6016 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01006017 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006018 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006019 else
6020 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02006021 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006022 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02006023 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006024 // "." is ".name" lookup when we found a dict or when evaluating and
6025 // scriptversion is at least 2, where string concatenation is "..".
6026 else if (**arg == '['
6027 || (**arg == '.' && (rettv->v_type == VAR_DICT
6028 || (!evaluate
6029 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006030 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006031 {
6032 dict_unref(selfdict);
6033 if (rettv->v_type == VAR_DICT)
6034 {
6035 selfdict = rettv->vval.v_dict;
6036 if (selfdict != NULL)
6037 ++selfdict->dv_refcount;
6038 }
6039 else
6040 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006041 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006042 {
6043 clear_tv(rettv);
6044 ret = FAIL;
6045 }
6046 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006047 else
6048 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006049 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006050
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006051 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6052 // Don't do this when "Func" is already a partial that was bound
6053 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006054 if (selfdict != NULL
6055 && (rettv->v_type == VAR_FUNC
6056 || (rettv->v_type == VAR_PARTIAL
6057 && (rettv->vval.v_partial->pt_auto
6058 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006059 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006060
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006061 dict_unref(selfdict);
6062 return ret;
6063}
6064
6065/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006066 * Make a copy of an item.
6067 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006068 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6069 * reference to an already copied list/dict can be used.
6070 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006071 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006072 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006073item_copy(
6074 typval_T *from,
6075 typval_T *to,
6076 int deep,
6077 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006078{
6079 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006080 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006081
Bram Moolenaar33570922005-01-25 22:26:29 +00006082 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006083 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006084 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006085 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006086 }
6087 ++recurse;
6088
6089 switch (from->v_type)
6090 {
6091 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006092 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006093 case VAR_STRING:
6094 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006095 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006096 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006097 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006098 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006099 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006100 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006101 copy_tv(from, to);
6102 break;
6103 case VAR_LIST:
6104 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006105 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006106 if (from->vval.v_list == NULL)
6107 to->vval.v_list = NULL;
6108 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6109 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006110 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006111 to->vval.v_list = from->vval.v_list->lv_copylist;
6112 ++to->vval.v_list->lv_refcount;
6113 }
6114 else
6115 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
6116 if (to->vval.v_list == NULL)
6117 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006118 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006119 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006120 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006121 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006122 case VAR_DICT:
6123 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006124 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006125 if (from->vval.v_dict == NULL)
6126 to->vval.v_dict = NULL;
6127 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6128 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006129 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006130 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6131 ++to->vval.v_dict->dv_refcount;
6132 }
6133 else
6134 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
6135 if (to->vval.v_dict == NULL)
6136 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006137 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006138 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006139 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006140 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006141 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006142 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006143 }
6144 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006145 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006146}
6147
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006148 void
6149echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6150{
6151 char_u *tofree;
6152 char_u numbuf[NUMBUFLEN];
6153 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6154
6155 if (*atstart)
6156 {
6157 *atstart = FALSE;
6158 // Call msg_start() after eval1(), evaluating the expression
6159 // may cause a message to appear.
6160 if (with_space)
6161 {
6162 // Mark the saved text as finishing the line, so that what
6163 // follows is displayed on a new line when scrolling back
6164 // at the more prompt.
6165 msg_sb_eol();
6166 msg_start();
6167 }
6168 }
6169 else if (with_space)
6170 msg_puts_attr(" ", echo_attr);
6171
6172 if (p != NULL)
6173 for ( ; *p != NUL && !got_int; ++p)
6174 {
6175 if (*p == '\n' || *p == '\r' || *p == TAB)
6176 {
6177 if (*p != TAB && *needclr)
6178 {
6179 // remove any text still there from the command
6180 msg_clr_eos();
6181 *needclr = FALSE;
6182 }
6183 msg_putchar_attr(*p, echo_attr);
6184 }
6185 else
6186 {
6187 if (has_mbyte)
6188 {
6189 int i = (*mb_ptr2len)(p);
6190
6191 (void)msg_outtrans_len_attr(p, i, echo_attr);
6192 p += i - 1;
6193 }
6194 else
6195 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6196 }
6197 }
6198 vim_free(tofree);
6199}
6200
Bram Moolenaare9a41262005-01-15 22:18:47 +00006201/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 * ":echo expr1 ..." print each argument separated with a space, add a
6203 * newline at the end.
6204 * ":echon expr1 ..." print each argument plain.
6205 */
6206 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006207ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208{
6209 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006210 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006211 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 int needclr = TRUE;
6213 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006214 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006215 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006216 evalarg_T evalarg;
6217
Bram Moolenaare707c882020-07-01 13:07:37 +02006218 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219
6220 if (eap->skip)
6221 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006222 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006224 // If eval1() causes an error message the text from the command may
6225 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006226 need_clr_eos = needclr;
6227
Bram Moolenaar68db9962021-05-09 23:19:22 +02006228 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006229 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 {
6231 /*
6232 * Report the invalid expression unless the expression evaluation
6233 * has been cancelled due to an aborting error, an interrupt, or an
6234 * exception.
6235 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006236 if (!aborting() && did_emsg == did_emsg_before
6237 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006238 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006239 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 break;
6241 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006242 need_clr_eos = FALSE;
6243
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006245 {
6246 if (rettv.v_type == VAR_VOID)
6247 {
6248 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6249 break;
6250 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006251 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006252 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006253
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006254 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 arg = skipwhite(arg);
6256 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006257 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006258 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259
6260 if (eap->skip)
6261 --emsg_skip;
6262 else
6263 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006264 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 if (needclr)
6266 msg_clr_eos();
6267 if (eap->cmdidx == CMD_echo)
6268 msg_end();
6269 }
6270}
6271
6272/*
6273 * ":echohl {name}".
6274 */
6275 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006276ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006278 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279}
6280
6281/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006282 * Returns the :echo attribute
6283 */
6284 int
6285get_echo_attr(void)
6286{
6287 return echo_attr;
6288}
6289
6290/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 * ":execute expr1 ..." execute the result of an expression.
6292 * ":echomsg expr1 ..." Print a message
6293 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006294 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 * Each gets spaces around each argument and a newline at the end for
6296 * echo commands
6297 */
6298 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006299ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300{
6301 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006302 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 int ret = OK;
6304 char_u *p;
6305 garray_T ga;
6306 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006307 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308
6309 ga_init2(&ga, 1, 80);
6310
6311 if (eap->skip)
6312 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006313 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006315 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006316 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318
6319 if (!eap->skip)
6320 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006321 char_u buf[NUMBUFLEN];
6322
6323 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006324 {
6325 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6326 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006327 semsg(_(e_using_invalid_value_as_string_str),
6328 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006329 p = NULL;
6330 }
6331 else
6332 p = tv_get_string_buf(&rettv, buf);
6333 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006334 else
6335 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006336 if (p == NULL)
6337 {
6338 clear_tv(&rettv);
6339 ret = FAIL;
6340 break;
6341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 len = (int)STRLEN(p);
6343 if (ga_grow(&ga, len + 2) == FAIL)
6344 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006345 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 ret = FAIL;
6347 break;
6348 }
6349 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 ga.ga_len += len;
6353 }
6354
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006355 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 arg = skipwhite(arg);
6357 }
6358
6359 if (ret != FAIL && ga.ga_data != NULL)
6360 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006361 // use the first line of continuation lines for messages
6362 SOURCING_LNUM = start_lnum;
6363
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006364 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6365 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006366 // Mark the already saved text as finishing the line, so that what
6367 // follows is displayed on a new line when scrolling back at the
6368 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006369 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006370 }
6371
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006373 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006374 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006375 out_flush();
6376 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006377 else if (eap->cmdidx == CMD_echoconsole)
6378 {
6379 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6380 ui_write((char_u *)"\r\n", 2, TRUE);
6381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 else if (eap->cmdidx == CMD_echoerr)
6383 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006384 int save_did_emsg = did_emsg;
6385
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006386 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006387 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 if (!force_abort)
6389 did_emsg = save_did_emsg;
6390 }
6391 else if (eap->cmdidx == CMD_execute)
6392 do_cmdline((char_u *)ga.ga_data,
6393 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6394 }
6395
6396 ga_clear(&ga);
6397
6398 if (eap->skip)
6399 --emsg_skip;
6400
Bram Moolenaar63b91732021-08-05 20:40:03 +02006401 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402}
6403
6404/*
6405 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6406 * "arg" points to the "&" or '+' when called, to "option" when returning.
6407 * Returns NULL when no option name found. Otherwise pointer to the char
6408 * after the option name.
6409 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006410 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006411find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412{
6413 char_u *p = *arg;
6414
6415 ++p;
6416 if (*p == 'g' && p[1] == ':')
6417 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006418 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 p += 2;
6420 }
6421 else if (*p == 'l' && p[1] == ':')
6422 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006423 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 p += 2;
6425 }
6426 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006427 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428
6429 if (!ASCII_ISALPHA(*p))
6430 return NULL;
6431 *arg = p;
6432
6433 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006434 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 else
6436 while (ASCII_ISALPHA(*p))
6437 ++p;
6438 return p;
6439}
6440
6441/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006442 * Display script name where an item was last set.
6443 * Should only be invoked when 'verbose' is non-zero.
6444 */
6445 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006446last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006447{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006448 char_u *p;
6449
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006450 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006451 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006452 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006453 if (p != NULL)
6454 {
6455 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006456 msg_puts(_("\n\tLast set from "));
6457 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006458 if (script_ctx.sc_lnum > 0)
6459 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006460 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006461 msg_outnum((long)script_ctx.sc_lnum);
6462 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006463 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006464 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006465 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006466 }
6467}
6468
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006469#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470
6471/*
6472 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006473 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 * "flags" can be "g" to do a global substitute.
6475 * Returns an allocated string, NULL for error.
6476 */
6477 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006478do_string_sub(
6479 char_u *str,
6480 char_u *pat,
6481 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006482 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006483 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484{
6485 int sublen;
6486 regmatch_T regmatch;
6487 int i;
6488 int do_all;
6489 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006490 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 garray_T ga;
6492 char_u *ret;
6493 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006494 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006496 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006498 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499
6500 ga_init2(&ga, 1, 200);
6501
6502 do_all = (flags[0] == 'g');
6503
6504 regmatch.rm_ic = p_ic;
6505 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6506 if (regmatch.regprog != NULL)
6507 {
6508 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006509 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6511 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006512 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006513 if (regmatch.startp[0] == regmatch.endp[0])
6514 {
6515 if (zero_width == regmatch.startp[0])
6516 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006517 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006518 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006519 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6520 (size_t)i);
6521 ga.ga_len += i;
6522 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006523 continue;
6524 }
6525 zero_width = regmatch.startp[0];
6526 }
6527
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 /*
6529 * Get some space for a temporary buffer to do the substitution
6530 * into. It will contain:
6531 * - The text up to where the match is.
6532 * - The substituted text.
6533 * - The text after the match.
6534 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006535 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006536 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6538 {
6539 ga_clear(&ga);
6540 break;
6541 }
6542
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006543 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 i = (int)(regmatch.startp[0] - tail);
6545 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006546 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006547 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 + ga.ga_len + i, TRUE, TRUE, FALSE);
6549 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006550 tail = regmatch.endp[0];
6551 if (*tail == NUL)
6552 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 if (!do_all)
6554 break;
6555 }
6556
6557 if (ga.ga_data != NULL)
6558 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6559
Bram Moolenaar473de612013-06-08 18:19:48 +02006560 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 }
6562
6563 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6564 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006565 if (p_cpo == empty_option)
6566 p_cpo = save_cpo;
6567 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006568 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006569 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006570 // If it's still empty it was changed and restored, need to restore in
6571 // the complicated way.
6572 if (*p_cpo == NUL)
6573 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006574 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576
6577 return ret;
6578}