blob: f08ae578f5ba3c49f64f8c3d8c3b05075a40ba64 [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 Moolenaar33570922005-01-25 22:26:29 +000044} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000045
Bram Moolenaar48e697e2016-01-23 22:17:30 +010046static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020047static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
48static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
49static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
50static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
51static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
52static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +020053static int eval7_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000054
Bram Moolenaar48e697e2016-01-23 22:17:30 +010055static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020056static 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 +020057
Bram Moolenaare21c1582019-03-02 11:57:09 +010058/*
59 * Return "n1" divided by "n2", taking care of dividing by zero.
60 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020061 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010062num_divide(varnumber_T n1, varnumber_T n2)
63{
64 varnumber_T result;
65
66 if (n2 == 0) // give an error message?
67 {
68 if (n1 == 0)
69 result = VARNUM_MIN; // similar to NaN
70 else if (n1 < 0)
71 result = -VARNUM_MAX;
72 else
73 result = VARNUM_MAX;
74 }
75 else
76 result = n1 / n2;
77
78 return result;
79}
80
81/*
82 * Return "n1" modulus "n2", taking care of dividing by zero.
83 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020084 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010085num_modulus(varnumber_T n1, varnumber_T n2)
86{
87 // Give an error when n2 is 0?
88 return (n2 == 0) ? 0 : (n1 % n2);
89}
90
Bram Moolenaara1fa8922017-01-12 20:06:33 +010091#if defined(EBCDIC) || defined(PROTO)
92/*
93 * Compare struct fst by function name.
94 */
95 static int
96compare_func_name(const void *s1, const void *s2)
97{
98 struct fst *p1 = (struct fst *)s1;
99 struct fst *p2 = (struct fst *)s2;
100
101 return STRCMP(p1->f_name, p2->f_name);
102}
103
104/*
105 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100106 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100107 * On machines using EBCDIC we have to sort it.
108 */
109 static void
110sortFunctions(void)
111{
112 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
113
114 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
115}
116#endif
117
Bram Moolenaar33570922005-01-25 22:26:29 +0000118/*
119 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000120 */
121 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100122eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000123{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200124 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200125 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000126
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200127#ifdef EBCDIC
128 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100129 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200130 */
131 sortFunctions();
132#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000133}
134
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000135#if defined(EXITFREE) || defined(PROTO)
136 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100137eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000138{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200139 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100140 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200141 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000142
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200143 // autoloaded script names
144 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000145
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200146 // unreferenced lists and dicts
147 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200148
149 // functions not garbage collected
150 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000151}
152#endif
153
Bram Moolenaare6b53242020-07-01 17:28:33 +0200154 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200155fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
156{
157 CLEAR_FIELD(*evalarg);
158 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
159 if (eap != NULL && getline_equal(eap->getline, eap->cookie, getsourceline))
160 {
161 evalarg->eval_getline = eap->getline;
162 evalarg->eval_cookie = eap->cookie;
163 }
164}
165
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166/*
167 * Top level evaluation function, returning a boolean.
168 * Sets "error" to TRUE if there was an error.
169 * Return TRUE or FALSE.
170 */
171 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100172eval_to_bool(
173 char_u *arg,
174 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200175 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100176 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177{
Bram Moolenaar33570922005-01-25 22:26:29 +0000178 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200179 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200180 evalarg_T evalarg;
181
Bram Moolenaar37c83712020-06-30 21:18:36 +0200182 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183
184 if (skip)
185 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200186 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 else
189 {
190 *error = FALSE;
191 if (!skip)
192 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200193 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200194 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200195 else
196 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000197 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 }
199 }
200 if (skip)
201 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200202 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200204 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205}
206
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100207/*
208 * Call eval1() and give an error message if not done at a lower level.
209 */
210 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200211eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100212{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100213 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100214 int ret;
215 int did_emsg_before = did_emsg;
216 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200217 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100218
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200219 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
220
221 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100222 if (ret == FAIL)
223 {
224 // Report the invalid expression unless the expression evaluation has
225 // been cancelled due to an aborting error, an interrupt, or an
226 // exception, or we already gave a more specific error.
227 // Also check called_emsg for when using assert_fails().
228 if (!aborting() && did_emsg == did_emsg_before
229 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100230 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100231 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200232 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100233 return ret;
234}
235
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100236/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200237 * Return whether a typval is a valid expression to pass to eval_expr_typval()
238 * or eval_expr_to_bool(). An empty string returns FALSE;
239 */
240 int
241eval_expr_valid_arg(typval_T *tv)
242{
243 return tv->v_type != VAR_UNKNOWN
244 && (tv->v_type != VAR_STRING
245 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
246}
247
248/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100249 * Evaluate an expression, which can be a function, partial or string.
250 * Pass arguments "argv[argc]".
251 * Return the result in "rettv" and OK or FAIL.
252 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200253 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100254eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
255{
256 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100257 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200258 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100259
260 if (expr->v_type == VAR_FUNC)
261 {
262 s = expr->vval.v_string;
263 if (s == NULL || *s == NUL)
264 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200265 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200266 funcexe.evaluate = TRUE;
267 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100268 return FAIL;
269 }
270 else if (expr->v_type == VAR_PARTIAL)
271 {
272 partial_T *partial = expr->vval.v_partial;
273
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200274 if (partial == NULL)
275 return FAIL;
276
Bram Moolenaar822ba242020-05-24 23:00:18 +0200277 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200278 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100279 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200280 if (call_def_function(partial->pt_func, argc, argv,
281 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100282 return FAIL;
283 }
284 else
285 {
286 s = partial_name(partial);
287 if (s == NULL || *s == NUL)
288 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200289 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100290 funcexe.evaluate = TRUE;
291 funcexe.partial = partial;
292 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
293 return FAIL;
294 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100295 }
296 else
297 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100298 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100299 if (s == NULL)
300 return FAIL;
301 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200302 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100303 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200304 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100305 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200306 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100307 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100308 return FAIL;
309 }
310 }
311 return OK;
312}
313
314/*
315 * Like eval_to_bool() but using a typval_T instead of a string.
316 * Works for string, funcref and partial.
317 */
318 int
319eval_expr_to_bool(typval_T *expr, int *error)
320{
321 typval_T rettv;
322 int res;
323
324 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
325 {
326 *error = TRUE;
327 return FALSE;
328 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200329 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100330 clear_tv(&rettv);
331 return res;
332}
333
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334/*
335 * Top level evaluation function, returning a string. If "skip" is TRUE,
336 * only parsing to "nextcmd" is done, without reporting errors. Return
337 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
338 */
339 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100340eval_to_string_skip(
341 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200342 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100343 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344{
Bram Moolenaar33570922005-01-25 22:26:29 +0000345 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200347 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348
Bram Moolenaar37c83712020-06-30 21:18:36 +0200349 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 if (skip)
351 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200352 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 retval = NULL;
354 else
355 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100356 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000357 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 }
359 if (skip)
360 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200361 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362
363 return retval;
364}
365
366/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000367 * Skip over an expression at "*pp".
368 * Return FAIL for an error, OK otherwise.
369 */
370 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200371skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000372{
Bram Moolenaar33570922005-01-25 22:26:29 +0000373 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000374
375 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200376 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000377}
378
379/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200380 * Skip over an expression at "*pp".
381 * If in Vim9 script and line breaks are encountered, the lines are
382 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200383 * "arg" is advanced to just after the expression.
384 * "start" is set to the start of the expression, "end" to just after the end.
385 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200386 * Return FAIL for an error, OK otherwise.
387 */
388 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200389skip_expr_concatenate(
390 char_u **arg,
391 char_u **start,
392 char_u **end,
393 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200394{
395 typval_T rettv;
396 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200397 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200398 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200399 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200400 int evaluate = evalarg == NULL
401 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200402
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200403 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200404 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200405 {
406 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200407 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200408 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200409 ++gap->ga_len;
410 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200411 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200412
413 // Don't evaluate the expression.
414 if (evalarg != NULL)
415 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200416 *arg = skipwhite(*arg);
417 res = eval1(arg, &rettv, evalarg);
418 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200419 if (evalarg != NULL)
420 evalarg->eval_flags = save_flags;
421
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200422 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200423 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200424 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200425 if (evalarg->eval_ga.ga_len == 1)
426 {
427 // just one line, no need to concatenate
428 ga_clear(gap);
429 gap->ga_itemsize = 0;
430 }
431 else
432 {
433 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200434 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200435
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200436 // Line breaks encountered, concatenate all the lines.
437 *((char_u **)gap->ga_data) = *start;
438 p = ga_concat_strings(gap, "");
439
440 // free the lines only when using getsourceline()
441 if (evalarg->eval_cookie != NULL)
442 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200443 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200444 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200445 // Do not free the last line, "arg" points into it, free it
446 // later.
447 vim_free(evalarg->eval_tofree);
448 evalarg->eval_tofree =
449 ((char_u **)gap->ga_data)[gap->ga_len - 1];
450 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200451 ga_clear_strings(gap);
452 }
453 else
454 ga_clear(gap);
455 gap->ga_itemsize = 0;
456 if (p == NULL)
457 return FAIL;
458 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200459 vim_free(evalarg->eval_tofree_lambda);
460 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200461 // Compute "end" relative to the end.
462 *end = *start + STRLEN(*start) - endoff;
463 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200464 }
465
466 return res;
467}
468
469/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200470 * Top level evaluation function, returning a string. Does not handle line
471 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000472 * When "convert" is TRUE convert a List into a sequence of lines and convert
473 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 * Return pointer to allocated memory, or NULL for failure.
475 */
476 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100477eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100478 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100479 int convert,
480 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481{
Bram Moolenaar33570922005-01-25 22:26:29 +0000482 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000484 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000485#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000486 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000487#endif
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100488 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100490 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
491 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 retval = NULL;
493 else
494 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000495 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000496 {
497 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000498 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200499 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200500 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200501 if (tv.vval.v_list->lv_len > 0)
502 ga_append(&ga, NL);
503 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000504 ga_append(&ga, NUL);
505 retval = (char_u *)ga.ga_data;
506 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000507#ifdef FEAT_FLOAT
508 else if (convert && tv.v_type == VAR_FLOAT)
509 {
510 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
511 retval = vim_strsave(numbuf);
512 }
513#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000514 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100515 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000516 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100518 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519
520 return retval;
521}
522
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100523 char_u *
524eval_to_string(
525 char_u *arg,
526 int convert)
527{
528 return eval_to_string_eap(arg, convert, NULL);
529}
530
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000532 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200533 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200534 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 */
536 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100537eval_to_string_safe(
538 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100539 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540{
541 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200542 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200543 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200545 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200546 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000547 if (use_sandbox)
548 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200549 ++textwinlock;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200550 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000551 if (use_sandbox)
552 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200553 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200554 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200555 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 return retval;
557}
558
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559/*
560 * Top level evaluation function, returning a number.
561 * Evaluates "expr" silently.
562 * Returns -1 for an error.
563 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200564 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100565eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566{
Bram Moolenaar33570922005-01-25 22:26:29 +0000567 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200568 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000569 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570
571 ++emsg_off;
572
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200573 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 retval = -1;
575 else
576 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100577 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000578 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 }
580 --emsg_off;
581
582 return retval;
583}
584
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000585/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000586 * Top level evaluation function.
587 * Returns an allocated typval_T with the result.
588 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000589 */
590 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200591eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000592{
593 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200594 evalarg_T evalarg;
595
596 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000597
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200598 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200599 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100600 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000601
Bram Moolenaar37c83712020-06-30 21:18:36 +0200602 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000603 return tv;
604}
605
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100607 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200608 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
609 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000610 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200612 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100613call_vim_function(
614 char_u *func,
615 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200616 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200617 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000619 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200620 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100622 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200623 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200624 funcexe.firstline = curwin->w_cursor.lnum;
625 funcexe.lastline = curwin->w_cursor.lnum;
626 funcexe.evaluate = TRUE;
627 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000628 if (ret == FAIL)
629 clear_tv(rettv);
630
631 return ret;
632}
633
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100634/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100635 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100636 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200637 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
638 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100639 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200640 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100641call_func_retnr(
642 char_u *func,
643 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200644 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100645{
646 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200647 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100648
Bram Moolenaarded27a12018-08-01 19:06:03 +0200649 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100650 return -1;
651
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100652 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100653 clear_tv(&rettv);
654 return retval;
655}
656
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000657/*
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100658 * Call Vim script function like call_func_retnr() and drop the result.
659 * Returns FAIL when calling the function fails.
660 */
661 int
662call_func_noret(
663 char_u *func,
664 int argc,
665 typval_T *argv)
666{
667 typval_T rettv;
668
669 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
670 return FAIL;
671 clear_tv(&rettv);
672 return OK;
673}
674
675/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100676 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100677 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000678 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000679 */
680 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100681call_func_retstr(
682 char_u *func,
683 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200684 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000685{
686 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000687 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000688
Bram Moolenaarded27a12018-08-01 19:06:03 +0200689 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000690 return NULL;
691
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100692 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000693 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 return retval;
695}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000696
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000697/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100698 * Call Vim script function "func" and return the result as a List.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100699 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000700 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000701 */
702 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100703call_func_retlist(
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;
709
Bram Moolenaarded27a12018-08-01 19:06:03 +0200710 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000711 return NULL;
712
713 if (rettv.v_type != VAR_LIST)
714 {
715 clear_tv(&rettv);
716 return NULL;
717 }
718
719 return rettv.vval.v_list;
720}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722#ifdef FEAT_FOLDING
723/*
724 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
725 * it in "*cp". Doesn't give error messages.
726 */
727 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100728eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729{
Bram Moolenaar33570922005-01-25 22:26:29 +0000730 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200731 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000733 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
734 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735
736 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000737 if (use_sandbox)
738 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200739 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200741 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 retval = 0;
743 else
744 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100745 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000746 if (tv.v_type == VAR_NUMBER)
747 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000748 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 retval = 0;
750 else
751 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100752 // If the result is a string, check if there is a non-digit before
753 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000754 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 if (!VIM_ISDIGIT(*s) && *s != '-')
756 *cp = *s++;
757 retval = atol((char *)s);
758 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000759 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 }
761 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000762 if (use_sandbox)
763 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200764 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200765 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200767 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768}
769#endif
770
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000772 * Get an lval: variable, Dict item or List item that can be assigned a value
773 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
774 * "name.key", "name.key[expr]" etc.
775 * Indexing only works if "name" is an existing List or Dictionary.
776 * "name" points to the start of the name.
777 * If "rettv" is not NULL it points to the value to be assigned.
778 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
779 * wrong; must end in space or cmd separator.
780 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100781 * flags:
782 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100783 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100784 * GLV_NO_AUTOLOAD: do not use script autoloading
785 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000786 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000787 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000788 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000789 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200790 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100791get_lval(
792 char_u *name,
793 typval_T *rettv,
794 lval_T *lp,
795 int unlet,
796 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100797 int flags, // GLV_ values
798 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000799{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000800 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000801 char_u *expr_start, *expr_end;
802 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000803 dictitem_T *v;
804 typval_T var1;
805 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000806 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000807 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000808 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000809 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100810 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100811 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000812
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100813 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200814 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000815
816 if (skip)
817 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100818 // When skipping just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000819 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200820 lp->ll_name_end = find_name_end(name, NULL, NULL,
821 FNE_INCL_BR | fne_flags);
822 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000823 }
824
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100825 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000826 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100827 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000828 if (expr_start != NULL)
829 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100830 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100831 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000832 && *p != '[' && *p != '.')
833 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200834 semsg(_(e_trailing_arg), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000835 return NULL;
836 }
837
838 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
839 if (lp->ll_exp_name == NULL)
840 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100841 // Report an invalid expression in braces, unless the
842 // expression evaluation has been cancelled due to an
843 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000844 if (!aborting() && !quiet)
845 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000846 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100847 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000848 return NULL;
849 }
850 }
851 lp->ll_name = lp->ll_exp_name;
852 }
853 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100854 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000855 lp->ll_name = name;
856
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200857 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100858 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200859 // "a: type" is declaring variable "a" with a type, not "a:".
860 if (p == name + 2 && p[-1] == ':')
861 {
862 --p;
863 lp->ll_name_end = p;
864 }
865 if (*p == ':')
866 {
867 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
868 char_u *tp = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100869
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200870 // parse the type after the name
871 lp->ll_type = parse_type(&tp, &si->sn_type_list);
872 lp->ll_name_end = tp;
873 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100874 }
875 }
876
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100877 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000878 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
879 return p;
880
881 cc = *p;
882 *p = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100883 // Only pass &ht when we would write to the variable, it prevents autoload
884 // as well.
Bram Moolenaar6e65d592017-12-07 22:11:27 +0100885 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
886 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000887 if (v == NULL && !quiet)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200888 semsg(_(e_undefined_variable_str), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000889 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000890 if (v == NULL)
891 return NULL;
892
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +0100893 if (in_vim9script() && (flags & GLV_NO_DECL) == 0)
894 {
895 if (!quiet)
896 semsg(_(e_variable_already_declared), lp->ll_name);
897 return NULL;
898 }
899
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000900 /*
901 * Loop until no more [idx] or .key is following.
902 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000903 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100904 var1.v_type = VAR_UNKNOWN;
905 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000906 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000907 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000908 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
Bram Moolenaar81ed4962020-09-23 15:56:50 +0200909 && !(lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100910 && !(lp->ll_tv->v_type == VAR_BLOB
911 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000912 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000913 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100914 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000915 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000916 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000917 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000918 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000919 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100920 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000921 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000922 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000923
Bram Moolenaar10c65862020-10-08 21:16:42 +0200924 if (in_vim9script() && lp->ll_valtype == NULL
925 && lp->ll_tv == &v->di_tv
926 && ht != NULL && ht == get_script_local_ht())
927 {
928 svar_T *sv = find_typval_in_script(lp->ll_tv);
929
930 // Vim9 script local variable: get the type
931 if (sv != NULL)
932 lp->ll_valtype = sv->sv_type;
933 }
934
Bram Moolenaar8c711452005-01-14 21:53:12 +0000935 len = -1;
936 if (*p == '.')
937 {
938 key = p + 1;
939 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
940 ;
941 if (len == 0)
942 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000943 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100944 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000945 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000946 }
947 p = key + len;
948 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000949 else
950 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100951 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000952 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000953 if (*p == ':')
954 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000955 else
956 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000957 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200958 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000959 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100960 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000961 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100962 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000963 clear_tv(&var1);
964 return NULL;
965 }
Bram Moolenaar6a250262020-08-04 15:53:01 +0200966 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000967 }
968
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100969 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000970 if (*p == ':')
971 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000972 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000973 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000974 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +0200975 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100976 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000977 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000978 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100979 if (rettv != NULL
980 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100981 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100982 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100983 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +0000984 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000985 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100986 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100987 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000988 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000989 }
990 p = skipwhite(p + 1);
991 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000992 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000993 else
994 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000995 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200996 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200997 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000998 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100999 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001000 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001001 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001002 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001003 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001004 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001005 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001006 clear_tv(&var2);
1007 return NULL;
1008 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001009 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001010 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001011 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001012 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001013 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001014
Bram Moolenaar8c711452005-01-14 21:53:12 +00001015 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001016 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001017 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001018 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001019 clear_tv(&var1);
1020 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001021 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001022 }
1023
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001024 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001025 ++p;
1026 }
1027
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001028 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001029 {
1030 if (len == -1)
1031 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001032 // "[key]": get key from "var1"
1033 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001034 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001035 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001036 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001037 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001038 }
1039 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001040 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001041
1042 // a NULL dict is equivalent with an empty dict
1043 if (lp->ll_tv->vval.v_dict == NULL)
1044 {
1045 lp->ll_tv->vval.v_dict = dict_alloc();
1046 if (lp->ll_tv->vval.v_dict == NULL)
1047 {
1048 clear_tv(&var1);
1049 return NULL;
1050 }
1051 ++lp->ll_tv->vval.v_dict->dv_refcount;
1052 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001053 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001054
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001055 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001056
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001057 // When assigning to a scope dictionary check that a function and
1058 // variable name is valid (only variable name unless it is l: or
1059 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001060 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001061 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001062 int prevval;
1063 int wrong;
1064
1065 if (len != -1)
1066 {
1067 prevval = key[len];
1068 key[len] = NUL;
1069 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001070 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001071 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001072 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1073 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001074 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar03290b82020-12-19 16:30:44 +01001075 || !valid_varname(key, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001076 if (len != -1)
1077 key[len] = prevval;
1078 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001079 {
1080 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001081 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001082 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001083 }
1084
Bram Moolenaar10c65862020-10-08 21:16:42 +02001085 if (lp->ll_valtype != NULL)
1086 // use the type of the member
1087 lp->ll_valtype = lp->ll_valtype->tt_member;
1088
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001089 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001090 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001091 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001092 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001093 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001094 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001095 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001096 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001097 return NULL;
1098 }
1099
Bram Moolenaar31b81602019-02-10 22:14:27 +01001100 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001101 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001102 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001103 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001104 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001105 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001106 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001107 }
1108 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001109 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001110 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001111 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001112 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001113 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001114 p = NULL;
1115 break;
1116 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001117 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001118 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001119 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1120 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001121 {
1122 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001123 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001124 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001125
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001126 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001127 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001128 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001129 else if (lp->ll_tv->v_type == VAR_BLOB)
1130 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001131 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1132
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001133 /*
1134 * Get the number and item for the only or first index of the List.
1135 */
1136 if (empty1)
1137 lp->ll_n1 = 0;
1138 else
1139 // is number or string
1140 lp->ll_n1 = (long)tv_get_number(&var1);
1141 clear_tv(&var1);
1142
1143 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001144 || lp->ll_n1 > bloblen
1145 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001146 {
1147 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001148 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001149 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001150 return NULL;
1151 }
1152 if (lp->ll_range && !lp->ll_empty2)
1153 {
1154 lp->ll_n2 = (long)tv_get_number(&var2);
1155 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001156 if (lp->ll_n2 < 0
1157 || lp->ll_n2 >= bloblen
1158 || lp->ll_n2 < lp->ll_n1)
1159 {
1160 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001161 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001162 return NULL;
1163 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001164 }
1165 lp->ll_blob = lp->ll_tv->vval.v_blob;
1166 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001167 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001168 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001169 else
1170 {
1171 /*
1172 * Get the number and item for the only or first index of the List.
1173 */
1174 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001175 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001176 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001177 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001178 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001179 clear_tv(&var1);
1180
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001181 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001182 lp->ll_list = lp->ll_tv->vval.v_list;
1183 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1184 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001185 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001186 if (lp->ll_n1 < 0)
1187 {
1188 lp->ll_n1 = 0;
1189 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1190 }
1191 }
1192 if (lp->ll_li == NULL)
1193 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001194 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001195 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001196 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001197 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001198 }
1199
Bram Moolenaar10c65862020-10-08 21:16:42 +02001200 if (lp->ll_valtype != NULL)
1201 // use the type of the member
1202 lp->ll_valtype = lp->ll_valtype->tt_member;
1203
Bram Moolenaar8c711452005-01-14 21:53:12 +00001204 /*
1205 * May need to find the item or absolute index for the second
1206 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001207 * When no index given: "lp->ll_empty2" is TRUE.
1208 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001209 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001210 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001211 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001212 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001213 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001214 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001215 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001216 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001217 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001218 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001219 {
1220 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001221 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001222 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001223 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001224 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001225 }
1226
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001227 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001228 if (lp->ll_n1 < 0)
1229 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1230 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001231 {
1232 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001233 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001234 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001235 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001236 }
1237
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001238 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001239 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001240 }
1241
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001242 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001243 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001244 return p;
1245}
1246
1247/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001248 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001249 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001250 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001251clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001252{
1253 vim_free(lp->ll_exp_name);
1254 vim_free(lp->ll_newkey);
1255}
1256
1257/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001258 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001259 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001260 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1261 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001262 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001263 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001264set_var_lval(
1265 lval_T *lp,
1266 char_u *endp,
1267 typval_T *rettv,
1268 int copy,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001269 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar7454a062016-01-30 15:14:10 +01001270 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001271{
1272 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001273 listitem_T *ri;
1274 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001275
1276 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001277 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001278 cc = *endp;
1279 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001280 if (lp->ll_blob != NULL)
1281 {
1282 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001283
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001284 if (op != NULL && *op != '=')
1285 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001286 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001287 return;
1288 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001289 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001290 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001291
1292 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1293 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001294 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001295
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001296 if (lp->ll_empty2)
1297 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1298
1299 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001300 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001301 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001302 return;
1303 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001304 if (lp->ll_empty2)
1305 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001306
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001307 ir = 0;
1308 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1309 blob_set(lp->ll_blob, il,
1310 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001311 }
1312 else
1313 {
1314 val = (int)tv_get_number_chk(rettv, &error);
1315 if (!error)
1316 {
1317 garray_T *gap = &lp->ll_blob->bv_ga;
1318
1319 // Allow for appending a byte. Setting a byte beyond
1320 // the end is an error otherwise.
1321 if (lp->ll_n1 < gap->ga_len
1322 || (lp->ll_n1 == gap->ga_len
1323 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1324 {
1325 blob_set(lp->ll_blob, lp->ll_n1, val);
1326 if (lp->ll_n1 == gap->ga_len)
1327 ++gap->ga_len;
1328 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001329 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001330 }
1331 }
1332 }
1333 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001334 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001335 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001336
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001337 if (flags & ASSIGN_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001338 {
1339 emsg(_(e_cannot_mod));
1340 *endp = cc;
1341 return;
1342 }
1343
Bram Moolenaarff697e62019-02-12 22:28:33 +01001344 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001345 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001346 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar79518e22017-02-17 16:31:35 +01001347 &tv, &di, TRUE, FALSE) == OK)
1348 {
1349 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001350 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1351 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001352 && tv_op(&tv, rettv, op) == OK)
1353 set_var(lp->ll_name, &tv, FALSE);
1354 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001355 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001356 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001357 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001358 {
1359 if (lp->ll_type != NULL
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001360 && check_typval_type(lp->ll_type, rettv, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001361 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001362 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001363 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001364 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001365 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001366 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001367 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001368 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001369 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001370 else if (lp->ll_range)
1371 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001372 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001373 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001374
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001375 if (flags & ASSIGN_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001376 {
1377 emsg(_("E996: Cannot lock a range"));
1378 return;
1379 }
1380
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001381 /*
1382 * Check whether any of the list items is locked
1383 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001384 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001385 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001386 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001387 return;
1388 ri = ri->li_next;
1389 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1390 break;
1391 ll_li = ll_li->li_next;
1392 ++ll_n1;
1393 }
1394
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001395 /*
1396 * Assign the List values to the list items.
1397 */
1398 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001399 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001400 if (op != NULL && *op != '=')
1401 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1402 else
1403 {
1404 clear_tv(&lp->ll_li->li_tv);
1405 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1406 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001407 ri = ri->li_next;
1408 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1409 break;
1410 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001411 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001412 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001413 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001414 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001415 ri = NULL;
1416 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001417 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001418 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001419 lp->ll_li = lp->ll_li->li_next;
1420 ++lp->ll_n1;
1421 }
1422 if (ri != NULL)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001423 emsg(_(e_list_value_has_more_items_than_targets));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001424 else if (lp->ll_empty2
1425 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001426 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001427 emsg(_(e_list_value_does_not_have_enough_items));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001428 }
1429 else
1430 {
1431 /*
1432 * Assign to a List or Dictionary item.
1433 */
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001434 if (flags & ASSIGN_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001435 {
1436 emsg(_("E996: Cannot lock a list or dict"));
1437 return;
1438 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001439
1440 if (lp->ll_valtype != NULL
1441 && check_typval_type(lp->ll_valtype, rettv, 0) == FAIL)
1442 return;
1443
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001444 if (lp->ll_newkey != NULL)
1445 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001446 if (op != NULL && *op != '=')
1447 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001448 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001449 return;
1450 }
1451
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001452 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001453 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001454 if (di == NULL)
1455 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001456 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1457 {
1458 vim_free(di);
1459 return;
1460 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001461 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001462 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001463 else if (op != NULL && *op != '=')
1464 {
1465 tv_op(lp->ll_tv, rettv, op);
1466 return;
1467 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001468 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001469 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001470
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001471 /*
1472 * Assign the value to the variable or list item.
1473 */
1474 if (copy)
1475 copy_tv(rettv, lp->ll_tv);
1476 else
1477 {
1478 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001479 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001480 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001481 }
1482 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001483}
1484
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001485/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001486 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1487 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001488 * Returns OK or FAIL.
1489 */
1490 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001491tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001492{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001493 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001494 char_u numbuf[NUMBUFLEN];
1495 char_u *s;
1496
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001497 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001498 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001499 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001500 {
1501 switch (tv1->v_type)
1502 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001503 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001504 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001505 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001506 case VAR_DICT:
1507 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001508 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001509 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001510 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001511 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001512 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001513 break;
1514
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001515 case VAR_BLOB:
1516 if (*op != '+' || tv2->v_type != VAR_BLOB)
1517 break;
1518 // BLOB += BLOB
1519 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1520 {
1521 blob_T *b1 = tv1->vval.v_blob;
1522 blob_T *b2 = tv2->vval.v_blob;
1523 int i, len = blob_len(b2);
1524 for (i = 0; i < len; i++)
1525 ga_append(&b1->bv_ga, blob_get(b2, i));
1526 }
1527 return OK;
1528
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001529 case VAR_LIST:
1530 if (*op != '+' || tv2->v_type != VAR_LIST)
1531 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001532 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001533 if (tv2->vval.v_list != NULL)
1534 {
1535 if (tv1->vval.v_list == NULL)
1536 {
1537 tv1->vval.v_list = tv2->vval.v_list;
1538 ++tv1->vval.v_list->lv_refcount;
1539 }
1540 else
1541 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1542 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001543 return OK;
1544
1545 case VAR_NUMBER:
1546 case VAR_STRING:
1547 if (tv2->v_type == VAR_LIST)
1548 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001549 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001550 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001551 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001552 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001553#ifdef FEAT_FLOAT
1554 if (tv2->v_type == VAR_FLOAT)
1555 {
1556 float_T f = n;
1557
Bram Moolenaarff697e62019-02-12 22:28:33 +01001558 if (*op == '%')
1559 break;
1560 switch (*op)
1561 {
1562 case '+': f += tv2->vval.v_float; break;
1563 case '-': f -= tv2->vval.v_float; break;
1564 case '*': f *= tv2->vval.v_float; break;
1565 case '/': f /= tv2->vval.v_float; break;
1566 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001567 clear_tv(tv1);
1568 tv1->v_type = VAR_FLOAT;
1569 tv1->vval.v_float = f;
1570 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001571 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001572#endif
1573 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001574 switch (*op)
1575 {
1576 case '+': n += tv_get_number(tv2); break;
1577 case '-': n -= tv_get_number(tv2); break;
1578 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001579 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1580 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001581 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001582 clear_tv(tv1);
1583 tv1->v_type = VAR_NUMBER;
1584 tv1->vval.v_number = n;
1585 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001586 }
1587 else
1588 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001589 if (tv2->v_type == VAR_FLOAT)
1590 break;
1591
Bram Moolenaarff697e62019-02-12 22:28:33 +01001592 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001593 s = tv_get_string(tv1);
1594 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001595 clear_tv(tv1);
1596 tv1->v_type = VAR_STRING;
1597 tv1->vval.v_string = s;
1598 }
1599 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001600
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001601 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001602#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001603 {
1604 float_T f;
1605
Bram Moolenaarff697e62019-02-12 22:28:33 +01001606 if (*op == '%' || *op == '.'
1607 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001608 && tv2->v_type != VAR_NUMBER
1609 && tv2->v_type != VAR_STRING))
1610 break;
1611 if (tv2->v_type == VAR_FLOAT)
1612 f = tv2->vval.v_float;
1613 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001614 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001615 switch (*op)
1616 {
1617 case '+': tv1->vval.v_float += f; break;
1618 case '-': tv1->vval.v_float -= f; break;
1619 case '*': tv1->vval.v_float *= f; break;
1620 case '/': tv1->vval.v_float /= f; break;
1621 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001622 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001623#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001624 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001625 }
1626 }
1627
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001628 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001629 return FAIL;
1630}
1631
1632/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001633 * Evaluate the expression used in a ":for var in expr" command.
1634 * "arg" points to "var".
1635 * Set "*errp" to TRUE for an error, FALSE otherwise;
1636 * Return a pointer that holds the info. Null when there is an error.
1637 */
1638 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001639eval_for_line(
1640 char_u *arg,
1641 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001642 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001643 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001644{
Bram Moolenaar33570922005-01-25 22:26:29 +00001645 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001646 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001647 typval_T tv;
1648 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001649 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001650
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001651 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001652
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001653 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001654 if (fi == NULL)
1655 return NULL;
1656
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001657 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001658 if (expr == NULL)
1659 return fi;
1660
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001661 expr = skipwhite_and_linebreak(expr, evalarg);
1662 if (expr[0] != 'i' || expr[1] != 'n'
1663 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001664 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001665 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001666 return fi;
1667 }
1668
1669 if (skip)
1670 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001671 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1672 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001673 {
1674 *errp = FALSE;
1675 if (!skip)
1676 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001677 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001678 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001679 l = tv.vval.v_list;
1680 if (l == NULL)
1681 {
1682 // a null list is like an empty list: do nothing
1683 clear_tv(&tv);
1684 }
1685 else
1686 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001687 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001688 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001689
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001690 // No need to increment the refcount, it's already set for
1691 // the list being used in "tv".
1692 fi->fi_list = l;
1693 list_add_watch(l, &fi->fi_lw);
1694 fi->fi_lw.lw_item = l->lv_first;
1695 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001696 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001697 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001698 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001699 fi->fi_bi = 0;
1700 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001701 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001702 typval_T btv;
1703
1704 // Make a copy, so that the iteration still works when the
1705 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001706 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001707 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001708 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001709 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001710 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001711 else
1712 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001713 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001714 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001715 }
1716 }
1717 }
1718 if (skip)
1719 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001720 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001721
1722 return fi;
1723}
1724
1725/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001726 * Used when looping over a :for line, skip the "in expr" part.
1727 */
1728 void
1729skip_for_lines(void *fi_void, evalarg_T *evalarg)
1730{
1731 forinfo_T *fi = (forinfo_T *)fi_void;
1732 int i;
1733
1734 for (i = 0; i < fi->fi_break_count; ++i)
1735 eval_next_line(evalarg);
1736}
1737
1738/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001739 * Use the first item in a ":for" list. Advance to the next.
1740 * Assign the values to the variable (list). "arg" points to the first one.
1741 * Return TRUE when a valid item was found, FALSE when at end of list or
1742 * something wrong.
1743 */
1744 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001745next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001746{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001747 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001748 int result;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001749 int flag = in_vim9script() ? ASSIGN_NO_DECL : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001750 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001751
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001752 if (fi->fi_blob != NULL)
1753 {
1754 typval_T tv;
1755
1756 if (fi->fi_bi >= blob_len(fi->fi_blob))
1757 return FALSE;
1758 tv.v_type = VAR_NUMBER;
1759 tv.v_lock = VAR_FIXED;
1760 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1761 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001762 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001763 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001764 }
1765
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001766 item = fi->fi_lw.lw_item;
1767 if (item == NULL)
1768 result = FALSE;
1769 else
1770 {
1771 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001772 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001773 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001774 }
1775 return result;
1776}
1777
1778/*
1779 * Free the structure used to store info used by ":for".
1780 */
1781 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001782free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001783{
Bram Moolenaar33570922005-01-25 22:26:29 +00001784 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001785
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001786 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001787 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001788 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001789 list_unref(fi->fi_list);
1790 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001791 if (fi != NULL && fi->fi_blob != NULL)
1792 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001793 vim_free(fi);
1794}
1795
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001797set_context_for_expression(
1798 expand_T *xp,
1799 char_u *arg,
1800 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001802 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001804 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001806 if (cmdidx == CMD_let || cmdidx == CMD_var
1807 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001808 {
1809 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001810 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001811 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001812 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001813 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001814 {
1815 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001816 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001817 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001818 break;
1819 }
1820 return;
1821 }
1822 }
1823 else
1824 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1825 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 while ((xp->xp_pattern = vim_strpbrk(arg,
1827 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1828 {
1829 c = *xp->xp_pattern;
1830 if (c == '&')
1831 {
1832 c = xp->xp_pattern[1];
1833 if (c == '&')
1834 {
1835 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001836 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 }
1838 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001839 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001841 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1842 xp->xp_pattern += 2;
1843
1844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 }
1846 else if (c == '$')
1847 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001848 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 xp->xp_context = EXPAND_ENV_VARS;
1850 }
1851 else if (c == '=')
1852 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001853 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 xp->xp_context = EXPAND_EXPRESSION;
1855 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001856 else if (c == '#'
1857 && xp->xp_context == EXPAND_EXPRESSION)
1858 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001859 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001860 break;
1861 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001862 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 && xp->xp_context == EXPAND_FUNCTIONS
1864 && vim_strchr(xp->xp_pattern, '(') == NULL)
1865 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001866 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 break;
1868 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001869 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001871 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 {
1873 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1874 if (c == '\\' && xp->xp_pattern[1] != NUL)
1875 ++xp->xp_pattern;
1876 xp->xp_context = EXPAND_NOTHING;
1877 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001878 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001880 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1882 /* skip */ ;
1883 xp->xp_context = EXPAND_NOTHING;
1884 }
1885 else if (c == '|')
1886 {
1887 if (xp->xp_pattern[1] == '|')
1888 {
1889 ++xp->xp_pattern;
1890 xp->xp_context = EXPAND_EXPRESSION;
1891 }
1892 else
1893 xp->xp_context = EXPAND_COMMANDS;
1894 }
1895 else
1896 xp->xp_context = EXPAND_EXPRESSION;
1897 }
1898 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001899 // Doesn't look like something valid, expand as an expression
1900 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001901 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 arg = xp->xp_pattern;
1903 if (*arg != NUL)
1904 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1905 /* skip */ ;
1906 }
1907 xp->xp_pattern = arg;
1908}
1909
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001911 * Return TRUE if "pat" matches "text".
1912 * Does not use 'cpo' and always uses 'magic'.
1913 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001914 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001915pattern_match(char_u *pat, char_u *text, int ic)
1916{
1917 int matches = FALSE;
1918 char_u *save_cpo;
1919 regmatch_T regmatch;
1920
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001921 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001922 save_cpo = p_cpo;
1923 p_cpo = (char_u *)"";
1924 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1925 if (regmatch.regprog != NULL)
1926 {
1927 regmatch.rm_ic = ic;
1928 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1929 vim_regfree(regmatch.regprog);
1930 }
1931 p_cpo = save_cpo;
1932 return matches;
1933}
1934
1935/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001936 * Handle a name followed by "(". Both for just "name(arg)" and for
1937 * "expr->name(arg)".
1938 * Returns OK or FAIL.
1939 */
1940 static int
1941eval_func(
1942 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02001943 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001944 char_u *name,
1945 int name_len,
1946 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001947 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001948 typval_T *basetv) // "expr" for "expr->name(arg)"
1949{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001950 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001951 char_u *s = name;
1952 int len = name_len;
1953 partial_T *partial;
1954 int ret = OK;
1955
1956 if (!evaluate)
1957 check_vars(s, len);
1958
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001959 // If "s" is the name of a variable of type VAR_FUNC
1960 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001961 s = deref_func_name(s, &len, &partial, !evaluate);
1962
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001963 // Need to make a copy, in case evaluating the arguments makes
1964 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001965 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001966 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001967 ret = FAIL;
1968 else
1969 {
1970 funcexe_T funcexe;
1971
1972 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001973 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001974 funcexe.firstline = curwin->w_cursor.lnum;
1975 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001976 funcexe.evaluate = evaluate;
1977 funcexe.partial = partial;
1978 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02001979 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001980 }
1981 vim_free(s);
1982
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001983 // If evaluate is FALSE rettv->v_type was not set in
1984 // get_func_tv, but it's needed in handle_subscript() to parse
1985 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001986 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
1987 {
1988 rettv->vval.v_string = NULL;
1989 rettv->v_type = VAR_FUNC;
1990 }
1991
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001992 // Stop the expression evaluation when immediately
1993 // aborting on error, or when an interrupt occurred or
1994 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001995 if (evaluate && aborting())
1996 {
1997 if (ret == OK)
1998 clear_tv(rettv);
1999 ret = FAIL;
2000 }
2001 return ret;
2002}
2003
2004/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002005 * Get the next line source line without advancing. But do skip over comment
2006 * lines.
2007 */
2008 static char_u *
2009getline_peek_skip_comments(evalarg_T *evalarg)
2010{
2011 for (;;)
2012 {
2013 char_u *next = getline_peek(evalarg->eval_getline,
2014 evalarg->eval_cookie);
2015 char_u *p;
2016
2017 if (next == NULL)
2018 break;
2019 p = skipwhite(next);
2020 if (*p != NUL && !vim9_comment_start(p))
2021 return next;
2022 (void)eval_next_line(evalarg);
2023 }
2024 return NULL;
2025}
2026
2027/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002028 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2029 * comment) and there is a next line, return the next line (skipping blanks)
2030 * and set "getnext".
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002031 * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
2032 * "arg" must point somewhere inside a line, not at the start.
2033 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002034 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002035eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2036{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002037 char_u *p = skipwhite(arg);
2038
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002039 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002040 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002041 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002042 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaar9d489562020-07-30 20:08:50 +02002043 && (*p == NUL || (VIM_ISWHITE(p[-1]) && vim9_comment_start(p))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002044 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002045 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002046
2047 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002048 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002049 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002050 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002051
Bram Moolenaar9d489562020-07-30 20:08:50 +02002052 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002053 {
2054 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002055 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002056 }
2057 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002058 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002059}
2060
2061/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002062 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002063 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002064 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002065eval_next_line(evalarg_T *evalarg)
2066{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002067 garray_T *gap = &evalarg->eval_ga;
2068 char_u *line;
2069
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002070 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002071 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2072 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002073 else
2074 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002075 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002076 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2077 {
2078 // Going to concatenate the lines after parsing.
2079 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2080 ++gap->ga_len;
2081 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002082 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002083 {
2084 vim_free(evalarg->eval_tofree);
2085 evalarg->eval_tofree = line;
2086 }
2087 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002088}
2089
Bram Moolenaare6b53242020-07-01 17:28:33 +02002090/*
2091 * Call eval_next_non_blank() and get the next line if needed.
2092 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002093 char_u *
2094skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2095{
2096 int getnext;
2097 char_u *p = skipwhite(arg);
2098
Bram Moolenaar962d7212020-07-04 14:15:00 +02002099 if (evalarg == NULL)
2100 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002101 eval_next_non_blank(p, evalarg, &getnext);
2102 if (getnext)
2103 return eval_next_line(evalarg);
2104 return p;
2105}
2106
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002107/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002108 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002109 */
2110 void
2111clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2112{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002113 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002114 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002115 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002116 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002117 if (eap != NULL)
2118 {
2119 // We may need to keep the original command line, e.g. for
2120 // ":let" it has the variable names. But we may also need the
2121 // new one, "nextcmd" points into it. Keep both.
2122 vim_free(eap->cmdline_tofree);
2123 eap->cmdline_tofree = *eap->cmdlinep;
2124 *eap->cmdlinep = evalarg->eval_tofree;
2125 }
2126 else
2127 vim_free(evalarg->eval_tofree);
2128 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002129 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002130
2131 vim_free(evalarg->eval_tofree_lambda);
2132 evalarg->eval_tofree_lambda = NULL;
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002133 }
2134}
2135
2136/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002138 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2140 */
2141
2142/*
2143 * Handle zero level expression.
2144 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002145 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002146 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002147 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 * Return OK or FAIL.
2149 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002150 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002151eval0(
2152 char_u *arg,
2153 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002154 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002155 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156{
2157 int ret;
2158 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002159 int did_emsg_before = did_emsg;
2160 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002161 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162
2163 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002164 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002165 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002166
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002167 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 {
2169 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002170 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 /*
2172 * Report the invalid expression unless the expression evaluation has
2173 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002174 * exception, or we already gave a more specific error.
2175 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002177 if (!aborting()
2178 && did_emsg == did_emsg_before
2179 && called_emsg == called_emsg_before
2180 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002181 semsg(_(e_invexpr2), arg);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002182
2183 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002184 // a next command to avoid more errors, unless "|" is following, which
2185 // could only be a command separator.
2186 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2187 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002188 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002190
2191 if (eap != NULL)
2192 eap->nextcmd = check_nextcmd(p);
2193
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 return ret;
2195}
2196
2197/*
2198 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002199 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002200 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 *
2202 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002203 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002205 * Note: "rettv.v_lock" is not set.
2206 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 * Return OK or FAIL.
2208 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002209 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002210eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002212 char_u *p;
2213 int getnext;
2214
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002215 CLEAR_POINTER(rettv);
2216
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 /*
2218 * Get the first variable.
2219 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002220 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 return FAIL;
2222
Bram Moolenaar793648f2020-06-26 21:28:25 +02002223 p = eval_next_non_blank(*arg, evalarg, &getnext);
2224 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002226 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002227 int result;
2228 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002229 evalarg_T *evalarg_used = evalarg;
2230 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002231 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002232 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002233 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002234
2235 if (evalarg == NULL)
2236 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002237 CLEAR_FIELD(local_evalarg);
2238 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002239 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002240 orig_flags = evalarg_used->eval_flags;
2241 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002242
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002243 if (getnext)
2244 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002245 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002246 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002247 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002248 {
2249 error_white_both(p, 1);
2250 clear_tv(rettv);
2251 return FAIL;
2252 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002253 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002254 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002255
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002257 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002259 int error = FALSE;
2260
Bram Moolenaar13106602020-10-04 16:06:05 +02002261 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002262 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002263 else if (vim9script)
2264 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002265 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002267 if (error || !op_falsy || !result)
2268 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002269 if (error)
2270 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 }
2272
2273 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002274 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002276 if (op_falsy)
2277 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002278 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002279 {
2280 error_white_both(p, 1);
2281 clear_tv(rettv);
2282 return FAIL;
2283 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002284 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002285 evalarg_used->eval_flags = (op_falsy ? !result : result)
2286 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2287 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002288 {
2289 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002291 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002292 if (!op_falsy || !result)
2293 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002295 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002297 /*
2298 * Check for the ":".
2299 */
2300 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2301 if (*p != ':')
2302 {
2303 emsg(_(e_missing_colon));
2304 if (evaluate && result)
2305 clear_tv(rettv);
2306 evalarg_used->eval_flags = orig_flags;
2307 return FAIL;
2308 }
2309 if (getnext)
2310 *arg = eval_next_line(evalarg_used);
2311 else
2312 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002313 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002314 {
2315 error_white_both(p, 1);
2316 clear_tv(rettv);
2317 evalarg_used->eval_flags = orig_flags;
2318 return FAIL;
2319 }
2320 *arg = p;
2321 }
2322
2323 /*
2324 * Get the third variable. Recursive!
2325 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002326 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002327 {
2328 error_white_both(p, 1);
2329 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002330 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002331 return FAIL;
2332 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002333 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2334 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002335 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002336 if (eval1(arg, &var2, evalarg_used) == FAIL)
2337 {
2338 if (evaluate && result)
2339 clear_tv(rettv);
2340 evalarg_used->eval_flags = orig_flags;
2341 return FAIL;
2342 }
2343 if (evaluate && !result)
2344 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002346
2347 if (evalarg == NULL)
2348 clear_evalarg(&local_evalarg, NULL);
2349 else
2350 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 }
2352
2353 return OK;
2354}
2355
2356/*
2357 * Handle first level expression:
2358 * expr2 || expr2 || expr2 logical OR
2359 *
2360 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002361 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 *
2363 * Return OK or FAIL.
2364 */
2365 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002366eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002368 char_u *p;
2369 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370
2371 /*
2372 * Get the first variable.
2373 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002374 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 return FAIL;
2376
2377 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002378 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002380 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002381 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 {
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 evaluate;
2386 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002387 long result = FALSE;
2388 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002389 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002390 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002391
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002392 if (evalarg == NULL)
2393 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002394 CLEAR_FIELD(local_evalarg);
2395 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002396 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002397 orig_flags = evalarg_used->eval_flags;
2398 evaluate = orig_flags & EVAL_EVALUATE;
2399 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002400 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002401 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002402 result = tv_get_bool_chk(rettv, &error);
2403 else if (tv_get_number_chk(rettv, &error) != 0)
2404 result = TRUE;
2405 clear_tv(rettv);
2406 if (error)
2407 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 }
2409
2410 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002411 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002413 while (p[0] == '|' && p[1] == '|')
2414 {
2415 if (getnext)
2416 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002417 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002418 {
2419 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2420 {
2421 error_white_both(p, 2);
2422 clear_tv(rettv);
2423 return FAIL;
2424 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002425 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002426 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002427
2428 /*
2429 * Get the second variable.
2430 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002431 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2432 {
2433 error_white_both(p, 2);
2434 clear_tv(rettv);
2435 return FAIL;
2436 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002437 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2438 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002439 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002440 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002441 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002442
2443 /*
2444 * Compute the result.
2445 */
2446 if (evaluate && !result)
2447 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002448 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002449 result = tv_get_bool_chk(&var2, &error);
2450 else if (tv_get_number_chk(&var2, &error) != 0)
2451 result = TRUE;
2452 clear_tv(&var2);
2453 if (error)
2454 return FAIL;
2455 }
2456 if (evaluate)
2457 {
2458 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002459 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002460 rettv->v_type = VAR_BOOL;
2461 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002462 }
2463 else
2464 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002465 rettv->v_type = VAR_NUMBER;
2466 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002467 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002468 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002469
2470 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002472
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002473 if (evalarg == NULL)
2474 clear_evalarg(&local_evalarg, NULL);
2475 else
2476 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 }
2478
2479 return OK;
2480}
2481
2482/*
2483 * Handle second level expression:
2484 * expr3 && expr3 && expr3 logical AND
2485 *
2486 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002487 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 *
2489 * Return OK or FAIL.
2490 */
2491 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002492eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002494 char_u *p;
2495 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496
2497 /*
2498 * Get the first variable.
2499 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002500 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 return FAIL;
2502
2503 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002504 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002506 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002507 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002509 evalarg_T *evalarg_used = evalarg;
2510 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002511 int orig_flags;
2512 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002513 long result = TRUE;
2514 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002515 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002516 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002517
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002518 if (evalarg == NULL)
2519 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002520 CLEAR_FIELD(local_evalarg);
2521 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002522 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002523 orig_flags = evalarg_used->eval_flags;
2524 evaluate = orig_flags & EVAL_EVALUATE;
2525 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002526 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002527 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002528 result = tv_get_bool_chk(rettv, &error);
2529 else if (tv_get_number_chk(rettv, &error) == 0)
2530 result = FALSE;
2531 clear_tv(rettv);
2532 if (error)
2533 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 }
2535
2536 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002537 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002539 while (p[0] == '&' && p[1] == '&')
2540 {
2541 if (getnext)
2542 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002543 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002544 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002545 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002546 {
2547 error_white_both(p, 2);
2548 clear_tv(rettv);
2549 return FAIL;
2550 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002551 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002552 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002553
2554 /*
2555 * Get the second variable.
2556 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002557 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2558 {
2559 error_white_both(p, 2);
2560 clear_tv(rettv);
2561 return FAIL;
2562 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002563 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2564 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002565 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002566 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002567 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002568 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002569
2570 /*
2571 * Compute the result.
2572 */
2573 if (evaluate && result)
2574 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002575 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002576 result = tv_get_bool_chk(&var2, &error);
2577 else if (tv_get_number_chk(&var2, &error) == 0)
2578 result = FALSE;
2579 clear_tv(&var2);
2580 if (error)
2581 return FAIL;
2582 }
2583 if (evaluate)
2584 {
2585 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002586 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002587 rettv->v_type = VAR_BOOL;
2588 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002589 }
2590 else
2591 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002592 rettv->v_type = VAR_NUMBER;
2593 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002594 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002595 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002596
2597 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002599
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002600 if (evalarg == NULL)
2601 clear_evalarg(&local_evalarg, NULL);
2602 else
2603 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 }
2605
2606 return OK;
2607}
2608
2609/*
2610 * Handle third level expression:
2611 * var1 == var2
2612 * var1 =~ var2
2613 * var1 != var2
2614 * var1 !~ var2
2615 * var1 > var2
2616 * var1 >= var2
2617 * var1 < var2
2618 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002619 * var1 is var2
2620 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 *
2622 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002623 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 *
2625 * Return OK or FAIL.
2626 */
2627 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002628eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002631 int getnext;
Bram Moolenaar87396072019-12-31 22:36:18 +01002632 exptype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002634 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635
2636 /*
2637 * Get the first variable.
2638 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002639 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 return FAIL;
2641
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002642 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002643 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644
2645 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002646 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002648 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002650 typval_T var2;
2651 int ic;
2652 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002653 int evaluate = evalarg == NULL
2654 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002655
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002656 if (getnext)
2657 *arg = eval_next_line(evalarg);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002658 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2659 {
2660 error_white_both(p, len);
2661 clear_tv(rettv);
2662 return FAIL;
2663 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002664
Bram Moolenaar696ba232020-07-29 21:20:41 +02002665 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2666 {
2667 semsg(_(e_invexpr2), p);
2668 clear_tv(rettv);
2669 return FAIL;
2670 }
2671
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002672 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 if (p[len] == '?')
2674 {
2675 ic = TRUE;
2676 ++len;
2677 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002678 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 else if (p[len] == '#')
2680 {
2681 ic = FALSE;
2682 ++len;
2683 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002684 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002686 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687
2688 /*
2689 * Get the second variable.
2690 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002691 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2692 {
2693 error_white_both(p, 1);
2694 clear_tv(rettv);
2695 return FAIL;
2696 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002697 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002698 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002700 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 return FAIL;
2702 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002703 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002704 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002705 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002706
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002707 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002708 {
2709 ret = FAIL;
2710 clear_tv(rettv);
2711 }
2712 else
2713 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002714 clear_tv(&var2);
2715 return ret;
2716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 }
2718
2719 return OK;
2720}
2721
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002722/*
2723 * Make a copy of blob "tv1" and append blob "tv2".
2724 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002725 void
2726eval_addblob(typval_T *tv1, typval_T *tv2)
2727{
2728 blob_T *b1 = tv1->vval.v_blob;
2729 blob_T *b2 = tv2->vval.v_blob;
2730 blob_T *b = blob_alloc();
2731 int i;
2732
2733 if (b != NULL)
2734 {
2735 for (i = 0; i < blob_len(b1); i++)
2736 ga_append(&b->bv_ga, blob_get(b1, i));
2737 for (i = 0; i < blob_len(b2); i++)
2738 ga_append(&b->bv_ga, blob_get(b2, i));
2739
2740 clear_tv(tv1);
2741 rettv_blob_set(tv1, b);
2742 }
2743}
2744
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002745/*
2746 * Make a copy of list "tv1" and append list "tv2".
2747 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002748 int
2749eval_addlist(typval_T *tv1, typval_T *tv2)
2750{
2751 typval_T var3;
2752
2753 // concatenate Lists
2754 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2755 {
2756 clear_tv(tv1);
2757 clear_tv(tv2);
2758 return FAIL;
2759 }
2760 clear_tv(tv1);
2761 *tv1 = var3;
2762 return OK;
2763}
2764
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765/*
2766 * Handle fourth level expression:
2767 * + number addition
2768 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002769 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002770 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 *
2772 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002773 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 *
2775 * Return OK or FAIL.
2776 */
2777 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002778eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 /*
2781 * Get the first variable.
2782 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002783 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 return FAIL;
2785
2786 /*
2787 * Repeat computing, until no '+', '-' or '.' is following.
2788 */
2789 for (;;)
2790 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002791 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002792 int getnext;
2793 char_u *p;
2794 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002795 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002796 int concat;
2797 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002798 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002799
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002800 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarce2c5442020-11-28 21:21:17 +01002801 // "+=" and "-=" are assignment
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002802 p = eval_next_non_blank(*arg, evalarg, &getnext);
2803 op = *p;
2804 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaarce2c5442020-11-28 21:21:17 +01002805 if ((op != '+' && op != '-' && !concat) || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002807
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002808 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002809 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002810 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002811 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002812 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002813 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002814 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002815 {
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002816 error_white_both(p, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002817 clear_tv(rettv);
2818 return FAIL;
2819 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002820 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002821 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002822 if ((op != '+' || (rettv->v_type != VAR_LIST
2823 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002824#ifdef FEAT_FLOAT
2825 && (op == '.' || rettv->v_type != VAR_FLOAT)
2826#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002827 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002828 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002829 int error = FALSE;
2830
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002831 // For "list + ...", an illegal use of the first operand as
2832 // a number cannot be determined before evaluating the 2nd
2833 // operand: if this is also a list, all is ok.
2834 // For "something . ...", "something - ..." or "non-list + ...",
2835 // we know that the first operand needs to be a string or number
2836 // without evaluating the 2nd operand. So check before to avoid
2837 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002838 if (op != '.')
2839 tv_get_number_chk(rettv, &error);
2840 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002841 {
2842 clear_tv(rettv);
2843 return FAIL;
2844 }
2845 }
2846
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 /*
2848 * Get the second variable.
2849 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002850 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002851 {
2852 error_white_both(p, oplen);
2853 clear_tv(rettv);
2854 return FAIL;
2855 }
2856 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002857 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002859 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 return FAIL;
2861 }
2862
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002863 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 {
2865 /*
2866 * Compute the result.
2867 */
2868 if (op == '.')
2869 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002870 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2871 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002872 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002873
Bram Moolenaar2e086612020-08-25 22:37:48 +02002874 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002875 || var2.v_type == VAR_CHANNEL
2876 || var2.v_type == VAR_JOB))
2877 emsg(_(e_inval_string));
2878#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02002879 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002880 {
2881 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2882 var2.vval.v_float);
2883 s2 = buf2;
2884 }
2885#endif
2886 else
2887 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002888 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002889 {
2890 clear_tv(rettv);
2891 clear_tv(&var2);
2892 return FAIL;
2893 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002894 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002895 clear_tv(rettv);
2896 rettv->v_type = VAR_STRING;
2897 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002899 else if (op == '+' && rettv->v_type == VAR_BLOB
2900 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002901 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002902 else if (op == '+' && rettv->v_type == VAR_LIST
2903 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002904 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002905 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002906 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 else
2909 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002910 int error = FALSE;
2911 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002912#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002913 float_T f1 = 0, f2 = 0;
2914
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002915 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002916 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002917 f1 = rettv->vval.v_float;
2918 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002919 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002920 else
2921#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002922 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002923 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002924 if (error)
2925 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002926 // This can only happen for "list + non-list". For
2927 // "non-list + ..." or "something - ...", we returned
2928 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002929 clear_tv(rettv);
2930 return FAIL;
2931 }
2932#ifdef FEAT_FLOAT
2933 if (var2.v_type == VAR_FLOAT)
2934 f1 = n1;
2935#endif
2936 }
2937#ifdef FEAT_FLOAT
2938 if (var2.v_type == VAR_FLOAT)
2939 {
2940 f2 = var2.vval.v_float;
2941 n2 = 0;
2942 }
2943 else
2944#endif
2945 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002946 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002947 if (error)
2948 {
2949 clear_tv(rettv);
2950 clear_tv(&var2);
2951 return FAIL;
2952 }
2953#ifdef FEAT_FLOAT
2954 if (rettv->v_type == VAR_FLOAT)
2955 f2 = n2;
2956#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002957 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002958 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002959
2960#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002961 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002962 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2963 {
2964 if (op == '+')
2965 f1 = f1 + f2;
2966 else
2967 f1 = f1 - f2;
2968 rettv->v_type = VAR_FLOAT;
2969 rettv->vval.v_float = f1;
2970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002972#endif
2973 {
2974 if (op == '+')
2975 n1 = n1 + n2;
2976 else
2977 n1 = n1 - n2;
2978 rettv->v_type = VAR_NUMBER;
2979 rettv->vval.v_number = n1;
2980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002982 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 }
2984 }
2985 return OK;
2986}
2987
2988/*
2989 * Handle fifth level expression:
2990 * * number multiplication
2991 * / number division
2992 * % number modulo
2993 *
2994 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002995 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 *
2997 * Return OK or FAIL.
2998 */
2999 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003000eval6(
3001 char_u **arg,
3002 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003003 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003004 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003006#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003007 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009
3010 /*
3011 * Get the first variable.
3012 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003013 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 return FAIL;
3015
3016 /*
3017 * Repeat computing, until no '*', '/' or '%' is following.
3018 */
3019 for (;;)
3020 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003021 int evaluate;
3022 int getnext;
3023 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003024 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003025 int op;
3026 varnumber_T n1, n2;
3027#ifdef FEAT_FLOAT
3028 float_T f1, f2;
3029#endif
3030 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003031
Bram Moolenaar9d489562020-07-30 20:08:50 +02003032 p = eval_next_non_blank(*arg, evalarg, &getnext);
3033 op = *p;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02003034 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003036
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003037 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003038 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003039 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003040 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003041 {
3042 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3043 {
3044 error_white_both(p, 1);
3045 clear_tv(rettv);
3046 return FAIL;
3047 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003048 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003051#ifdef FEAT_FLOAT
3052 f1 = 0;
3053 f2 = 0;
3054#endif
3055 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003056 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003058#ifdef FEAT_FLOAT
3059 if (rettv->v_type == VAR_FLOAT)
3060 {
3061 f1 = rettv->vval.v_float;
3062 use_float = TRUE;
3063 n1 = 0;
3064 }
3065 else
3066#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003067 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003068 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003069 if (error)
3070 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 }
3072 else
3073 n1 = 0;
3074
3075 /*
3076 * Get the second variable.
3077 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003078 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3079 {
3080 error_white_both(p, 1);
3081 clear_tv(rettv);
3082 return FAIL;
3083 }
3084 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003085 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 return FAIL;
3087
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003088 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003090#ifdef FEAT_FLOAT
3091 if (var2.v_type == VAR_FLOAT)
3092 {
3093 if (!use_float)
3094 {
3095 f1 = n1;
3096 use_float = TRUE;
3097 }
3098 f2 = var2.vval.v_float;
3099 n2 = 0;
3100 }
3101 else
3102#endif
3103 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003104 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003105 clear_tv(&var2);
3106 if (error)
3107 return FAIL;
3108#ifdef FEAT_FLOAT
3109 if (use_float)
3110 f2 = n2;
3111#endif
3112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113
3114 /*
3115 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003116 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003118#ifdef FEAT_FLOAT
3119 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003121 if (op == '*')
3122 f1 = f1 * f2;
3123 else if (op == '/')
3124 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003125# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003126 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003127 if (f2 == 0.0)
3128 {
3129 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003130 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003131 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003132 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003133 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003134 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003135 }
3136 else
3137 f1 = f1 / f2;
3138# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003139 // We rely on the floating point library to handle divide
3140 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003141 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003142# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003145 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003146 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003147 return FAIL;
3148 }
3149 rettv->v_type = VAR_FLOAT;
3150 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 }
3152 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003153#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003155 if (op == '*')
3156 n1 = n1 * n2;
3157 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01003158 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01003160 n1 = num_modulus(n1, n2);
3161
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003162 rettv->v_type = VAR_NUMBER;
3163 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 }
3166 }
3167
3168 return OK;
3169}
3170
3171/*
3172 * Handle sixth level expression:
3173 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003174 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003175 * "string" string constant
3176 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 * &option-name option value
3178 * @r register contents
3179 * identifier variable value
3180 * function() function call
3181 * $VAR environment variable
3182 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003183 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003184 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003185 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003186 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 *
3188 * Also handle:
3189 * ! in front logical NOT
3190 * - in front unary minus
3191 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003192 * trailing [] subscript in String or List
3193 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003194 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 *
3196 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003197 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 *
3199 * Return OK or FAIL.
3200 */
3201 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003202eval7(
3203 char_u **arg,
3204 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003205 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003206 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003208 int evaluate = evalarg != NULL
3209 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 int len;
3211 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 char_u *start_leader, *end_leader;
3213 int ret = OK;
3214 char_u *alias;
3215
3216 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003217 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003218 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003220 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221
3222 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003223 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 */
3225 start_leader = *arg;
3226 while (**arg == '!' || **arg == '-' || **arg == '+')
3227 *arg = skipwhite(*arg + 1);
3228 end_leader = *arg;
3229
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003230 if (**arg == '.' && (!isdigit(*(*arg + 1))
3231#ifdef FEAT_FLOAT
3232 || current_sctx.sc_version < 2
3233#endif
3234 ))
3235 {
3236 semsg(_(e_invexpr2), *arg);
3237 ++*arg;
3238 return FAIL;
3239 }
3240
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 switch (**arg)
3242 {
3243 /*
3244 * Number constant.
3245 */
3246 case '0':
3247 case '1':
3248 case '2':
3249 case '3':
3250 case '4':
3251 case '5':
3252 case '6':
3253 case '7':
3254 case '8':
3255 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003256 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003257
3258 // Apply prefixed "-" and "+" now. Matters especially when
3259 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003260 if (ret == OK && evaluate && end_leader > start_leader
3261 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003262 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 break;
3264
3265 /*
3266 * String constant: "string".
3267 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003268 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 break;
3270
3271 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003272 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003274 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003275 break;
3276
3277 /*
3278 * List: [expr, expr]
3279 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003280 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 break;
3282
3283 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003284 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003285 */
Bram Moolenaare0de1712020-12-02 17:36:54 +01003286 case '#': if (!in_vim9script() && (*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003287 {
3288 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003289 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003290 }
3291 else
3292 ret = NOTDONE;
3293 break;
3294
3295 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003296 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003297 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003298 */
Bram Moolenaarc2ca9352020-11-25 21:30:11 +01003299 case '{': ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003300 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003301 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003302 break;
3303
3304 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003305 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003307 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 break;
3309
3310 /*
3311 * Environment variable: $VAR.
3312 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003313 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 break;
3315
3316 /*
3317 * Register contents: @r.
3318 */
3319 case '@': ++*arg;
3320 if (evaluate)
3321 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003322 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02003323 rettv->vval.v_string = get_reg_contents(**arg,
3324 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 }
3326 if (**arg != NUL)
3327 ++*arg;
3328 break;
3329
3330 /*
3331 * nested expression: (expression).
3332 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003333 case '(': {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003334 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003335 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003336
Bram Moolenaar9215f012020-06-27 21:18:00 +02003337 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003338 if (**arg == ')')
3339 ++*arg;
3340 else if (ret == OK)
3341 {
3342 emsg(_(e_missing_close));
3343 clear_tv(rettv);
3344 ret = FAIL;
3345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 }
3347 break;
3348
Bram Moolenaar8c711452005-01-14 21:53:12 +00003349 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 break;
3351 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003352
3353 if (ret == NOTDONE)
3354 {
3355 /*
3356 * Must be a variable or function name.
3357 * Can also be a curly-braces kind of name: {expr}.
3358 */
3359 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003360 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003361 if (alias != NULL)
3362 s = alias;
3363
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003364 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003365 ret = FAIL;
3366 else
3367 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003368 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3369
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003370 if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
3371 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003372 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003373 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003374 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003375 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003376 else if (flags & EVAL_CONSTANT)
3377 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003378 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003379 {
3380 // get the value of "true", "false" or a variable
3381 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3382 {
3383 rettv->v_type = VAR_BOOL;
3384 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003385 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003386 }
3387 else if (len == 5 && in_vim9script()
3388 && STRNCMP(s, "false", 4) == 0)
3389 {
3390 rettv->v_type = VAR_BOOL;
3391 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003392 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003393 }
3394 else
3395 ret = eval_variable(s, len, rettv, NULL, TRUE, FALSE);
3396 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003397 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003398 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003399 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003400 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003401 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003402 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003403 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003404 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003405 }
3406
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003407 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3408 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003409 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003410 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411
3412 /*
3413 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3414 */
3415 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003416 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003417 return ret;
3418}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003419
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003420/*
3421 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003422 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003423 * Adjusts "end_leaderp" until it is at "start_leader".
3424 */
3425 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003426eval7_leader(
3427 typval_T *rettv,
3428 int numeric_only,
3429 char_u *start_leader,
3430 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003431{
3432 char_u *end_leader = *end_leaderp;
3433 int ret = OK;
3434 int error = FALSE;
3435 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003436 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003437#ifdef FEAT_FLOAT
3438 float_T f = 0.0;
3439
3440 if (rettv->v_type == VAR_FLOAT)
3441 f = rettv->vval.v_float;
3442 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003443#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003444 {
3445 while (VIM_ISWHITE(end_leader[-1]))
3446 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003447 if (in_vim9script() && end_leader[-1] == '!')
3448 val = tv2bool(rettv);
3449 else
3450 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003451 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003452 if (error)
3453 {
3454 clear_tv(rettv);
3455 ret = FAIL;
3456 }
3457 else
3458 {
3459 while (end_leader > start_leader)
3460 {
3461 --end_leader;
3462 if (*end_leader == '!')
3463 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003464 if (numeric_only)
3465 {
3466 ++end_leader;
3467 break;
3468 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003469#ifdef FEAT_FLOAT
3470 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003471 {
3472 if (in_vim9script())
3473 {
3474 rettv->v_type = VAR_BOOL;
3475 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3476 }
3477 else
3478 f = !f;
3479 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003480 else
3481#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003482 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003483 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003484 type = VAR_BOOL;
3485 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003486 }
3487 else if (*end_leader == '-')
3488 {
3489#ifdef FEAT_FLOAT
3490 if (rettv->v_type == VAR_FLOAT)
3491 f = -f;
3492 else
3493#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003494 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003495 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003496 type = VAR_NUMBER;
3497 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003498 }
3499 }
3500#ifdef FEAT_FLOAT
3501 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003503 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003504 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003506 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003507#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003508 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003509 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003510 if (in_vim9script())
3511 rettv->v_type = type;
3512 else
3513 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003514 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003517 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 return ret;
3519}
3520
3521/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003522 * Call the function referred to in "rettv".
3523 */
3524 static int
3525call_func_rettv(
3526 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003527 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003528 typval_T *rettv,
3529 int evaluate,
3530 dict_T *selfdict,
3531 typval_T *basetv)
3532{
3533 partial_T *pt = NULL;
3534 funcexe_T funcexe;
3535 typval_T functv;
3536 char_u *s;
3537 int ret;
3538
3539 // need to copy the funcref so that we can clear rettv
3540 if (evaluate)
3541 {
3542 functv = *rettv;
3543 rettv->v_type = VAR_UNKNOWN;
3544
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003545 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003546 if (functv.v_type == VAR_PARTIAL)
3547 {
3548 pt = functv.vval.v_partial;
3549 s = partial_name(pt);
3550 }
3551 else
3552 s = functv.vval.v_string;
3553 }
3554 else
3555 s = (char_u *)"";
3556
Bram Moolenaara80faa82020-04-12 19:37:17 +02003557 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003558 funcexe.firstline = curwin->w_cursor.lnum;
3559 funcexe.lastline = curwin->w_cursor.lnum;
3560 funcexe.evaluate = evaluate;
3561 funcexe.partial = pt;
3562 funcexe.selfdict = selfdict;
3563 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003564 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003565
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003566 // Clear the funcref afterwards, so that deleting it while
3567 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003568 if (evaluate)
3569 clear_tv(&functv);
3570
3571 return ret;
3572}
3573
3574/*
3575 * Evaluate "->method()".
3576 * "*arg" points to the '-'.
3577 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3578 */
3579 static int
3580eval_lambda(
3581 char_u **arg,
3582 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003583 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003584 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003585{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003586 int evaluate = evalarg != NULL
3587 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003588 typval_T base = *rettv;
3589 int ret;
3590
3591 // Skip over the ->.
3592 *arg += 2;
3593 rettv->v_type = VAR_UNKNOWN;
3594
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003595 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003596 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003597 return FAIL;
3598 else if (**arg != '(')
3599 {
3600 if (verbose)
3601 {
3602 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003603 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003604 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003605 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003606 }
3607 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003608 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003609 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003610 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003611 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003612
3613 // Clear the funcref afterwards, so that deleting it while
3614 // evaluating the arguments is possible (see test55).
3615 if (evaluate)
3616 clear_tv(&base);
3617
3618 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003619}
3620
3621/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003622 * Evaluate "->method()".
3623 * "*arg" points to the '-'.
3624 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3625 */
3626 static int
3627eval_method(
3628 char_u **arg,
3629 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003630 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003631 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003632{
3633 char_u *name;
3634 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003635 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003636 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003637 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003638 int evaluate = evalarg != NULL
3639 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003640
3641 // Skip over the ->.
3642 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003643 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003644
Bram Moolenaarac92e252019-08-03 21:58:38 +02003645 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003646 len = get_name_len(arg, &alias, evaluate, TRUE);
3647 if (alias != NULL)
3648 name = alias;
3649
3650 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003651 {
3652 if (verbose)
3653 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003654 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003655 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003656 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003657 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003658 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003659 if (**arg != '(')
3660 {
3661 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003662 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003663 ret = FAIL;
3664 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003665 else if (VIM_ISWHITE((*arg)[-1]))
3666 {
3667 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003668 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003669 ret = FAIL;
3670 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003671 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003672 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003673 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003674 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003675
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003676 // Clear the funcref afterwards, so that deleting it while
3677 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003678 if (evaluate)
3679 clear_tv(&base);
3680
Bram Moolenaarac92e252019-08-03 21:58:38 +02003681 return ret;
3682}
3683
3684/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003685 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3686 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003687 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3688 */
3689 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003690eval_index(
3691 char_u **arg,
3692 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003693 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003694 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003695{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003696 int evaluate = evalarg != NULL
3697 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003698 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003699 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003700 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003701 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003702 int keylen = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003703
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003704 if (check_can_index(rettv, evaluate, verbose) == FAIL)
3705 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003706
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003707 init_tv(&var1);
3708 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003709 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003710 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003711 /*
3712 * dict.name
3713 */
3714 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003715 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003716 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003717 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003718 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02003719 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003720 }
3721 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003722 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003723 /*
3724 * something[idx]
3725 *
3726 * Get the (first) variable from inside the [].
3727 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003728 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003729 if (**arg == ':')
3730 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003731 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003732 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003733 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003734 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003735 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003736 clear_tv(&var1);
3737 return FAIL;
3738 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003739
3740 /*
3741 * Get the second variable from inside the [:].
3742 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003743 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003744 if (**arg == ':')
3745 {
3746 range = TRUE;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003747 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003748 if (**arg == ']')
3749 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003750 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003751 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003752 if (!empty1)
3753 clear_tv(&var1);
3754 return FAIL;
3755 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003756 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003757 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003758 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003759 if (!empty1)
3760 clear_tv(&var1);
3761 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003762 return FAIL;
3763 }
3764 }
3765
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003766 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003767 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003768 if (**arg != ']')
3769 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003770 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003771 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003772 clear_tv(&var1);
3773 if (range)
3774 clear_tv(&var2);
3775 return FAIL;
3776 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02003777 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003778 }
3779
3780 if (evaluate)
3781 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003782 int res = eval_index_inner(rettv, range,
3783 empty1 ? NULL : &var1, empty2 ? NULL : &var2,
3784 key, keylen, verbose);
3785 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003786 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003787 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003788 clear_tv(&var2);
3789 return res;
3790 }
3791 return OK;
3792}
3793
3794/*
3795 * Check if "rettv" can have an [index] or [sli:ce]
3796 */
3797 int
3798check_can_index(typval_T *rettv, int evaluate, int verbose)
3799{
3800 switch (rettv->v_type)
3801 {
3802 case VAR_FUNC:
3803 case VAR_PARTIAL:
3804 if (verbose)
3805 emsg(_("E695: Cannot index a Funcref"));
3806 return FAIL;
3807 case VAR_FLOAT:
3808#ifdef FEAT_FLOAT
3809 if (verbose)
3810 emsg(_(e_float_as_string));
3811 return FAIL;
3812#endif
3813 case VAR_BOOL:
3814 case VAR_SPECIAL:
3815 case VAR_JOB:
3816 case VAR_CHANNEL:
3817 if (verbose)
3818 emsg(_(e_cannot_index_special_variable));
3819 return FAIL;
3820 case VAR_UNKNOWN:
3821 case VAR_ANY:
3822 case VAR_VOID:
3823 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003824 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003825 emsg(_(e_cannot_index_special_variable));
3826 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003827 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003828 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003829
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003830 case VAR_STRING:
3831 case VAR_LIST:
3832 case VAR_DICT:
3833 case VAR_BLOB:
3834 break;
3835 case VAR_NUMBER:
3836 if (in_vim9script())
3837 emsg(_(e_cannot_index_number));
3838 break;
3839 }
3840 return OK;
3841}
3842
3843/*
3844 * Apply index or range to "rettv".
3845 * "var1" is the first index, NULL for [:expr].
3846 * "var2" is the second index, NULL for [expr] and [expr: ]
3847 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
3848 */
3849 int
3850eval_index_inner(
3851 typval_T *rettv,
3852 int is_range,
3853 typval_T *var1,
3854 typval_T *var2,
3855 char_u *key,
3856 int keylen,
3857 int verbose)
3858{
3859 long n1, n2 = 0;
3860 long len;
3861
3862 n1 = 0;
3863 if (var1 != NULL && rettv->v_type != VAR_DICT)
3864 n1 = tv_get_number(var1);
3865
3866 if (is_range)
3867 {
3868 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003869 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003870 if (verbose)
3871 emsg(_(e_cannot_slice_dictionary));
3872 return FAIL;
3873 }
3874 if (var2 == NULL)
3875 n2 = -1;
3876 else
3877 n2 = tv_get_number(var2);
3878 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01003879
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003880 switch (rettv->v_type)
3881 {
3882 case VAR_UNKNOWN:
3883 case VAR_ANY:
3884 case VAR_VOID:
3885 case VAR_FUNC:
3886 case VAR_PARTIAL:
3887 case VAR_FLOAT:
3888 case VAR_BOOL:
3889 case VAR_SPECIAL:
3890 case VAR_JOB:
3891 case VAR_CHANNEL:
3892 break; // not evaluating, skipping over subscript
3893
3894 case VAR_NUMBER:
3895 case VAR_STRING:
3896 {
3897 char_u *s = tv_get_string(rettv);
3898
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003899 len = (long)STRLEN(s);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003900 if (in_vim9script())
3901 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003902 if (is_range)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003903 s = string_slice(s, n1, n2);
3904 else
3905 s = char_from_string(s, n1);
3906 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003907 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003908 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003909 // The resulting variable is a substring. If the indexes
3910 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003911 if (n1 < 0)
3912 {
3913 n1 = len + n1;
3914 if (n1 < 0)
3915 n1 = 0;
3916 }
3917 if (n2 < 0)
3918 n2 = len + n2;
3919 else if (n2 >= len)
3920 n2 = len;
3921 if (n1 >= len || n2 < 0 || n1 > n2)
3922 s = NULL;
3923 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02003924 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003925 }
3926 else
3927 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003928 // The resulting variable is a string of a single
3929 // character. If the index is too big or negative the
3930 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003931 if (n1 >= len || n1 < 0)
3932 s = NULL;
3933 else
3934 s = vim_strnsave(s + n1, 1);
3935 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003936 clear_tv(rettv);
3937 rettv->v_type = VAR_STRING;
3938 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003939 }
3940 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003941
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003942 case VAR_BLOB:
3943 len = blob_len(rettv->vval.v_blob);
3944 if (is_range)
3945 {
3946 // The resulting variable is a sub-blob. If the indexes
3947 // are out of range the result is empty.
3948 if (n1 < 0)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003949 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003950 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003951 if (n1 < 0)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003952 n1 = 0;
3953 }
3954 if (n2 < 0)
3955 n2 = len + n2;
3956 else if (n2 >= len)
3957 n2 = len - 1;
3958 if (n1 >= len || n2 < 0 || n1 > n2)
3959 {
3960 clear_tv(rettv);
3961 rettv->v_type = VAR_BLOB;
3962 rettv->vval.v_blob = NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003963 }
3964 else
3965 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003966 blob_T *blob = blob_alloc();
3967 long i;
3968
3969 if (blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003970 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003971 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003972 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003973 blob_free(blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003974 return FAIL;
3975 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003976 blob->bv_ga.ga_len = n2 - n1 + 1;
3977 for (i = n1; i <= n2; i++)
3978 blob_set(blob, i - n1,
3979 blob_get(rettv->vval.v_blob, i));
3980
3981 clear_tv(rettv);
3982 rettv_blob_set(rettv, blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003983 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003984 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003985 }
3986 else
3987 {
3988 // The resulting variable is a byte value.
3989 // If the index is too big or negative that is an error.
3990 if (n1 < 0)
3991 n1 = len + n1;
3992 if (n1 < len && n1 >= 0)
3993 {
3994 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003995
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003996 clear_tv(rettv);
3997 rettv->v_type = VAR_NUMBER;
3998 rettv->vval.v_number = v;
3999 }
4000 else
4001 semsg(_(e_blobidx), n1);
4002 }
4003 break;
4004
4005 case VAR_LIST:
4006 if (var1 == NULL)
4007 n1 = 0;
4008 if (var2 == NULL)
4009 n2 = -1;
4010 if (list_slice_or_index(rettv->vval.v_list,
4011 is_range, n1, n2, rettv, verbose) == FAIL)
4012 return FAIL;
4013 break;
4014
4015 case VAR_DICT:
4016 {
4017 dictitem_T *item;
4018 typval_T tmp;
4019
4020 if (key == NULL)
4021 {
4022 key = tv_get_string_chk(var1);
4023 if (key == NULL)
4024 return FAIL;
4025 }
4026
4027 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
4028
4029 if (item == NULL && verbose)
4030 semsg(_(e_dictkey), key);
4031 if (item == NULL)
4032 return FAIL;
4033
4034 copy_tv(&item->di_tv, &tmp);
4035 clear_tv(rettv);
4036 *rettv = tmp;
4037 }
4038 break;
4039 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004040 return OK;
4041}
4042
4043/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004044 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004045 */
4046 char_u *
4047partial_name(partial_T *pt)
4048{
4049 if (pt->pt_name != NULL)
4050 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004051 if (pt->pt_func != NULL)
4052 return pt->pt_func->uf_name;
4053 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004054}
4055
Bram Moolenaarddecc252016-04-06 22:59:37 +02004056 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004057partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004058{
4059 int i;
4060
4061 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004062 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004063 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004064 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004065 if (pt->pt_name != NULL)
4066 {
4067 func_unref(pt->pt_name);
4068 vim_free(pt->pt_name);
4069 }
4070 else
4071 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004072
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004073 // Decrease the reference count for the context of a closure. If down
4074 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004075 if (pt->pt_funcstack != NULL)
4076 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004077 --pt->pt_funcstack->fs_refcount;
4078 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004079 }
4080
Bram Moolenaarddecc252016-04-06 22:59:37 +02004081 vim_free(pt);
4082}
4083
4084/*
4085 * Unreference a closure: decrement the reference count and free it when it
4086 * becomes zero.
4087 */
4088 void
4089partial_unref(partial_T *pt)
4090{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004091 if (pt != NULL)
4092 {
4093 if (--pt->pt_refcount <= 0)
4094 partial_free(pt);
4095
4096 // If the reference count goes down to one, the funcstack may be the
4097 // only reference and can be freed if no other partials reference it.
4098 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4099 funcstack_check_refcount(pt->pt_funcstack);
4100 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004101}
4102
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004103/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004104 * Return the next (unique) copy ID.
4105 * Used for serializing nested structures.
4106 */
4107 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004108get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004109{
4110 current_copyID += COPYID_INC;
4111 return current_copyID;
4112}
4113
4114/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004115 * Garbage collection for lists and dictionaries.
4116 *
4117 * We use reference counts to be able to free most items right away when they
4118 * are no longer used. But for composite items it's possible that it becomes
4119 * unused while the reference count is > 0: When there is a recursive
4120 * reference. Example:
4121 * :let l = [1, 2, 3]
4122 * :let d = {9: l}
4123 * :let l[1] = d
4124 *
4125 * Since this is quite unusual we handle this with garbage collection: every
4126 * once in a while find out which lists and dicts are not referenced from any
4127 * variable.
4128 *
4129 * Here is a good reference text about garbage collection (refers to Python
4130 * but it applies to all reference-counting mechanisms):
4131 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004132 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004133
4134/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004135 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004136 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004137 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004138 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004139 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004140garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004141{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004142 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004143 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004144 buf_T *buf;
4145 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004146 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004147 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004148
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004149 if (!testing)
4150 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004151 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004152 want_garbage_collect = FALSE;
4153 may_garbage_collect = FALSE;
4154 garbage_collect_at_exit = FALSE;
4155 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004156
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004157 // The execution stack can grow big, limit the size.
4158 if (exestack.ga_maxlen - exestack.ga_len > 500)
4159 {
4160 size_t new_len;
4161 char_u *pp;
4162 int n;
4163
4164 // Keep 150% of the current size, with a minimum of the growth size.
4165 n = exestack.ga_len / 2;
4166 if (n < exestack.ga_growsize)
4167 n = exestack.ga_growsize;
4168
4169 // Don't make it bigger though.
4170 if (exestack.ga_len + n < exestack.ga_maxlen)
4171 {
4172 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4173 pp = vim_realloc(exestack.ga_data, new_len);
4174 if (pp == NULL)
4175 return FAIL;
4176 exestack.ga_maxlen = exestack.ga_len + n;
4177 exestack.ga_data = pp;
4178 }
4179 }
4180
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004181 // We advance by two because we add one for items referenced through
4182 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004183 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004184
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004185 /*
4186 * 1. Go through all accessible variables and mark all lists and dicts
4187 * with copyID.
4188 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004189
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004190 // Don't free variables in the previous_funccal list unless they are only
4191 // referenced through previous_funccal. This must be first, because if
4192 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004193 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004194
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004195 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004196 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004197
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004198 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004199 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004200 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4201 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004202
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004203 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004204 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004205 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4206 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004207 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004208 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4209 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004210#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004211 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004212 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4213 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004214 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004215 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004216 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4217 NULL, NULL);
4218#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004219
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004220 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004221 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004222 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4223 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004224 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004225 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004226
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004227 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004228 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004229
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004230 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004231 abort = abort || set_ref_in_functions(copyID);
4232
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004233 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004234 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004235
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004236 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004237 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004238
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004239 // callbacks in buffers
4240 abort = abort || set_ref_in_buffers(copyID);
4241
Bram Moolenaar1dced572012-04-05 16:54:08 +02004242#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004243 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004244#endif
4245
Bram Moolenaardb913952012-06-29 12:54:53 +02004246#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004247 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004248#endif
4249
4250#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004251 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004252#endif
4253
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004254#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004255 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004256 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004257#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004258#ifdef FEAT_NETBEANS_INTG
4259 abort = abort || set_ref_in_nb_channel(copyID);
4260#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004261
Bram Moolenaare3188e22016-05-31 21:13:04 +02004262#ifdef FEAT_TIMERS
4263 abort = abort || set_ref_in_timer(copyID);
4264#endif
4265
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004266#ifdef FEAT_QUICKFIX
4267 abort = abort || set_ref_in_quickfix(copyID);
4268#endif
4269
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004270#ifdef FEAT_TERMINAL
4271 abort = abort || set_ref_in_term(copyID);
4272#endif
4273
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004274#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004275 abort = abort || set_ref_in_popups(copyID);
4276#endif
4277
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004278 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004279 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004280 /*
4281 * 2. Free lists and dictionaries that are not referenced.
4282 */
4283 did_free = free_unref_items(copyID);
4284
4285 /*
4286 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004287 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004288 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004289 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004290 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004291 else if (p_verbose > 0)
4292 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004293 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004294 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004295
4296 return did_free;
4297}
4298
4299/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004300 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004301 */
4302 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004303free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004304{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004305 int did_free = FALSE;
4306
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004307 // Let all "free" functions know that we are here. This means no
4308 // dictionaries, lists, channels or jobs are to be freed, because we will
4309 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004310 in_free_unref_items = TRUE;
4311
4312 /*
4313 * PASS 1: free the contents of the items. We don't free the items
4314 * themselves yet, so that it is possible to decrement refcount counters
4315 */
4316
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004317 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004318 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004319
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004320 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004321 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004322
4323#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004324 // Go through the list of jobs and free items without the copyID. This
4325 // must happen before doing channels, because jobs refer to channels, but
4326 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004327 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4328
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004329 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004330 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4331#endif
4332
4333 /*
4334 * PASS 2: free the items themselves.
4335 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004336 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004337 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004338
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004339#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004340 // Go through the list of jobs and free items without the copyID. This
4341 // must happen before doing channels, because jobs refer to channels, but
4342 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004343 free_unused_jobs(copyID, COPYID_MASK);
4344
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004345 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004346 free_unused_channels(copyID, COPYID_MASK);
4347#endif
4348
4349 in_free_unref_items = FALSE;
4350
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004351 return did_free;
4352}
4353
4354/*
4355 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004356 * "list_stack" is used to add lists to be marked. Can be NULL.
4357 *
4358 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004359 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004360 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004361set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004362{
4363 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004364 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004365 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004366 hashtab_T *cur_ht;
4367 ht_stack_T *ht_stack = NULL;
4368 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004369
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004370 cur_ht = ht;
4371 for (;;)
4372 {
4373 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004374 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004375 // Mark each item in the hashtab. If the item contains a hashtab
4376 // it is added to ht_stack, if it contains a list it is added to
4377 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004378 todo = (int)cur_ht->ht_used;
4379 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4380 if (!HASHITEM_EMPTY(hi))
4381 {
4382 --todo;
4383 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4384 &ht_stack, list_stack);
4385 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004386 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004387
4388 if (ht_stack == NULL)
4389 break;
4390
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004391 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004392 cur_ht = ht_stack->ht;
4393 tempitem = ht_stack;
4394 ht_stack = ht_stack->prev;
4395 free(tempitem);
4396 }
4397
4398 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004399}
4400
4401/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004402 * Mark a dict and its items with "copyID".
4403 * Returns TRUE if setting references failed somehow.
4404 */
4405 int
4406set_ref_in_dict(dict_T *d, int copyID)
4407{
4408 if (d != NULL && d->dv_copyID != copyID)
4409 {
4410 d->dv_copyID = copyID;
4411 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4412 }
4413 return FALSE;
4414}
4415
4416/*
4417 * Mark a list and its items with "copyID".
4418 * Returns TRUE if setting references failed somehow.
4419 */
4420 int
4421set_ref_in_list(list_T *ll, int copyID)
4422{
4423 if (ll != NULL && ll->lv_copyID != copyID)
4424 {
4425 ll->lv_copyID = copyID;
4426 return set_ref_in_list_items(ll, copyID, NULL);
4427 }
4428 return FALSE;
4429}
4430
4431/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004432 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004433 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4434 *
4435 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004436 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004437 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004438set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004439{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004440 listitem_T *li;
4441 int abort = FALSE;
4442 list_T *cur_l;
4443 list_stack_T *list_stack = NULL;
4444 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004445
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004446 cur_l = l;
4447 for (;;)
4448 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004449 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004450 // Mark each item in the list. If the item contains a hashtab
4451 // it is added to ht_stack, if it contains a list it is added to
4452 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004453 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4454 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4455 ht_stack, &list_stack);
4456 if (list_stack == NULL)
4457 break;
4458
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004459 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004460 cur_l = list_stack->list;
4461 tempitem = list_stack;
4462 list_stack = list_stack->prev;
4463 free(tempitem);
4464 }
4465
4466 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004467}
4468
4469/*
4470 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004471 * "list_stack" is used to add lists to be marked. Can be NULL.
4472 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4473 *
4474 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004475 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004476 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004477set_ref_in_item(
4478 typval_T *tv,
4479 int copyID,
4480 ht_stack_T **ht_stack,
4481 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004482{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004483 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004484
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004485 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004486 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004487 dict_T *dd = tv->vval.v_dict;
4488
Bram Moolenaara03f2332016-02-06 18:09:59 +01004489 if (dd != NULL && dd->dv_copyID != copyID)
4490 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004491 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004492 dd->dv_copyID = copyID;
4493 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004494 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004495 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4496 }
4497 else
4498 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004499 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4500
Bram Moolenaara03f2332016-02-06 18:09:59 +01004501 if (newitem == NULL)
4502 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004503 else
4504 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004505 newitem->ht = &dd->dv_hashtab;
4506 newitem->prev = *ht_stack;
4507 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004508 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004509 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004510 }
4511 }
4512 else if (tv->v_type == VAR_LIST)
4513 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004514 list_T *ll = tv->vval.v_list;
4515
Bram Moolenaara03f2332016-02-06 18:09:59 +01004516 if (ll != NULL && ll->lv_copyID != copyID)
4517 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004518 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004519 ll->lv_copyID = copyID;
4520 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004521 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004522 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004523 }
4524 else
4525 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004526 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4527
Bram Moolenaara03f2332016-02-06 18:09:59 +01004528 if (newitem == NULL)
4529 abort = TRUE;
4530 else
4531 {
4532 newitem->list = ll;
4533 newitem->prev = *list_stack;
4534 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004535 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004536 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004537 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004538 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004539 else if (tv->v_type == VAR_FUNC)
4540 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004541 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004542 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004543 else if (tv->v_type == VAR_PARTIAL)
4544 {
4545 partial_T *pt = tv->vval.v_partial;
4546 int i;
4547
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004548 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004549 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004550 // Didn't see this partial yet.
4551 pt->pt_copyID = copyID;
4552
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004553 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004554
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004555 if (pt->pt_dict != NULL)
4556 {
4557 typval_T dtv;
4558
4559 dtv.v_type = VAR_DICT;
4560 dtv.vval.v_dict = pt->pt_dict;
4561 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4562 }
4563
4564 for (i = 0; i < pt->pt_argc; ++i)
4565 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4566 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004567 if (pt->pt_funcstack != NULL)
4568 {
4569 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4570
4571 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4572 abort = abort || set_ref_in_item(stack + i, copyID,
4573 ht_stack, list_stack);
4574 }
4575
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004576 }
4577 }
4578#ifdef FEAT_JOB_CHANNEL
4579 else if (tv->v_type == VAR_JOB)
4580 {
4581 job_T *job = tv->vval.v_job;
4582 typval_T dtv;
4583
4584 if (job != NULL && job->jv_copyID != copyID)
4585 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004586 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004587 if (job->jv_channel != NULL)
4588 {
4589 dtv.v_type = VAR_CHANNEL;
4590 dtv.vval.v_channel = job->jv_channel;
4591 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4592 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004593 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004594 {
4595 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004596 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004597 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4598 }
4599 }
4600 }
4601 else if (tv->v_type == VAR_CHANNEL)
4602 {
4603 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004604 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004605 typval_T dtv;
4606 jsonq_T *jq;
4607 cbq_T *cq;
4608
4609 if (ch != NULL && ch->ch_copyID != copyID)
4610 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004611 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004612 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004613 {
4614 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4615 jq = jq->jq_next)
4616 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4617 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4618 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004619 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004620 {
4621 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004622 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004623 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4624 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004625 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004626 {
4627 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004628 dtv.vval.v_partial =
4629 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004630 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4631 }
4632 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004633 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004634 {
4635 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004636 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004637 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4638 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004639 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004640 {
4641 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004642 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004643 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4644 }
4645 }
4646 }
4647#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004648 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004649}
4650
Bram Moolenaar8c711452005-01-14 21:53:12 +00004651/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004652 * Return a string with the string representation of a variable.
4653 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004654 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004655 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004656 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004657 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02004658 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004659 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4660 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004661 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004662 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004663 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004664echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004665 typval_T *tv,
4666 char_u **tofree,
4667 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004668 int copyID,
4669 int echo_style,
4670 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004671 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004672{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004673 static int recurse = 0;
4674 char_u *r = NULL;
4675
Bram Moolenaar33570922005-01-25 22:26:29 +00004676 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004677 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004678 if (!did_echo_string_emsg)
4679 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004680 // Only give this message once for a recursive call to avoid
4681 // flooding the user with errors. And stop iterating over lists
4682 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004683 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004684 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004685 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004686 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004687 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004688 }
4689 ++recurse;
4690
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004691 switch (tv->v_type)
4692 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004693 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004694 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004695 {
4696 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004697 r = tv->vval.v_string;
4698 if (r == NULL)
4699 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004700 }
4701 else
4702 {
4703 *tofree = string_quote(tv->vval.v_string, FALSE);
4704 r = *tofree;
4705 }
4706 break;
4707
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004708 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004709 if (echo_style)
4710 {
4711 *tofree = NULL;
4712 r = tv->vval.v_string;
4713 }
4714 else
4715 {
4716 *tofree = string_quote(tv->vval.v_string, TRUE);
4717 r = *tofree;
4718 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004719 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004720
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004721 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004722 {
4723 partial_T *pt = tv->vval.v_partial;
4724 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004725 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004726 garray_T ga;
4727 int i;
4728 char_u *tf;
4729
4730 ga_init2(&ga, 1, 100);
4731 ga_concat(&ga, (char_u *)"function(");
4732 if (fname != NULL)
4733 {
4734 ga_concat(&ga, fname);
4735 vim_free(fname);
4736 }
4737 if (pt != NULL && pt->pt_argc > 0)
4738 {
4739 ga_concat(&ga, (char_u *)", [");
4740 for (i = 0; i < pt->pt_argc; ++i)
4741 {
4742 if (i > 0)
4743 ga_concat(&ga, (char_u *)", ");
4744 ga_concat(&ga,
4745 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4746 vim_free(tf);
4747 }
4748 ga_concat(&ga, (char_u *)"]");
4749 }
4750 if (pt != NULL && pt->pt_dict != NULL)
4751 {
4752 typval_T dtv;
4753
4754 ga_concat(&ga, (char_u *)", ");
4755 dtv.v_type = VAR_DICT;
4756 dtv.vval.v_dict = pt->pt_dict;
4757 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4758 vim_free(tf);
4759 }
4760 ga_concat(&ga, (char_u *)")");
4761
4762 *tofree = ga.ga_data;
4763 r = *tofree;
4764 break;
4765 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004766
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004767 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004768 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004769 break;
4770
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004771 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004772 if (tv->vval.v_list == NULL)
4773 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004774 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004775 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004776 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004777 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004778 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4779 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004780 {
4781 *tofree = NULL;
4782 r = (char_u *)"[...]";
4783 }
4784 else
4785 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004786 int old_copyID = tv->vval.v_list->lv_copyID;
4787
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004788 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004789 *tofree = list2string(tv, copyID, restore_copyID);
4790 if (restore_copyID)
4791 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004792 r = *tofree;
4793 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004794 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004795
Bram Moolenaar8c711452005-01-14 21:53:12 +00004796 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004797 if (tv->vval.v_dict == NULL)
4798 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004799 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004800 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004801 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004802 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004803 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4804 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004805 {
4806 *tofree = NULL;
4807 r = (char_u *)"{...}";
4808 }
4809 else
4810 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004811 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004812
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004813 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004814 *tofree = dict2string(tv, copyID, restore_copyID);
4815 if (restore_copyID)
4816 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004817 r = *tofree;
4818 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004819 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004820
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004821 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004822 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004823 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004824 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004825 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004826 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004827 break;
4828
Bram Moolenaar835dc632016-02-07 14:27:38 +01004829 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004830 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004831 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004832 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004833 if (composite_val)
4834 {
4835 *tofree = string_quote(r, FALSE);
4836 r = *tofree;
4837 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004838 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004839
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004840 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004841#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004842 *tofree = NULL;
4843 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4844 r = numbuf;
4845 break;
4846#endif
4847
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004848 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004849 case VAR_SPECIAL:
4850 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004851 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004852 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004853 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004854
Bram Moolenaar8502c702014-06-17 12:51:16 +02004855 if (--recurse == 0)
4856 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004857 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004858}
4859
4860/*
4861 * Return a string with the string representation of a variable.
4862 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4863 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004864 * Does not put quotes around strings, as ":echo" displays values.
4865 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4866 * May return NULL.
4867 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004868 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004869echo_string(
4870 typval_T *tv,
4871 char_u **tofree,
4872 char_u *numbuf,
4873 int copyID)
4874{
4875 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4876}
4877
4878/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004879 * Return string "str" in ' quotes, doubling ' characters.
4880 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004881 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004882 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004883 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004884string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004885{
Bram Moolenaar33570922005-01-25 22:26:29 +00004886 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004887 char_u *p, *r, *s;
4888
Bram Moolenaar33570922005-01-25 22:26:29 +00004889 len = (function ? 13 : 3);
4890 if (str != NULL)
4891 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004892 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004893 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004894 if (*p == '\'')
4895 ++len;
4896 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004897 s = r = alloc(len);
4898 if (r != NULL)
4899 {
4900 if (function)
4901 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004902 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004903 r += 10;
4904 }
4905 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004906 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004907 if (str != NULL)
4908 for (p = str; *p != NUL; )
4909 {
4910 if (*p == '\'')
4911 *r++ = '\'';
4912 MB_COPY_CHAR(p, r);
4913 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004914 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004915 if (function)
4916 *r++ = ')';
4917 *r++ = NUL;
4918 }
4919 return s;
4920}
4921
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004922#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004923/*
4924 * Convert the string "text" to a floating point number.
4925 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4926 * this always uses a decimal point.
4927 * Returns the length of the text that was consumed.
4928 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004929 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004930string2float(
4931 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004932 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004933{
4934 char *s = (char *)text;
4935 float_T f;
4936
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004937 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004938 if (STRNICMP(text, "inf", 3) == 0)
4939 {
4940 *value = INFINITY;
4941 return 3;
4942 }
4943 if (STRNICMP(text, "-inf", 3) == 0)
4944 {
4945 *value = -INFINITY;
4946 return 4;
4947 }
4948 if (STRNICMP(text, "nan", 3) == 0)
4949 {
4950 *value = NAN;
4951 return 3;
4952 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004953 f = strtod(s, &s);
4954 *value = f;
4955 return (int)((char_u *)s - text);
4956}
4957#endif
4958
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004959/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004961 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004963 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004964var2fpos(
4965 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004966 int dollar_lnum, // TRUE when $ is last line
4967 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004969 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004971 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004973 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004974 if (varp->v_type == VAR_LIST)
4975 {
4976 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004977 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004978 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004979 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004980
4981 l = varp->vval.v_list;
4982 if (l == NULL)
4983 return NULL;
4984
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004985 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004986 pos.lnum = list_find_nr(l, 0L, &error);
4987 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004988 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004989 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004990
Bram Moolenaarec65d772020-08-20 22:29:12 +02004991 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004992 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004993 li = list_find(l, 1L);
4994 if (li != NULL && li->li_tv.v_type == VAR_STRING
4995 && li->li_tv.vval.v_string != NULL
4996 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02004997 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004998 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02004999 }
5000 else
5001 {
5002 pos.col = list_find_nr(l, 1L, &error);
5003 if (error)
5004 return NULL;
5005 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005006
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005007 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005008 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005009 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005010 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005011
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005012 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005013 pos.coladd = list_find_nr(l, 2L, &error);
5014 if (error)
5015 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005016
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005017 return &pos;
5018 }
5019
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005020 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005021 if (name == NULL)
5022 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005023 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005025 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005026 {
5027 if (VIsual_active)
5028 return &VIsual;
5029 return &curwin->w_cursor;
5030 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005031 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005033 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5035 return NULL;
5036 return pp;
5037 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005038
Bram Moolenaara5525202006-03-02 22:52:09 +00005039 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005040
Bram Moolenaar477933c2007-07-17 14:32:23 +00005041 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005042 {
5043 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005044 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005045 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005046 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005047 // In silent Ex mode topline is zero, but that's not a valid line
5048 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005049 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005050 return &pos;
5051 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005052 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005053 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005054 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005055 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005056 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005057 return &pos;
5058 }
5059 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005060 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005062 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 {
5064 pos.lnum = curbuf->b_ml.ml_line_count;
5065 pos.col = 0;
5066 }
5067 else
5068 {
5069 pos.lnum = curwin->w_cursor.lnum;
5070 pos.col = (colnr_T)STRLEN(ml_get_curline());
5071 }
5072 return &pos;
5073 }
5074 return NULL;
5075}
5076
5077/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005078 * Convert list in "arg" into a position and optional file number.
5079 * When "fnump" is NULL there is no file number, only 3 items.
5080 * Note that the column is passed on as-is, the caller may want to decrement
5081 * it to use 1 for the first column.
5082 * Return FAIL when conversion is not possible, doesn't check the position for
5083 * validity.
5084 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005085 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005086list2fpos(
5087 typval_T *arg,
5088 pos_T *posp,
5089 int *fnump,
5090 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005091{
5092 list_T *l = arg->vval.v_list;
5093 long i = 0;
5094 long n;
5095
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005096 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5097 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005098 if (arg->v_type != VAR_LIST
5099 || l == NULL
5100 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005101 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005102 return FAIL;
5103
5104 if (fnump != NULL)
5105 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005106 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005107 if (n < 0)
5108 return FAIL;
5109 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005110 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005111 *fnump = n;
5112 }
5113
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005114 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005115 if (n < 0)
5116 return FAIL;
5117 posp->lnum = n;
5118
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005119 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005120 if (n < 0)
5121 return FAIL;
5122 posp->col = n;
5123
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005124 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005125 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005126 posp->coladd = 0;
5127 else
5128 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005129
Bram Moolenaar493c1782014-05-28 14:34:46 +02005130 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005131 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005132
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005133 return OK;
5134}
5135
5136/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 * Get the length of an environment variable name.
5138 * Advance "arg" to the first character after the name.
5139 * Return 0 for error.
5140 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005141 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005142get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143{
5144 char_u *p;
5145 int len;
5146
5147 for (p = *arg; vim_isIDc(*p); ++p)
5148 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005149 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 return 0;
5151
5152 len = (int)(p - *arg);
5153 *arg = p;
5154 return len;
5155}
5156
5157/*
5158 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005159 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 * Return 0 if something is wrong.
5161 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005162 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005163get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164{
5165 char_u *p;
5166 int len;
5167
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005168 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005170 {
5171 if (*p == ':')
5172 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005173 // "s:" is start of "s:var", but "n:" is not and can be used in
5174 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005175 len = (int)(p - *arg);
5176 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5177 || len > 1)
5178 break;
5179 }
5180 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005181 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 return 0;
5183
5184 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005185 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186
5187 return len;
5188}
5189
5190/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005191 * Get the length of the name of a variable or function.
5192 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005194 * Return -1 if curly braces expansion failed.
5195 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 * If the name contains 'magic' {}'s, expand them and return the
5197 * expanded name in an allocated string via 'alias' - caller must free.
5198 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005199 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005200get_name_len(
5201 char_u **arg,
5202 char_u **alias,
5203 int evaluate,
5204 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205{
5206 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 char_u *p;
5208 char_u *expr_start;
5209 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005211 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212
5213 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5214 && (*arg)[2] == (int)KE_SNR)
5215 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005216 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 *arg += 3;
5218 return get_id_len(arg) + 3;
5219 }
5220 len = eval_fname_script(*arg);
5221 if (len > 0)
5222 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005223 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 *arg += len;
5225 }
5226
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005228 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005230 p = find_name_end(*arg, &expr_start, &expr_end,
5231 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 if (expr_start != NULL)
5233 {
5234 char_u *temp_string;
5235
5236 if (!evaluate)
5237 {
5238 len += (int)(p - *arg);
5239 *arg = skipwhite(p);
5240 return len;
5241 }
5242
5243 /*
5244 * Include any <SID> etc in the expanded string:
5245 * Thus the -len here.
5246 */
5247 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5248 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005249 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 *alias = temp_string;
5251 *arg = skipwhite(p);
5252 return (int)STRLEN(temp_string);
5253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254
5255 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005256 // Only give an error when there is something, otherwise it will be
5257 // reported at a higher level.
5258 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005259 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260
5261 return len;
5262}
5263
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005264/*
5265 * Find the end of a variable or function name, taking care of magic braces.
5266 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5267 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005268 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005269 * Return a pointer to just after the name. Equal to "arg" if there is no
5270 * valid name.
5271 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005272 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005273find_name_end(
5274 char_u *arg,
5275 char_u **expr_start,
5276 char_u **expr_end,
5277 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005279 int mb_nest = 0;
5280 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005282 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005283 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005285 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005287 *expr_start = NULL;
5288 *expr_end = NULL;
5289 }
5290
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005291 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005292 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5293 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005294 return arg;
5295
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005296 for (p = arg; *p != NUL
5297 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005298 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005299 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005300 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005301 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005302 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005303 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005304 if (*p == '\'')
5305 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005306 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005307 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005308 ;
5309 if (*p == NUL)
5310 break;
5311 }
5312 else if (*p == '"')
5313 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005314 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005315 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005316 if (*p == '\\' && p[1] != NUL)
5317 ++p;
5318 if (*p == NUL)
5319 break;
5320 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005321 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5322 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005323 // "s:" is start of "s:var", but "n:" is not and can be used in
5324 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005325 len = (int)(p - arg);
5326 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005327 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005328 break;
5329 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005330
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005331 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005333 if (*p == '[')
5334 ++br_nest;
5335 else if (*p == ']')
5336 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005338
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005339 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005341 if (*p == '{')
5342 {
5343 mb_nest++;
5344 if (expr_start != NULL && *expr_start == NULL)
5345 *expr_start = p;
5346 }
5347 else if (*p == '}')
5348 {
5349 mb_nest--;
5350 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5351 *expr_end = p;
5352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 }
5355
5356 return p;
5357}
5358
5359/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005360 * Expands out the 'magic' {}'s in a variable/function name.
5361 * Note that this can call itself recursively, to deal with
5362 * constructs like foo{bar}{baz}{bam}
5363 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5364 * "in_start" ^
5365 * "expr_start" ^
5366 * "expr_end" ^
5367 * "in_end" ^
5368 *
5369 * Returns a new allocated string, which the caller must free.
5370 * Returns NULL for failure.
5371 */
5372 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005373make_expanded_name(
5374 char_u *in_start,
5375 char_u *expr_start,
5376 char_u *expr_end,
5377 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005378{
5379 char_u c1;
5380 char_u *retval = NULL;
5381 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005382
5383 if (expr_end == NULL || in_end == NULL)
5384 return NULL;
5385 *expr_start = NUL;
5386 *expr_end = NUL;
5387 c1 = *in_end;
5388 *in_end = NUL;
5389
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005390 temp_result = eval_to_string(expr_start + 1, FALSE);
5391 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005392 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005393 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5394 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005395 if (retval != NULL)
5396 {
5397 STRCPY(retval, in_start);
5398 STRCAT(retval, temp_result);
5399 STRCAT(retval, expr_end + 1);
5400 }
5401 }
5402 vim_free(temp_result);
5403
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005404 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005405 *expr_start = '{';
5406 *expr_end = '}';
5407
5408 if (retval != NULL)
5409 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005410 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005411 if (expr_start != NULL)
5412 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005413 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005414 temp_result = make_expanded_name(retval, expr_start,
5415 expr_end, temp_result);
5416 vim_free(retval);
5417 retval = temp_result;
5418 }
5419 }
5420
5421 return retval;
5422}
5423
5424/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005426 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005428 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005429eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005431 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005432}
5433
5434/*
5435 * Return TRUE if character "c" can be used as the first character in a
5436 * variable or function name (excluding '{' and '}').
5437 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005438 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005439eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005440{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005441 return ASCII_ISALPHA(c) || c == '_';
5442}
5443
5444/*
5445 * Return TRUE if character "c" can be used as the first character of a
5446 * dictionary key.
5447 */
5448 int
5449eval_isdictc(int c)
5450{
5451 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452}
5453
5454/*
Bram Moolenaare3c37d82020-08-15 18:39:05 +02005455 * Return the character "str[index]" where "index" is the character index. If
5456 * "index" is out of range NULL is returned.
5457 */
5458 char_u *
5459char_from_string(char_u *str, varnumber_T index)
5460{
5461 size_t nbyte = 0;
5462 varnumber_T nchar = index;
5463 size_t slen;
5464
5465 if (str == NULL || index < 0)
5466 return NULL;
5467 slen = STRLEN(str);
5468 while (nchar > 0 && nbyte < slen)
5469 {
5470 nbyte += MB_CPTR2LEN(str + nbyte);
5471 --nchar;
5472 }
5473 if (nbyte >= slen)
5474 return NULL;
5475 return vim_strnsave(str + nbyte, MB_CPTR2LEN(str + nbyte));
5476}
5477
5478/*
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005479 * Get the byte index for character index "idx" in string "str" with length
5480 * "str_len".
5481 * If going over the end return "str_len".
5482 * If "idx" is negative count from the end, -1 is the last character.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005483 * When going over the start return -1.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005484 */
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005485 static long
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005486char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
5487{
5488 varnumber_T nchar = idx;
5489 size_t nbyte = 0;
5490
5491 if (nchar >= 0)
5492 {
5493 while (nchar > 0 && nbyte < str_len)
5494 {
5495 nbyte += MB_CPTR2LEN(str + nbyte);
5496 --nchar;
5497 }
5498 }
5499 else
5500 {
5501 nbyte = str_len;
5502 while (nchar < 0 && nbyte > 0)
5503 {
5504 --nbyte;
5505 nbyte -= mb_head_off(str, str + nbyte);
5506 ++nchar;
5507 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005508 if (nchar < 0)
5509 return -1;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005510 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005511 return (long)nbyte;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005512}
5513
5514/*
5515 * Return the slice "str[first:last]" using character indexes.
5516 * Return NULL when the result is empty.
5517 */
5518 char_u *
5519string_slice(char_u *str, varnumber_T first, varnumber_T last)
5520{
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005521 long start_byte, end_byte;
5522 size_t slen;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005523
5524 if (str == NULL)
5525 return NULL;
5526 slen = STRLEN(str);
5527 start_byte = char_idx2byte(str, slen, first);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005528 if (start_byte < 0)
5529 start_byte = 0; // first index very negative: use zero
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005530 if (last == -1)
Bram Moolenaar25660542020-08-28 16:38:11 +02005531 end_byte = (long)slen;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005532 else
5533 {
5534 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005535 if (end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005536 // end index is inclusive
5537 end_byte += MB_CPTR2LEN(str + end_byte);
5538 }
5539
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005540 if (start_byte >= (long)slen || end_byte <= start_byte)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005541 return NULL;
5542 return vim_strnsave(str + start_byte, end_byte - start_byte);
5543}
5544
5545/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005546 * Handle:
5547 * - expr[expr], expr[expr:expr] subscript
5548 * - ".name" lookup
5549 * - function call with Funcref variable: func(expr)
5550 * - method call: var->method()
5551 *
5552 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005553 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005554 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005555handle_subscript(
5556 char_u **arg,
5557 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005558 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005559 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005560{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005561 int evaluate = evalarg != NULL
5562 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005563 int ret = OK;
5564 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005565 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005566 int getnext;
5567 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005568
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005569 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005570 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005571 // When at the end of the line and ".name" or "->{" or "->X" follows in
5572 // the next line then consume the line break.
5573 p = eval_next_non_blank(*arg, evalarg, &getnext);
5574 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005575 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaar9d489562020-07-30 20:08:50 +02005576 || (p[0] == '-' && p[1] == '>'
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005577 && (p[2] == '{' || ASCII_ISALPHA(p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005578 {
5579 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005580 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005581 check_white = FALSE;
5582 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005583
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005584 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5585 || rettv->v_type == VAR_PARTIAL))
5586 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005587 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005588 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5589 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005590
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005591 // Stop the expression evaluation when immediately aborting on
5592 // error, or when an interrupt occurred or an exception was thrown
5593 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005594 if (aborting())
5595 {
5596 if (ret == OK)
5597 clear_tv(rettv);
5598 ret = FAIL;
5599 }
5600 dict_unref(selfdict);
5601 selfdict = NULL;
5602 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005603 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005604 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02005605 *arg = p;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005606 if (ret == OK)
5607 {
5608 if ((*arg)[2] == '{')
5609 // expr->{lambda}()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005610 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005611 else
5612 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005613 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005614 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005615 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005616 // "." is ".name" lookup when we found a dict or when evaluating and
5617 // scriptversion is at least 2, where string concatenation is "..".
5618 else if (**arg == '['
5619 || (**arg == '.' && (rettv->v_type == VAR_DICT
5620 || (!evaluate
5621 && (*arg)[1] != '.'
5622 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005623 {
5624 dict_unref(selfdict);
5625 if (rettv->v_type == VAR_DICT)
5626 {
5627 selfdict = rettv->vval.v_dict;
5628 if (selfdict != NULL)
5629 ++selfdict->dv_refcount;
5630 }
5631 else
5632 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005633 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005634 {
5635 clear_tv(rettv);
5636 ret = FAIL;
5637 }
5638 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005639 else
5640 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005641 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005642
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005643 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5644 // Don't do this when "Func" is already a partial that was bound
5645 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005646 if (selfdict != NULL
5647 && (rettv->v_type == VAR_FUNC
5648 || (rettv->v_type == VAR_PARTIAL
5649 && (rettv->vval.v_partial->pt_auto
5650 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005651 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005652
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005653 dict_unref(selfdict);
5654 return ret;
5655}
5656
5657/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005658 * Make a copy of an item.
5659 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005660 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5661 * reference to an already copied list/dict can be used.
5662 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005663 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005664 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005665item_copy(
5666 typval_T *from,
5667 typval_T *to,
5668 int deep,
5669 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005670{
5671 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005672 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005673
Bram Moolenaar33570922005-01-25 22:26:29 +00005674 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005675 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005676 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005677 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005678 }
5679 ++recurse;
5680
5681 switch (from->v_type)
5682 {
5683 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005684 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005685 case VAR_STRING:
5686 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005687 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005688 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005689 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005690 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005691 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005692 copy_tv(from, to);
5693 break;
5694 case VAR_LIST:
5695 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005696 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005697 if (from->vval.v_list == NULL)
5698 to->vval.v_list = NULL;
5699 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5700 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005701 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005702 to->vval.v_list = from->vval.v_list->lv_copylist;
5703 ++to->vval.v_list->lv_refcount;
5704 }
5705 else
5706 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5707 if (to->vval.v_list == NULL)
5708 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005709 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005710 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005711 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005712 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005713 case VAR_DICT:
5714 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005715 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005716 if (from->vval.v_dict == NULL)
5717 to->vval.v_dict = NULL;
5718 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5719 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005720 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005721 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5722 ++to->vval.v_dict->dv_refcount;
5723 }
5724 else
5725 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5726 if (to->vval.v_dict == NULL)
5727 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005728 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005729 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005730 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005731 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005732 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005733 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005734 }
5735 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005736 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005737}
5738
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005739 void
5740echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5741{
5742 char_u *tofree;
5743 char_u numbuf[NUMBUFLEN];
5744 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5745
5746 if (*atstart)
5747 {
5748 *atstart = FALSE;
5749 // Call msg_start() after eval1(), evaluating the expression
5750 // may cause a message to appear.
5751 if (with_space)
5752 {
5753 // Mark the saved text as finishing the line, so that what
5754 // follows is displayed on a new line when scrolling back
5755 // at the more prompt.
5756 msg_sb_eol();
5757 msg_start();
5758 }
5759 }
5760 else if (with_space)
5761 msg_puts_attr(" ", echo_attr);
5762
5763 if (p != NULL)
5764 for ( ; *p != NUL && !got_int; ++p)
5765 {
5766 if (*p == '\n' || *p == '\r' || *p == TAB)
5767 {
5768 if (*p != TAB && *needclr)
5769 {
5770 // remove any text still there from the command
5771 msg_clr_eos();
5772 *needclr = FALSE;
5773 }
5774 msg_putchar_attr(*p, echo_attr);
5775 }
5776 else
5777 {
5778 if (has_mbyte)
5779 {
5780 int i = (*mb_ptr2len)(p);
5781
5782 (void)msg_outtrans_len_attr(p, i, echo_attr);
5783 p += i - 1;
5784 }
5785 else
5786 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5787 }
5788 }
5789 vim_free(tofree);
5790}
5791
Bram Moolenaare9a41262005-01-15 22:18:47 +00005792/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 * ":echo expr1 ..." print each argument separated with a space, add a
5794 * newline at the end.
5795 * ":echon expr1 ..." print each argument plain.
5796 */
5797 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005798ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799{
5800 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005801 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 char_u *p;
5803 int needclr = TRUE;
5804 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01005805 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005806 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005807 evalarg_T evalarg;
5808
Bram Moolenaare707c882020-07-01 13:07:37 +02005809 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810
5811 if (eap->skip)
5812 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005813 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005815 // If eval1() causes an error message the text from the command may
5816 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005817 need_clr_eos = needclr;
5818
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005820 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 {
5822 /*
5823 * Report the invalid expression unless the expression evaluation
5824 * has been cancelled due to an aborting error, an interrupt, or an
5825 * exception.
5826 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005827 if (!aborting() && did_emsg == did_emsg_before
5828 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005829 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005830 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 break;
5832 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005833 need_clr_eos = FALSE;
5834
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005836 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005837
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005838 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 arg = skipwhite(arg);
5840 }
5841 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02005842 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843
5844 if (eap->skip)
5845 --emsg_skip;
5846 else
5847 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005848 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 if (needclr)
5850 msg_clr_eos();
5851 if (eap->cmdidx == CMD_echo)
5852 msg_end();
5853 }
5854}
5855
5856/*
5857 * ":echohl {name}".
5858 */
5859 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005860ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005862 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863}
5864
5865/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005866 * Returns the :echo attribute
5867 */
5868 int
5869get_echo_attr(void)
5870{
5871 return echo_attr;
5872}
5873
5874/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 * ":execute expr1 ..." execute the result of an expression.
5876 * ":echomsg expr1 ..." Print a message
5877 * ":echoerr expr1 ..." Print an error
5878 * Each gets spaces around each argument and a newline at the end for
5879 * echo commands
5880 */
5881 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005882ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883{
5884 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005885 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 int ret = OK;
5887 char_u *p;
5888 garray_T ga;
5889 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890
5891 ga_init2(&ga, 1, 80);
5892
5893 if (eap->skip)
5894 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02005895 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02005897 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01005898 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900
5901 if (!eap->skip)
5902 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005903 char_u buf[NUMBUFLEN];
5904
5905 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01005906 {
5907 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
5908 {
5909 emsg(_(e_inval_string));
5910 p = NULL;
5911 }
5912 else
5913 p = tv_get_string_buf(&rettv, buf);
5914 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005915 else
5916 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005917 if (p == NULL)
5918 {
5919 clear_tv(&rettv);
5920 ret = FAIL;
5921 break;
5922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 len = (int)STRLEN(p);
5924 if (ga_grow(&ga, len + 2) == FAIL)
5925 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005926 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 ret = FAIL;
5928 break;
5929 }
5930 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 ga.ga_len += len;
5934 }
5935
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005936 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 arg = skipwhite(arg);
5938 }
5939
5940 if (ret != FAIL && ga.ga_data != NULL)
5941 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005942 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
5943 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005944 // Mark the already saved text as finishing the line, so that what
5945 // follows is displayed on a new line when scrolling back at the
5946 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005947 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005948 }
5949
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005951 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005952 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005953 out_flush();
5954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 else if (eap->cmdidx == CMD_echoerr)
5956 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005957 int save_did_emsg = did_emsg;
5958
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005959 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005960 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 if (!force_abort)
5962 did_emsg = save_did_emsg;
5963 }
5964 else if (eap->cmdidx == CMD_execute)
5965 do_cmdline((char_u *)ga.ga_data,
5966 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
5967 }
5968
5969 ga_clear(&ga);
5970
5971 if (eap->skip)
5972 --emsg_skip;
5973
5974 eap->nextcmd = check_nextcmd(arg);
5975}
5976
5977/*
5978 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
5979 * "arg" points to the "&" or '+' when called, to "option" when returning.
5980 * Returns NULL when no option name found. Otherwise pointer to the char
5981 * after the option name.
5982 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005983 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005984find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985{
5986 char_u *p = *arg;
5987
5988 ++p;
5989 if (*p == 'g' && p[1] == ':')
5990 {
5991 *opt_flags = OPT_GLOBAL;
5992 p += 2;
5993 }
5994 else if (*p == 'l' && p[1] == ':')
5995 {
5996 *opt_flags = OPT_LOCAL;
5997 p += 2;
5998 }
5999 else
6000 *opt_flags = 0;
6001
6002 if (!ASCII_ISALPHA(*p))
6003 return NULL;
6004 *arg = p;
6005
6006 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006007 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 else
6009 while (ASCII_ISALPHA(*p))
6010 ++p;
6011 return p;
6012}
6013
6014/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006015 * Display script name where an item was last set.
6016 * Should only be invoked when 'verbose' is non-zero.
6017 */
6018 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006019last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006020{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006021 char_u *p;
6022
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006023 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006024 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006025 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006026 if (p != NULL)
6027 {
6028 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006029 msg_puts(_("\n\tLast set from "));
6030 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006031 if (script_ctx.sc_lnum > 0)
6032 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006033 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006034 msg_outnum((long)script_ctx.sc_lnum);
6035 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006036 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006037 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006038 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006039 }
6040}
6041
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006042#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043
6044/*
6045 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006046 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 * "flags" can be "g" to do a global substitute.
6048 * Returns an allocated string, NULL for error.
6049 */
6050 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006051do_string_sub(
6052 char_u *str,
6053 char_u *pat,
6054 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006055 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006056 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057{
6058 int sublen;
6059 regmatch_T regmatch;
6060 int i;
6061 int do_all;
6062 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006063 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 garray_T ga;
6065 char_u *ret;
6066 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006067 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006069 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006071 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072
6073 ga_init2(&ga, 1, 200);
6074
6075 do_all = (flags[0] == 'g');
6076
6077 regmatch.rm_ic = p_ic;
6078 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6079 if (regmatch.regprog != NULL)
6080 {
6081 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006082 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6084 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006085 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006086 if (regmatch.startp[0] == regmatch.endp[0])
6087 {
6088 if (zero_width == regmatch.startp[0])
6089 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006090 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006091 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006092 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6093 (size_t)i);
6094 ga.ga_len += i;
6095 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006096 continue;
6097 }
6098 zero_width = regmatch.startp[0];
6099 }
6100
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 /*
6102 * Get some space for a temporary buffer to do the substitution
6103 * into. It will contain:
6104 * - The text up to where the match is.
6105 * - The substituted text.
6106 * - The text after the match.
6107 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006108 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006109 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6111 {
6112 ga_clear(&ga);
6113 break;
6114 }
6115
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006116 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 i = (int)(regmatch.startp[0] - tail);
6118 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006119 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006120 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 + ga.ga_len + i, TRUE, TRUE, FALSE);
6122 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006123 tail = regmatch.endp[0];
6124 if (*tail == NUL)
6125 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 if (!do_all)
6127 break;
6128 }
6129
6130 if (ga.ga_data != NULL)
6131 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6132
Bram Moolenaar473de612013-06-08 18:19:48 +02006133 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 }
6135
6136 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6137 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006138 if (p_cpo == empty_option)
6139 p_cpo = save_cpo;
6140 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006141 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006142 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143
6144 return ret;
6145}