blob: 6ce2ebf68df3c2a23dd36bf66ac150881a6ca24b [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 Moolenaarb544f3c2017-02-23 19:03:28 +0100658 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000659 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200660 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
661 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000662 */
663 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100664call_func_retstr(
665 char_u *func,
666 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200667 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000668{
669 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000670 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000671
Bram Moolenaarded27a12018-08-01 19:06:03 +0200672 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000673 return NULL;
674
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100675 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000676 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 return retval;
678}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000679
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000680/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100681 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200682 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
683 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000684 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000685 */
686 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100687call_func_retlist(
688 char_u *func,
689 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200690 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000691{
692 typval_T rettv;
693
Bram Moolenaarded27a12018-08-01 19:06:03 +0200694 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000695 return NULL;
696
697 if (rettv.v_type != VAR_LIST)
698 {
699 clear_tv(&rettv);
700 return NULL;
701 }
702
703 return rettv.vval.v_list;
704}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706#ifdef FEAT_FOLDING
707/*
708 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
709 * it in "*cp". Doesn't give error messages.
710 */
711 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100712eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713{
Bram Moolenaar33570922005-01-25 22:26:29 +0000714 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200715 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000717 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
718 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719
720 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000721 if (use_sandbox)
722 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200723 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200725 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 retval = 0;
727 else
728 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100729 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000730 if (tv.v_type == VAR_NUMBER)
731 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000732 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 retval = 0;
734 else
735 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100736 // If the result is a string, check if there is a non-digit before
737 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000738 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 if (!VIM_ISDIGIT(*s) && *s != '-')
740 *cp = *s++;
741 retval = atol((char *)s);
742 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000743 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 }
745 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000746 if (use_sandbox)
747 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200748 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200749 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200751 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752}
753#endif
754
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000756 * Get an lval: variable, Dict item or List item that can be assigned a value
757 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
758 * "name.key", "name.key[expr]" etc.
759 * Indexing only works if "name" is an existing List or Dictionary.
760 * "name" points to the start of the name.
761 * If "rettv" is not NULL it points to the value to be assigned.
762 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
763 * wrong; must end in space or cmd separator.
764 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100765 * flags:
766 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100767 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100768 * GLV_NO_AUTOLOAD: do not use script autoloading
769 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000770 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000771 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000772 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000773 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200774 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100775get_lval(
776 char_u *name,
777 typval_T *rettv,
778 lval_T *lp,
779 int unlet,
780 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100781 int flags, // GLV_ values
782 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000783{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000784 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000785 char_u *expr_start, *expr_end;
786 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000787 dictitem_T *v;
788 typval_T var1;
789 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000790 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000791 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000792 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000793 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100794 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100795 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000796
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100797 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200798 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000799
800 if (skip)
801 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100802 // When skipping just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000803 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200804 lp->ll_name_end = find_name_end(name, NULL, NULL,
805 FNE_INCL_BR | fne_flags);
806 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000807 }
808
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100809 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000810 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100811 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000812 if (expr_start != NULL)
813 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100814 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100815 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000816 && *p != '[' && *p != '.')
817 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200818 semsg(_(e_trailing_arg), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000819 return NULL;
820 }
821
822 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
823 if (lp->ll_exp_name == NULL)
824 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100825 // Report an invalid expression in braces, unless the
826 // expression evaluation has been cancelled due to an
827 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000828 if (!aborting() && !quiet)
829 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000830 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100831 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000832 return NULL;
833 }
834 }
835 lp->ll_name = lp->ll_exp_name;
836 }
837 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100838 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000839 lp->ll_name = name;
840
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200841 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100842 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200843 // "a: type" is declaring variable "a" with a type, not "a:".
844 if (p == name + 2 && p[-1] == ':')
845 {
846 --p;
847 lp->ll_name_end = p;
848 }
849 if (*p == ':')
850 {
851 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
852 char_u *tp = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100853
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200854 // parse the type after the name
855 lp->ll_type = parse_type(&tp, &si->sn_type_list);
856 lp->ll_name_end = tp;
857 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100858 }
859 }
860
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100861 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000862 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
863 return p;
864
865 cc = *p;
866 *p = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100867 // Only pass &ht when we would write to the variable, it prevents autoload
868 // as well.
Bram Moolenaar6e65d592017-12-07 22:11:27 +0100869 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
870 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000871 if (v == NULL && !quiet)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200872 semsg(_(e_undefined_variable_str), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000873 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000874 if (v == NULL)
875 return NULL;
876
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000877 /*
878 * Loop until no more [idx] or .key is following.
879 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000880 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100881 var1.v_type = VAR_UNKNOWN;
882 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000883 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000884 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000885 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
Bram Moolenaar81ed4962020-09-23 15:56:50 +0200886 && !(lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100887 && !(lp->ll_tv->v_type == VAR_BLOB
888 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000889 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000890 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100891 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000892 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000893 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000894 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000895 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000896 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100897 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000898 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000899 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000900
Bram Moolenaar10c65862020-10-08 21:16:42 +0200901 if (in_vim9script() && lp->ll_valtype == NULL
902 && lp->ll_tv == &v->di_tv
903 && ht != NULL && ht == get_script_local_ht())
904 {
905 svar_T *sv = find_typval_in_script(lp->ll_tv);
906
907 // Vim9 script local variable: get the type
908 if (sv != NULL)
909 lp->ll_valtype = sv->sv_type;
910 }
911
Bram Moolenaar8c711452005-01-14 21:53:12 +0000912 len = -1;
913 if (*p == '.')
914 {
915 key = p + 1;
916 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
917 ;
918 if (len == 0)
919 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000920 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100921 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000922 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000923 }
924 p = key + len;
925 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000926 else
927 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100928 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000929 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000930 if (*p == ':')
931 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000932 else
933 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000934 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200935 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000936 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100937 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000938 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100939 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000940 clear_tv(&var1);
941 return NULL;
942 }
Bram Moolenaar6a250262020-08-04 15:53:01 +0200943 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000944 }
945
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100946 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000947 if (*p == ':')
948 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000949 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000950 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000951 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +0200952 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100953 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000954 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000955 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100956 if (rettv != NULL
957 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100958 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100959 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100960 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +0000961 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000962 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100963 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100964 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000965 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000966 }
967 p = skipwhite(p + 1);
968 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000969 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000970 else
971 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000972 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200973 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200974 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000975 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100976 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000977 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000978 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100979 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000980 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100981 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100982 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000983 clear_tv(&var2);
984 return NULL;
985 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000986 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000987 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000988 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000989 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000990 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000991
Bram Moolenaar8c711452005-01-14 21:53:12 +0000992 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000993 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000994 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100995 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100996 clear_tv(&var1);
997 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000998 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000999 }
1000
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001001 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001002 ++p;
1003 }
1004
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001005 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001006 {
1007 if (len == -1)
1008 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001009 // "[key]": get key from "var1"
1010 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001011 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001012 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001013 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001014 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001015 }
1016 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001017 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001018
1019 // a NULL dict is equivalent with an empty dict
1020 if (lp->ll_tv->vval.v_dict == NULL)
1021 {
1022 lp->ll_tv->vval.v_dict = dict_alloc();
1023 if (lp->ll_tv->vval.v_dict == NULL)
1024 {
1025 clear_tv(&var1);
1026 return NULL;
1027 }
1028 ++lp->ll_tv->vval.v_dict->dv_refcount;
1029 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001030 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001031
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001032 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001033
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001034 // When assigning to a scope dictionary check that a function and
1035 // variable name is valid (only variable name unless it is l: or
1036 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001037 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001038 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001039 int prevval;
1040 int wrong;
1041
1042 if (len != -1)
1043 {
1044 prevval = key[len];
1045 key[len] = NUL;
1046 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001047 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001048 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001049 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1050 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001051 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001052 || !valid_varname(key);
1053 if (len != -1)
1054 key[len] = prevval;
1055 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001056 {
1057 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001058 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001059 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001060 }
1061
Bram Moolenaar10c65862020-10-08 21:16:42 +02001062 if (lp->ll_valtype != NULL)
1063 // use the type of the member
1064 lp->ll_valtype = lp->ll_valtype->tt_member;
1065
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001066 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001067 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001068 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001069 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001070 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001071 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001072 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001073 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001074 return NULL;
1075 }
1076
Bram Moolenaar31b81602019-02-10 22:14:27 +01001077 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001078 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001079 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001080 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001081 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001082 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001083 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001084 }
1085 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001086 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001087 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001088 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001089 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001090 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001091 p = NULL;
1092 break;
1093 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001094 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001095 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001096 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1097 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001098 {
1099 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001100 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001101 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001102
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001103 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001104 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001105 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001106 else if (lp->ll_tv->v_type == VAR_BLOB)
1107 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001108 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1109
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001110 /*
1111 * Get the number and item for the only or first index of the List.
1112 */
1113 if (empty1)
1114 lp->ll_n1 = 0;
1115 else
1116 // is number or string
1117 lp->ll_n1 = (long)tv_get_number(&var1);
1118 clear_tv(&var1);
1119
1120 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001121 || lp->ll_n1 > bloblen
1122 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001123 {
1124 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001125 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001126 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001127 return NULL;
1128 }
1129 if (lp->ll_range && !lp->ll_empty2)
1130 {
1131 lp->ll_n2 = (long)tv_get_number(&var2);
1132 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001133 if (lp->ll_n2 < 0
1134 || lp->ll_n2 >= bloblen
1135 || lp->ll_n2 < lp->ll_n1)
1136 {
1137 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001138 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001139 return NULL;
1140 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001141 }
1142 lp->ll_blob = lp->ll_tv->vval.v_blob;
1143 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001144 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001145 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001146 else
1147 {
1148 /*
1149 * Get the number and item for the only or first index of the List.
1150 */
1151 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001152 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001153 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001154 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001155 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001156 clear_tv(&var1);
1157
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001158 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001159 lp->ll_list = lp->ll_tv->vval.v_list;
1160 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1161 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001162 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001163 if (lp->ll_n1 < 0)
1164 {
1165 lp->ll_n1 = 0;
1166 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1167 }
1168 }
1169 if (lp->ll_li == NULL)
1170 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001171 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001172 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001173 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001174 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001175 }
1176
Bram Moolenaar10c65862020-10-08 21:16:42 +02001177 if (lp->ll_valtype != NULL)
1178 // use the type of the member
1179 lp->ll_valtype = lp->ll_valtype->tt_member;
1180
Bram Moolenaar8c711452005-01-14 21:53:12 +00001181 /*
1182 * May need to find the item or absolute index for the second
1183 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001184 * When no index given: "lp->ll_empty2" is TRUE.
1185 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001186 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001187 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001188 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001189 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001190 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001191 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001192 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001193 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001194 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001195 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001196 {
1197 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001198 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001199 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001200 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001201 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001202 }
1203
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001204 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001205 if (lp->ll_n1 < 0)
1206 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1207 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001208 {
1209 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001210 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001211 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001212 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001213 }
1214
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001215 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001216 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001217 }
1218
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001219 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001220 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001221 return p;
1222}
1223
1224/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001225 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001226 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001227 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001228clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001229{
1230 vim_free(lp->ll_exp_name);
1231 vim_free(lp->ll_newkey);
1232}
1233
1234/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001235 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001236 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001237 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1238 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001239 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001240 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001241set_var_lval(
1242 lval_T *lp,
1243 char_u *endp,
1244 typval_T *rettv,
1245 int copy,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001246 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar7454a062016-01-30 15:14:10 +01001247 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001248{
1249 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001250 listitem_T *ri;
1251 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001252
1253 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001254 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001255 cc = *endp;
1256 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001257 if (lp->ll_blob != NULL)
1258 {
1259 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001260
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001261 if (op != NULL && *op != '=')
1262 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001263 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001264 return;
1265 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001266 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001267 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001268
1269 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1270 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001271 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001272
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001273 if (lp->ll_empty2)
1274 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1275
1276 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001277 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001278 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001279 return;
1280 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001281 if (lp->ll_empty2)
1282 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001283
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001284 ir = 0;
1285 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1286 blob_set(lp->ll_blob, il,
1287 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001288 }
1289 else
1290 {
1291 val = (int)tv_get_number_chk(rettv, &error);
1292 if (!error)
1293 {
1294 garray_T *gap = &lp->ll_blob->bv_ga;
1295
1296 // Allow for appending a byte. Setting a byte beyond
1297 // the end is an error otherwise.
1298 if (lp->ll_n1 < gap->ga_len
1299 || (lp->ll_n1 == gap->ga_len
1300 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1301 {
1302 blob_set(lp->ll_blob, lp->ll_n1, val);
1303 if (lp->ll_n1 == gap->ga_len)
1304 ++gap->ga_len;
1305 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001306 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001307 }
1308 }
1309 }
1310 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001311 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001312 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001313
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001314 if (flags & ASSIGN_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001315 {
1316 emsg(_(e_cannot_mod));
1317 *endp = cc;
1318 return;
1319 }
1320
Bram Moolenaarff697e62019-02-12 22:28:33 +01001321 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001322 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001323 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar79518e22017-02-17 16:31:35 +01001324 &tv, &di, TRUE, FALSE) == OK)
1325 {
1326 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001327 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1328 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001329 && tv_op(&tv, rettv, op) == OK)
1330 set_var(lp->ll_name, &tv, FALSE);
1331 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001332 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001333 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001334 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001335 {
1336 if (lp->ll_type != NULL
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001337 && check_typval_type(lp->ll_type, rettv, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001338 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001339 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001340 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001341 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001342 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001343 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001344 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001345 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001346 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001347 else if (lp->ll_range)
1348 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001349 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001350 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001351
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001352 if (flags & ASSIGN_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001353 {
1354 emsg(_("E996: Cannot lock a range"));
1355 return;
1356 }
1357
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001358 /*
1359 * Check whether any of the list items is locked
1360 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001361 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001362 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001363 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001364 return;
1365 ri = ri->li_next;
1366 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1367 break;
1368 ll_li = ll_li->li_next;
1369 ++ll_n1;
1370 }
1371
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001372 /*
1373 * Assign the List values to the list items.
1374 */
1375 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001376 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001377 if (op != NULL && *op != '=')
1378 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1379 else
1380 {
1381 clear_tv(&lp->ll_li->li_tv);
1382 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1383 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001384 ri = ri->li_next;
1385 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1386 break;
1387 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001388 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001389 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001390 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001391 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001392 ri = NULL;
1393 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001394 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001395 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001396 lp->ll_li = lp->ll_li->li_next;
1397 ++lp->ll_n1;
1398 }
1399 if (ri != NULL)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001400 emsg(_(e_list_value_has_more_items_than_targets));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001401 else if (lp->ll_empty2
1402 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001403 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001404 emsg(_(e_list_value_does_not_have_enough_items));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001405 }
1406 else
1407 {
1408 /*
1409 * Assign to a List or Dictionary item.
1410 */
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001411 if (flags & ASSIGN_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001412 {
1413 emsg(_("E996: Cannot lock a list or dict"));
1414 return;
1415 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001416
1417 if (lp->ll_valtype != NULL
1418 && check_typval_type(lp->ll_valtype, rettv, 0) == FAIL)
1419 return;
1420
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001421 if (lp->ll_newkey != NULL)
1422 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001423 if (op != NULL && *op != '=')
1424 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001425 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001426 return;
1427 }
1428
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001429 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001430 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001431 if (di == NULL)
1432 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001433 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1434 {
1435 vim_free(di);
1436 return;
1437 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001438 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001439 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001440 else if (op != NULL && *op != '=')
1441 {
1442 tv_op(lp->ll_tv, rettv, op);
1443 return;
1444 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001445 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001446 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001447
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001448 /*
1449 * Assign the value to the variable or list item.
1450 */
1451 if (copy)
1452 copy_tv(rettv, lp->ll_tv);
1453 else
1454 {
1455 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001456 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001457 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001458 }
1459 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001460}
1461
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001462/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001463 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1464 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001465 * Returns OK or FAIL.
1466 */
1467 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001468tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001469{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001470 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001471 char_u numbuf[NUMBUFLEN];
1472 char_u *s;
1473
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001474 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001475 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001476 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001477 {
1478 switch (tv1->v_type)
1479 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001480 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001481 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001482 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001483 case VAR_DICT:
1484 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001485 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001486 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001487 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001488 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001489 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001490 break;
1491
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001492 case VAR_BLOB:
1493 if (*op != '+' || tv2->v_type != VAR_BLOB)
1494 break;
1495 // BLOB += BLOB
1496 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1497 {
1498 blob_T *b1 = tv1->vval.v_blob;
1499 blob_T *b2 = tv2->vval.v_blob;
1500 int i, len = blob_len(b2);
1501 for (i = 0; i < len; i++)
1502 ga_append(&b1->bv_ga, blob_get(b2, i));
1503 }
1504 return OK;
1505
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001506 case VAR_LIST:
1507 if (*op != '+' || tv2->v_type != VAR_LIST)
1508 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001509 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001510 if (tv2->vval.v_list != NULL)
1511 {
1512 if (tv1->vval.v_list == NULL)
1513 {
1514 tv1->vval.v_list = tv2->vval.v_list;
1515 ++tv1->vval.v_list->lv_refcount;
1516 }
1517 else
1518 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1519 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001520 return OK;
1521
1522 case VAR_NUMBER:
1523 case VAR_STRING:
1524 if (tv2->v_type == VAR_LIST)
1525 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001526 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001527 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001528 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001529 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001530#ifdef FEAT_FLOAT
1531 if (tv2->v_type == VAR_FLOAT)
1532 {
1533 float_T f = n;
1534
Bram Moolenaarff697e62019-02-12 22:28:33 +01001535 if (*op == '%')
1536 break;
1537 switch (*op)
1538 {
1539 case '+': f += tv2->vval.v_float; break;
1540 case '-': f -= tv2->vval.v_float; break;
1541 case '*': f *= tv2->vval.v_float; break;
1542 case '/': f /= tv2->vval.v_float; break;
1543 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001544 clear_tv(tv1);
1545 tv1->v_type = VAR_FLOAT;
1546 tv1->vval.v_float = f;
1547 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001548 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001549#endif
1550 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001551 switch (*op)
1552 {
1553 case '+': n += tv_get_number(tv2); break;
1554 case '-': n -= tv_get_number(tv2); break;
1555 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001556 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1557 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001558 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001559 clear_tv(tv1);
1560 tv1->v_type = VAR_NUMBER;
1561 tv1->vval.v_number = n;
1562 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001563 }
1564 else
1565 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001566 if (tv2->v_type == VAR_FLOAT)
1567 break;
1568
Bram Moolenaarff697e62019-02-12 22:28:33 +01001569 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001570 s = tv_get_string(tv1);
1571 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001572 clear_tv(tv1);
1573 tv1->v_type = VAR_STRING;
1574 tv1->vval.v_string = s;
1575 }
1576 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001577
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001578 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001579#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001580 {
1581 float_T f;
1582
Bram Moolenaarff697e62019-02-12 22:28:33 +01001583 if (*op == '%' || *op == '.'
1584 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001585 && tv2->v_type != VAR_NUMBER
1586 && tv2->v_type != VAR_STRING))
1587 break;
1588 if (tv2->v_type == VAR_FLOAT)
1589 f = tv2->vval.v_float;
1590 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001591 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001592 switch (*op)
1593 {
1594 case '+': tv1->vval.v_float += f; break;
1595 case '-': tv1->vval.v_float -= f; break;
1596 case '*': tv1->vval.v_float *= f; break;
1597 case '/': tv1->vval.v_float /= f; break;
1598 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001599 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001600#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001601 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001602 }
1603 }
1604
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001605 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001606 return FAIL;
1607}
1608
1609/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001610 * Evaluate the expression used in a ":for var in expr" command.
1611 * "arg" points to "var".
1612 * Set "*errp" to TRUE for an error, FALSE otherwise;
1613 * Return a pointer that holds the info. Null when there is an error.
1614 */
1615 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001616eval_for_line(
1617 char_u *arg,
1618 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001619 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001620 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001621{
Bram Moolenaar33570922005-01-25 22:26:29 +00001622 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001623 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001624 typval_T tv;
1625 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001626 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001627
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001628 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001629
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001630 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001631 if (fi == NULL)
1632 return NULL;
1633
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001634 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001635 if (expr == NULL)
1636 return fi;
1637
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001638 expr = skipwhite_and_linebreak(expr, evalarg);
1639 if (expr[0] != 'i' || expr[1] != 'n'
1640 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001641 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001642 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001643 return fi;
1644 }
1645
1646 if (skip)
1647 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001648 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1649 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001650 {
1651 *errp = FALSE;
1652 if (!skip)
1653 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001654 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001655 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001656 l = tv.vval.v_list;
1657 if (l == NULL)
1658 {
1659 // a null list is like an empty list: do nothing
1660 clear_tv(&tv);
1661 }
1662 else
1663 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001664 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001665 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001666
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001667 // No need to increment the refcount, it's already set for
1668 // the list being used in "tv".
1669 fi->fi_list = l;
1670 list_add_watch(l, &fi->fi_lw);
1671 fi->fi_lw.lw_item = l->lv_first;
1672 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001673 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001674 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001675 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001676 fi->fi_bi = 0;
1677 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001678 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001679 typval_T btv;
1680
1681 // Make a copy, so that the iteration still works when the
1682 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001683 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001684 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001685 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001686 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001687 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001688 else
1689 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001690 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001691 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001692 }
1693 }
1694 }
1695 if (skip)
1696 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001697 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001698
1699 return fi;
1700}
1701
1702/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001703 * Used when looping over a :for line, skip the "in expr" part.
1704 */
1705 void
1706skip_for_lines(void *fi_void, evalarg_T *evalarg)
1707{
1708 forinfo_T *fi = (forinfo_T *)fi_void;
1709 int i;
1710
1711 for (i = 0; i < fi->fi_break_count; ++i)
1712 eval_next_line(evalarg);
1713}
1714
1715/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001716 * Use the first item in a ":for" list. Advance to the next.
1717 * Assign the values to the variable (list). "arg" points to the first one.
1718 * Return TRUE when a valid item was found, FALSE when at end of list or
1719 * something wrong.
1720 */
1721 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001722next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001723{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001724 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001725 int result;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001726 int flag = in_vim9script() ? ASSIGN_NO_DECL : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001727 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001728
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001729 if (fi->fi_blob != NULL)
1730 {
1731 typval_T tv;
1732
1733 if (fi->fi_bi >= blob_len(fi->fi_blob))
1734 return FALSE;
1735 tv.v_type = VAR_NUMBER;
1736 tv.v_lock = VAR_FIXED;
1737 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1738 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001739 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001740 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001741 }
1742
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001743 item = fi->fi_lw.lw_item;
1744 if (item == NULL)
1745 result = FALSE;
1746 else
1747 {
1748 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001749 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001750 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001751 }
1752 return result;
1753}
1754
1755/*
1756 * Free the structure used to store info used by ":for".
1757 */
1758 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001759free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001760{
Bram Moolenaar33570922005-01-25 22:26:29 +00001761 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001762
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001763 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001764 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001765 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001766 list_unref(fi->fi_list);
1767 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001768 if (fi != NULL && fi->fi_blob != NULL)
1769 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001770 vim_free(fi);
1771}
1772
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001774set_context_for_expression(
1775 expand_T *xp,
1776 char_u *arg,
1777 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001779 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001781 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001783 if (cmdidx == CMD_let || cmdidx == CMD_var
1784 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001785 {
1786 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001787 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001788 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001789 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001790 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001791 {
1792 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001793 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001794 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001795 break;
1796 }
1797 return;
1798 }
1799 }
1800 else
1801 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1802 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 while ((xp->xp_pattern = vim_strpbrk(arg,
1804 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1805 {
1806 c = *xp->xp_pattern;
1807 if (c == '&')
1808 {
1809 c = xp->xp_pattern[1];
1810 if (c == '&')
1811 {
1812 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001813 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 }
1815 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001816 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001818 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1819 xp->xp_pattern += 2;
1820
1821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 }
1823 else if (c == '$')
1824 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001825 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 xp->xp_context = EXPAND_ENV_VARS;
1827 }
1828 else if (c == '=')
1829 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001830 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 xp->xp_context = EXPAND_EXPRESSION;
1832 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001833 else if (c == '#'
1834 && xp->xp_context == EXPAND_EXPRESSION)
1835 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001836 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001837 break;
1838 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001839 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 && xp->xp_context == EXPAND_FUNCTIONS
1841 && vim_strchr(xp->xp_pattern, '(') == NULL)
1842 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001843 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 break;
1845 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001846 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001848 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 {
1850 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1851 if (c == '\\' && xp->xp_pattern[1] != NUL)
1852 ++xp->xp_pattern;
1853 xp->xp_context = EXPAND_NOTHING;
1854 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001855 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001857 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1859 /* skip */ ;
1860 xp->xp_context = EXPAND_NOTHING;
1861 }
1862 else if (c == '|')
1863 {
1864 if (xp->xp_pattern[1] == '|')
1865 {
1866 ++xp->xp_pattern;
1867 xp->xp_context = EXPAND_EXPRESSION;
1868 }
1869 else
1870 xp->xp_context = EXPAND_COMMANDS;
1871 }
1872 else
1873 xp->xp_context = EXPAND_EXPRESSION;
1874 }
1875 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001876 // Doesn't look like something valid, expand as an expression
1877 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001878 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 arg = xp->xp_pattern;
1880 if (*arg != NUL)
1881 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1882 /* skip */ ;
1883 }
1884 xp->xp_pattern = arg;
1885}
1886
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001888 * Return TRUE if "pat" matches "text".
1889 * Does not use 'cpo' and always uses 'magic'.
1890 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001891 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001892pattern_match(char_u *pat, char_u *text, int ic)
1893{
1894 int matches = FALSE;
1895 char_u *save_cpo;
1896 regmatch_T regmatch;
1897
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001898 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001899 save_cpo = p_cpo;
1900 p_cpo = (char_u *)"";
1901 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1902 if (regmatch.regprog != NULL)
1903 {
1904 regmatch.rm_ic = ic;
1905 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1906 vim_regfree(regmatch.regprog);
1907 }
1908 p_cpo = save_cpo;
1909 return matches;
1910}
1911
1912/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001913 * Handle a name followed by "(". Both for just "name(arg)" and for
1914 * "expr->name(arg)".
1915 * Returns OK or FAIL.
1916 */
1917 static int
1918eval_func(
1919 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02001920 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001921 char_u *name,
1922 int name_len,
1923 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001924 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001925 typval_T *basetv) // "expr" for "expr->name(arg)"
1926{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001927 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001928 char_u *s = name;
1929 int len = name_len;
1930 partial_T *partial;
1931 int ret = OK;
1932
1933 if (!evaluate)
1934 check_vars(s, len);
1935
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001936 // If "s" is the name of a variable of type VAR_FUNC
1937 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001938 s = deref_func_name(s, &len, &partial, !evaluate);
1939
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001940 // Need to make a copy, in case evaluating the arguments makes
1941 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001942 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001943 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001944 ret = FAIL;
1945 else
1946 {
1947 funcexe_T funcexe;
1948
1949 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001950 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001951 funcexe.firstline = curwin->w_cursor.lnum;
1952 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001953 funcexe.evaluate = evaluate;
1954 funcexe.partial = partial;
1955 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02001956 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001957 }
1958 vim_free(s);
1959
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001960 // If evaluate is FALSE rettv->v_type was not set in
1961 // get_func_tv, but it's needed in handle_subscript() to parse
1962 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001963 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
1964 {
1965 rettv->vval.v_string = NULL;
1966 rettv->v_type = VAR_FUNC;
1967 }
1968
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001969 // Stop the expression evaluation when immediately
1970 // aborting on error, or when an interrupt occurred or
1971 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001972 if (evaluate && aborting())
1973 {
1974 if (ret == OK)
1975 clear_tv(rettv);
1976 ret = FAIL;
1977 }
1978 return ret;
1979}
1980
1981/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02001982 * Get the next line source line without advancing. But do skip over comment
1983 * lines.
1984 */
1985 static char_u *
1986getline_peek_skip_comments(evalarg_T *evalarg)
1987{
1988 for (;;)
1989 {
1990 char_u *next = getline_peek(evalarg->eval_getline,
1991 evalarg->eval_cookie);
1992 char_u *p;
1993
1994 if (next == NULL)
1995 break;
1996 p = skipwhite(next);
1997 if (*p != NUL && !vim9_comment_start(p))
1998 return next;
1999 (void)eval_next_line(evalarg);
2000 }
2001 return NULL;
2002}
2003
2004/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002005 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2006 * comment) and there is a next line, return the next line (skipping blanks)
2007 * and set "getnext".
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002008 * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
2009 * "arg" must point somewhere inside a line, not at the start.
2010 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002011 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002012eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2013{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002014 char_u *p = skipwhite(arg);
2015
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002016 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002017 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002018 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002019 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaar9d489562020-07-30 20:08:50 +02002020 && (*p == NUL || (VIM_ISWHITE(p[-1]) && vim9_comment_start(p))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002021 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002022 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002023
2024 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002025 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002026 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002027 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002028
Bram Moolenaar9d489562020-07-30 20:08:50 +02002029 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002030 {
2031 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002032 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002033 }
2034 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002035 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002036}
2037
2038/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002039 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002040 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002041 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002042eval_next_line(evalarg_T *evalarg)
2043{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002044 garray_T *gap = &evalarg->eval_ga;
2045 char_u *line;
2046
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002047 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002048 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2049 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002050 else
2051 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002052 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002053 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2054 {
2055 // Going to concatenate the lines after parsing.
2056 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2057 ++gap->ga_len;
2058 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002059 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002060 {
2061 vim_free(evalarg->eval_tofree);
2062 evalarg->eval_tofree = line;
2063 }
2064 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002065}
2066
Bram Moolenaare6b53242020-07-01 17:28:33 +02002067/*
2068 * Call eval_next_non_blank() and get the next line if needed.
2069 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002070 char_u *
2071skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2072{
2073 int getnext;
2074 char_u *p = skipwhite(arg);
2075
Bram Moolenaar962d7212020-07-04 14:15:00 +02002076 if (evalarg == NULL)
2077 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002078 eval_next_non_blank(p, evalarg, &getnext);
2079 if (getnext)
2080 return eval_next_line(evalarg);
2081 return p;
2082}
2083
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002084/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002085 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002086 */
2087 void
2088clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2089{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002090 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002091 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002092 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002093 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002094 if (eap != NULL)
2095 {
2096 // We may need to keep the original command line, e.g. for
2097 // ":let" it has the variable names. But we may also need the
2098 // new one, "nextcmd" points into it. Keep both.
2099 vim_free(eap->cmdline_tofree);
2100 eap->cmdline_tofree = *eap->cmdlinep;
2101 *eap->cmdlinep = evalarg->eval_tofree;
2102 }
2103 else
2104 vim_free(evalarg->eval_tofree);
2105 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002106 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002107
2108 vim_free(evalarg->eval_tofree_lambda);
2109 evalarg->eval_tofree_lambda = NULL;
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002110 }
2111}
2112
2113/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002115 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2117 */
2118
2119/*
2120 * Handle zero level expression.
2121 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002122 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002123 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002124 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 * Return OK or FAIL.
2126 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002127 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002128eval0(
2129 char_u *arg,
2130 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002131 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002132 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133{
2134 int ret;
2135 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002136 int did_emsg_before = did_emsg;
2137 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002138 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139
2140 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002141 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002142 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002143
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002144 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 {
2146 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002147 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 /*
2149 * Report the invalid expression unless the expression evaluation has
2150 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002151 * exception, or we already gave a more specific error.
2152 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002154 if (!aborting()
2155 && did_emsg == did_emsg_before
2156 && called_emsg == called_emsg_before
2157 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002158 semsg(_(e_invexpr2), arg);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002159
2160 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002161 // a next command to avoid more errors, unless "|" is following, which
2162 // could only be a command separator.
2163 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2164 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002165 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002167
2168 if (eap != NULL)
2169 eap->nextcmd = check_nextcmd(p);
2170
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 return ret;
2172}
2173
2174/*
2175 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002176 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002177 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 *
2179 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002180 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002182 * Note: "rettv.v_lock" is not set.
2183 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 * Return OK or FAIL.
2185 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002186 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002187eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002189 char_u *p;
2190 int getnext;
2191
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002192 CLEAR_POINTER(rettv);
2193
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 /*
2195 * Get the first variable.
2196 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002197 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 return FAIL;
2199
Bram Moolenaar793648f2020-06-26 21:28:25 +02002200 p = eval_next_non_blank(*arg, evalarg, &getnext);
2201 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002203 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002204 int result;
2205 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002206 evalarg_T *evalarg_used = evalarg;
2207 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002208 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002209 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002210 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002211
2212 if (evalarg == NULL)
2213 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002214 CLEAR_FIELD(local_evalarg);
2215 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002216 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002217 orig_flags = evalarg_used->eval_flags;
2218 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002219
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002220 if (getnext)
2221 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002222 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002223 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002224 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002225 {
2226 error_white_both(p, 1);
2227 clear_tv(rettv);
2228 return FAIL;
2229 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002230 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002231 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002232
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002234 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002236 int error = FALSE;
2237
Bram Moolenaar13106602020-10-04 16:06:05 +02002238 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002239 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002240 else if (vim9script)
2241 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002242 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002244 if (error || !op_falsy || !result)
2245 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002246 if (error)
2247 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 }
2249
2250 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002251 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002253 if (op_falsy)
2254 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002255 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002256 {
2257 error_white_both(p, 1);
2258 clear_tv(rettv);
2259 return FAIL;
2260 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002261 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002262 evalarg_used->eval_flags = (op_falsy ? !result : result)
2263 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2264 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002265 {
2266 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002268 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002269 if (!op_falsy || !result)
2270 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002272 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002274 /*
2275 * Check for the ":".
2276 */
2277 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2278 if (*p != ':')
2279 {
2280 emsg(_(e_missing_colon));
2281 if (evaluate && result)
2282 clear_tv(rettv);
2283 evalarg_used->eval_flags = orig_flags;
2284 return FAIL;
2285 }
2286 if (getnext)
2287 *arg = eval_next_line(evalarg_used);
2288 else
2289 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002290 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002291 {
2292 error_white_both(p, 1);
2293 clear_tv(rettv);
2294 evalarg_used->eval_flags = orig_flags;
2295 return FAIL;
2296 }
2297 *arg = p;
2298 }
2299
2300 /*
2301 * Get the third variable. Recursive!
2302 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002303 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002304 {
2305 error_white_both(p, 1);
2306 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002307 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002308 return FAIL;
2309 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002310 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2311 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002312 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002313 if (eval1(arg, &var2, evalarg_used) == FAIL)
2314 {
2315 if (evaluate && result)
2316 clear_tv(rettv);
2317 evalarg_used->eval_flags = orig_flags;
2318 return FAIL;
2319 }
2320 if (evaluate && !result)
2321 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002323
2324 if (evalarg == NULL)
2325 clear_evalarg(&local_evalarg, NULL);
2326 else
2327 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 }
2329
2330 return OK;
2331}
2332
2333/*
2334 * Handle first level expression:
2335 * expr2 || expr2 || expr2 logical OR
2336 *
2337 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002338 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 *
2340 * Return OK or FAIL.
2341 */
2342 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002343eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002345 char_u *p;
2346 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347
2348 /*
2349 * Get the first variable.
2350 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002351 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 return FAIL;
2353
2354 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002355 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002357 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002358 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002360 evalarg_T *evalarg_used = evalarg;
2361 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002362 int evaluate;
2363 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002364 long result = FALSE;
2365 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002366 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002367 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002368
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002369 if (evalarg == NULL)
2370 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002371 CLEAR_FIELD(local_evalarg);
2372 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002373 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002374 orig_flags = evalarg_used->eval_flags;
2375 evaluate = orig_flags & EVAL_EVALUATE;
2376 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002377 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002378 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002379 result = tv_get_bool_chk(rettv, &error);
2380 else if (tv_get_number_chk(rettv, &error) != 0)
2381 result = TRUE;
2382 clear_tv(rettv);
2383 if (error)
2384 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 }
2386
2387 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002388 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002390 while (p[0] == '|' && p[1] == '|')
2391 {
2392 if (getnext)
2393 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002394 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002395 {
2396 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2397 {
2398 error_white_both(p, 2);
2399 clear_tv(rettv);
2400 return FAIL;
2401 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002402 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002403 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002404
2405 /*
2406 * Get the second variable.
2407 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002408 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2409 {
2410 error_white_both(p, 2);
2411 clear_tv(rettv);
2412 return FAIL;
2413 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002414 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2415 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002416 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002417 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002418 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002419
2420 /*
2421 * Compute the result.
2422 */
2423 if (evaluate && !result)
2424 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002425 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002426 result = tv_get_bool_chk(&var2, &error);
2427 else if (tv_get_number_chk(&var2, &error) != 0)
2428 result = TRUE;
2429 clear_tv(&var2);
2430 if (error)
2431 return FAIL;
2432 }
2433 if (evaluate)
2434 {
2435 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002436 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002437 rettv->v_type = VAR_BOOL;
2438 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002439 }
2440 else
2441 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002442 rettv->v_type = VAR_NUMBER;
2443 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002444 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002445 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002446
2447 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002449
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002450 if (evalarg == NULL)
2451 clear_evalarg(&local_evalarg, NULL);
2452 else
2453 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 }
2455
2456 return OK;
2457}
2458
2459/*
2460 * Handle second level expression:
2461 * expr3 && expr3 && expr3 logical AND
2462 *
2463 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002464 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 *
2466 * Return OK or FAIL.
2467 */
2468 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002469eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002471 char_u *p;
2472 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473
2474 /*
2475 * Get the first variable.
2476 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002477 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 return FAIL;
2479
2480 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002481 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002483 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002484 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002486 evalarg_T *evalarg_used = evalarg;
2487 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002488 int orig_flags;
2489 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002490 long result = TRUE;
2491 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002492 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002493 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002494
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002495 if (evalarg == NULL)
2496 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002497 CLEAR_FIELD(local_evalarg);
2498 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002499 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002500 orig_flags = evalarg_used->eval_flags;
2501 evaluate = orig_flags & EVAL_EVALUATE;
2502 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002503 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002504 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002505 result = tv_get_bool_chk(rettv, &error);
2506 else if (tv_get_number_chk(rettv, &error) == 0)
2507 result = FALSE;
2508 clear_tv(rettv);
2509 if (error)
2510 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 }
2512
2513 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002514 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002516 while (p[0] == '&' && p[1] == '&')
2517 {
2518 if (getnext)
2519 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002520 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002521 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002522 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002523 {
2524 error_white_both(p, 2);
2525 clear_tv(rettv);
2526 return FAIL;
2527 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002528 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002529 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002530
2531 /*
2532 * Get the second variable.
2533 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002534 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2535 {
2536 error_white_both(p, 2);
2537 clear_tv(rettv);
2538 return FAIL;
2539 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002540 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2541 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002542 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002543 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002544 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002545 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002546
2547 /*
2548 * Compute the result.
2549 */
2550 if (evaluate && result)
2551 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002552 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002553 result = tv_get_bool_chk(&var2, &error);
2554 else if (tv_get_number_chk(&var2, &error) == 0)
2555 result = FALSE;
2556 clear_tv(&var2);
2557 if (error)
2558 return FAIL;
2559 }
2560 if (evaluate)
2561 {
2562 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002563 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002564 rettv->v_type = VAR_BOOL;
2565 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002566 }
2567 else
2568 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002569 rettv->v_type = VAR_NUMBER;
2570 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002571 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002572 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002573
2574 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002576
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002577 if (evalarg == NULL)
2578 clear_evalarg(&local_evalarg, NULL);
2579 else
2580 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 }
2582
2583 return OK;
2584}
2585
2586/*
2587 * Handle third level expression:
2588 * var1 == var2
2589 * var1 =~ var2
2590 * var1 != var2
2591 * var1 !~ var2
2592 * var1 > var2
2593 * var1 >= var2
2594 * var1 < var2
2595 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002596 * var1 is var2
2597 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 *
2599 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002600 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 *
2602 * Return OK or FAIL.
2603 */
2604 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002605eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002608 int getnext;
Bram Moolenaar87396072019-12-31 22:36:18 +01002609 exptype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002611 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612
2613 /*
2614 * Get the first variable.
2615 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002616 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 return FAIL;
2618
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002619 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002620 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621
2622 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002623 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002625 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002627 typval_T var2;
2628 int ic;
2629 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002630 int evaluate = evalarg == NULL
2631 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002632
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002633 if (getnext)
2634 *arg = eval_next_line(evalarg);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002635 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2636 {
2637 error_white_both(p, len);
2638 clear_tv(rettv);
2639 return FAIL;
2640 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002641
Bram Moolenaar696ba232020-07-29 21:20:41 +02002642 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2643 {
2644 semsg(_(e_invexpr2), p);
2645 clear_tv(rettv);
2646 return FAIL;
2647 }
2648
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002649 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 if (p[len] == '?')
2651 {
2652 ic = TRUE;
2653 ++len;
2654 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002655 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 else if (p[len] == '#')
2657 {
2658 ic = FALSE;
2659 ++len;
2660 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002661 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002663 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664
2665 /*
2666 * Get the second variable.
2667 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002668 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2669 {
2670 error_white_both(p, 1);
2671 clear_tv(rettv);
2672 return FAIL;
2673 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002674 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002675 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002677 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 return FAIL;
2679 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002680 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002681 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002682 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002683
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002684 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002685 {
2686 ret = FAIL;
2687 clear_tv(rettv);
2688 }
2689 else
2690 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002691 clear_tv(&var2);
2692 return ret;
2693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 }
2695
2696 return OK;
2697}
2698
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002699/*
2700 * Make a copy of blob "tv1" and append blob "tv2".
2701 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002702 void
2703eval_addblob(typval_T *tv1, typval_T *tv2)
2704{
2705 blob_T *b1 = tv1->vval.v_blob;
2706 blob_T *b2 = tv2->vval.v_blob;
2707 blob_T *b = blob_alloc();
2708 int i;
2709
2710 if (b != NULL)
2711 {
2712 for (i = 0; i < blob_len(b1); i++)
2713 ga_append(&b->bv_ga, blob_get(b1, i));
2714 for (i = 0; i < blob_len(b2); i++)
2715 ga_append(&b->bv_ga, blob_get(b2, i));
2716
2717 clear_tv(tv1);
2718 rettv_blob_set(tv1, b);
2719 }
2720}
2721
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002722/*
2723 * Make a copy of list "tv1" and append list "tv2".
2724 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002725 int
2726eval_addlist(typval_T *tv1, typval_T *tv2)
2727{
2728 typval_T var3;
2729
2730 // concatenate Lists
2731 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2732 {
2733 clear_tv(tv1);
2734 clear_tv(tv2);
2735 return FAIL;
2736 }
2737 clear_tv(tv1);
2738 *tv1 = var3;
2739 return OK;
2740}
2741
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742/*
2743 * Handle fourth level expression:
2744 * + number addition
2745 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002746 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002747 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 *
2749 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002750 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 *
2752 * Return OK or FAIL.
2753 */
2754 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002755eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 /*
2758 * Get the first variable.
2759 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002760 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 return FAIL;
2762
2763 /*
2764 * Repeat computing, until no '+', '-' or '.' is following.
2765 */
2766 for (;;)
2767 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002768 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002769 int getnext;
2770 char_u *p;
2771 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002772 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002773 int concat;
2774 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002775 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002776
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002777 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarce2c5442020-11-28 21:21:17 +01002778 // "+=" and "-=" are assignment
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002779 p = eval_next_non_blank(*arg, evalarg, &getnext);
2780 op = *p;
2781 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaarce2c5442020-11-28 21:21:17 +01002782 if ((op != '+' && op != '-' && !concat) || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002784
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002785 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002786 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002787 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002788 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002789 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002790 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002791 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002792 {
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002793 error_white_both(p, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002794 clear_tv(rettv);
2795 return FAIL;
2796 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002797 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002798 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002799 if ((op != '+' || (rettv->v_type != VAR_LIST
2800 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002801#ifdef FEAT_FLOAT
2802 && (op == '.' || rettv->v_type != VAR_FLOAT)
2803#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002804 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002805 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002806 int error = FALSE;
2807
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002808 // For "list + ...", an illegal use of the first operand as
2809 // a number cannot be determined before evaluating the 2nd
2810 // operand: if this is also a list, all is ok.
2811 // For "something . ...", "something - ..." or "non-list + ...",
2812 // we know that the first operand needs to be a string or number
2813 // without evaluating the 2nd operand. So check before to avoid
2814 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002815 if (op != '.')
2816 tv_get_number_chk(rettv, &error);
2817 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002818 {
2819 clear_tv(rettv);
2820 return FAIL;
2821 }
2822 }
2823
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 /*
2825 * Get the second variable.
2826 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002827 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002828 {
2829 error_white_both(p, oplen);
2830 clear_tv(rettv);
2831 return FAIL;
2832 }
2833 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002834 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002836 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 return FAIL;
2838 }
2839
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002840 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 {
2842 /*
2843 * Compute the result.
2844 */
2845 if (op == '.')
2846 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002847 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2848 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002849 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002850
Bram Moolenaar2e086612020-08-25 22:37:48 +02002851 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002852 || var2.v_type == VAR_CHANNEL
2853 || var2.v_type == VAR_JOB))
2854 emsg(_(e_inval_string));
2855#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02002856 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002857 {
2858 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2859 var2.vval.v_float);
2860 s2 = buf2;
2861 }
2862#endif
2863 else
2864 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002865 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002866 {
2867 clear_tv(rettv);
2868 clear_tv(&var2);
2869 return FAIL;
2870 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002871 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002872 clear_tv(rettv);
2873 rettv->v_type = VAR_STRING;
2874 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002876 else if (op == '+' && rettv->v_type == VAR_BLOB
2877 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002878 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002879 else if (op == '+' && rettv->v_type == VAR_LIST
2880 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002881 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002882 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002883 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 else
2886 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002887 int error = FALSE;
2888 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002889#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002890 float_T f1 = 0, f2 = 0;
2891
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002892 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002893 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002894 f1 = rettv->vval.v_float;
2895 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002896 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002897 else
2898#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002899 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002900 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002901 if (error)
2902 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002903 // This can only happen for "list + non-list". For
2904 // "non-list + ..." or "something - ...", we returned
2905 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002906 clear_tv(rettv);
2907 return FAIL;
2908 }
2909#ifdef FEAT_FLOAT
2910 if (var2.v_type == VAR_FLOAT)
2911 f1 = n1;
2912#endif
2913 }
2914#ifdef FEAT_FLOAT
2915 if (var2.v_type == VAR_FLOAT)
2916 {
2917 f2 = var2.vval.v_float;
2918 n2 = 0;
2919 }
2920 else
2921#endif
2922 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002923 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002924 if (error)
2925 {
2926 clear_tv(rettv);
2927 clear_tv(&var2);
2928 return FAIL;
2929 }
2930#ifdef FEAT_FLOAT
2931 if (rettv->v_type == VAR_FLOAT)
2932 f2 = n2;
2933#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002934 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002935 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002936
2937#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002938 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002939 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2940 {
2941 if (op == '+')
2942 f1 = f1 + f2;
2943 else
2944 f1 = f1 - f2;
2945 rettv->v_type = VAR_FLOAT;
2946 rettv->vval.v_float = f1;
2947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002949#endif
2950 {
2951 if (op == '+')
2952 n1 = n1 + n2;
2953 else
2954 n1 = n1 - n2;
2955 rettv->v_type = VAR_NUMBER;
2956 rettv->vval.v_number = n1;
2957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002959 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 }
2961 }
2962 return OK;
2963}
2964
2965/*
2966 * Handle fifth level expression:
2967 * * number multiplication
2968 * / number division
2969 * % number modulo
2970 *
2971 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002972 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 *
2974 * Return OK or FAIL.
2975 */
2976 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002977eval6(
2978 char_u **arg,
2979 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002980 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002981 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002983#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002984 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986
2987 /*
2988 * Get the first variable.
2989 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002990 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 return FAIL;
2992
2993 /*
2994 * Repeat computing, until no '*', '/' or '%' is following.
2995 */
2996 for (;;)
2997 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002998 int evaluate;
2999 int getnext;
3000 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003001 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003002 int op;
3003 varnumber_T n1, n2;
3004#ifdef FEAT_FLOAT
3005 float_T f1, f2;
3006#endif
3007 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003008
Bram Moolenaar9d489562020-07-30 20:08:50 +02003009 p = eval_next_non_blank(*arg, evalarg, &getnext);
3010 op = *p;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02003011 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003013
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003014 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003015 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003016 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003017 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003018 {
3019 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3020 {
3021 error_white_both(p, 1);
3022 clear_tv(rettv);
3023 return FAIL;
3024 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003025 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003028#ifdef FEAT_FLOAT
3029 f1 = 0;
3030 f2 = 0;
3031#endif
3032 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003033 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003035#ifdef FEAT_FLOAT
3036 if (rettv->v_type == VAR_FLOAT)
3037 {
3038 f1 = rettv->vval.v_float;
3039 use_float = TRUE;
3040 n1 = 0;
3041 }
3042 else
3043#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003044 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003045 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003046 if (error)
3047 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 }
3049 else
3050 n1 = 0;
3051
3052 /*
3053 * Get the second variable.
3054 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003055 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3056 {
3057 error_white_both(p, 1);
3058 clear_tv(rettv);
3059 return FAIL;
3060 }
3061 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003062 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 return FAIL;
3064
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003065 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003067#ifdef FEAT_FLOAT
3068 if (var2.v_type == VAR_FLOAT)
3069 {
3070 if (!use_float)
3071 {
3072 f1 = n1;
3073 use_float = TRUE;
3074 }
3075 f2 = var2.vval.v_float;
3076 n2 = 0;
3077 }
3078 else
3079#endif
3080 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003081 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003082 clear_tv(&var2);
3083 if (error)
3084 return FAIL;
3085#ifdef FEAT_FLOAT
3086 if (use_float)
3087 f2 = n2;
3088#endif
3089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090
3091 /*
3092 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003093 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003095#ifdef FEAT_FLOAT
3096 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003098 if (op == '*')
3099 f1 = f1 * f2;
3100 else if (op == '/')
3101 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003102# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003103 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003104 if (f2 == 0.0)
3105 {
3106 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003107 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003108 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003109 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003110 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003111 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003112 }
3113 else
3114 f1 = f1 / f2;
3115# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003116 // We rely on the floating point library to handle divide
3117 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003118 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003119# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003122 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003123 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003124 return FAIL;
3125 }
3126 rettv->v_type = VAR_FLOAT;
3127 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 }
3129 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003132 if (op == '*')
3133 n1 = n1 * n2;
3134 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01003135 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01003137 n1 = num_modulus(n1, n2);
3138
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003139 rettv->v_type = VAR_NUMBER;
3140 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 }
3143 }
3144
3145 return OK;
3146}
3147
3148/*
3149 * Handle sixth level expression:
3150 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003151 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003152 * "string" string constant
3153 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 * &option-name option value
3155 * @r register contents
3156 * identifier variable value
3157 * function() function call
3158 * $VAR environment variable
3159 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003160 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003161 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003162 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003163 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 *
3165 * Also handle:
3166 * ! in front logical NOT
3167 * - in front unary minus
3168 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003169 * trailing [] subscript in String or List
3170 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003171 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 *
3173 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003174 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 *
3176 * Return OK or FAIL.
3177 */
3178 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003179eval7(
3180 char_u **arg,
3181 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003182 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003183 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003185 int evaluate = evalarg != NULL
3186 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 int len;
3188 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 char_u *start_leader, *end_leader;
3190 int ret = OK;
3191 char_u *alias;
3192
3193 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003194 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003195 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003197 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198
3199 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003200 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 */
3202 start_leader = *arg;
3203 while (**arg == '!' || **arg == '-' || **arg == '+')
3204 *arg = skipwhite(*arg + 1);
3205 end_leader = *arg;
3206
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003207 if (**arg == '.' && (!isdigit(*(*arg + 1))
3208#ifdef FEAT_FLOAT
3209 || current_sctx.sc_version < 2
3210#endif
3211 ))
3212 {
3213 semsg(_(e_invexpr2), *arg);
3214 ++*arg;
3215 return FAIL;
3216 }
3217
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 switch (**arg)
3219 {
3220 /*
3221 * Number constant.
3222 */
3223 case '0':
3224 case '1':
3225 case '2':
3226 case '3':
3227 case '4':
3228 case '5':
3229 case '6':
3230 case '7':
3231 case '8':
3232 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003233 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003234
3235 // Apply prefixed "-" and "+" now. Matters especially when
3236 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003237 if (ret == OK && evaluate && end_leader > start_leader
3238 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003239 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 break;
3241
3242 /*
3243 * String constant: "string".
3244 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003245 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 break;
3247
3248 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003249 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003251 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003252 break;
3253
3254 /*
3255 * List: [expr, expr]
3256 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003257 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 break;
3259
3260 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003261 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003262 */
Bram Moolenaare0de1712020-12-02 17:36:54 +01003263 case '#': if (!in_vim9script() && (*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003264 {
3265 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003266 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003267 }
3268 else
3269 ret = NOTDONE;
3270 break;
3271
3272 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003273 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003274 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003275 */
Bram Moolenaarc2ca9352020-11-25 21:30:11 +01003276 case '{': ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003277 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003278 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003279 break;
3280
3281 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003282 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003284 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 break;
3286
3287 /*
3288 * Environment variable: $VAR.
3289 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003290 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 break;
3292
3293 /*
3294 * Register contents: @r.
3295 */
3296 case '@': ++*arg;
3297 if (evaluate)
3298 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003299 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02003300 rettv->vval.v_string = get_reg_contents(**arg,
3301 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 }
3303 if (**arg != NUL)
3304 ++*arg;
3305 break;
3306
3307 /*
3308 * nested expression: (expression).
3309 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003310 case '(': {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003311 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003312 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003313
Bram Moolenaar9215f012020-06-27 21:18:00 +02003314 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003315 if (**arg == ')')
3316 ++*arg;
3317 else if (ret == OK)
3318 {
3319 emsg(_(e_missing_close));
3320 clear_tv(rettv);
3321 ret = FAIL;
3322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 }
3324 break;
3325
Bram Moolenaar8c711452005-01-14 21:53:12 +00003326 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 break;
3328 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003329
3330 if (ret == NOTDONE)
3331 {
3332 /*
3333 * Must be a variable or function name.
3334 * Can also be a curly-braces kind of name: {expr}.
3335 */
3336 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003337 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003338 if (alias != NULL)
3339 s = alias;
3340
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003341 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003342 ret = FAIL;
3343 else
3344 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003345 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3346
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003347 if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
3348 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003349 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003350 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003351 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003352 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003353 else if (flags & EVAL_CONSTANT)
3354 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003355 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003356 {
3357 // get the value of "true", "false" or a variable
3358 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3359 {
3360 rettv->v_type = VAR_BOOL;
3361 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003362 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003363 }
3364 else if (len == 5 && in_vim9script()
3365 && STRNCMP(s, "false", 4) == 0)
3366 {
3367 rettv->v_type = VAR_BOOL;
3368 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003369 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003370 }
3371 else
3372 ret = eval_variable(s, len, rettv, NULL, TRUE, FALSE);
3373 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003374 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003375 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003376 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003377 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003378 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003379 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003380 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003381 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003382 }
3383
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003384 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3385 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003386 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003387 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388
3389 /*
3390 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3391 */
3392 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003393 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003394 return ret;
3395}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003396
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003397/*
3398 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003399 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003400 * Adjusts "end_leaderp" until it is at "start_leader".
3401 */
3402 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003403eval7_leader(
3404 typval_T *rettv,
3405 int numeric_only,
3406 char_u *start_leader,
3407 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003408{
3409 char_u *end_leader = *end_leaderp;
3410 int ret = OK;
3411 int error = FALSE;
3412 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003413 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003414#ifdef FEAT_FLOAT
3415 float_T f = 0.0;
3416
3417 if (rettv->v_type == VAR_FLOAT)
3418 f = rettv->vval.v_float;
3419 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003420#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003421 {
3422 while (VIM_ISWHITE(end_leader[-1]))
3423 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003424 if (in_vim9script() && end_leader[-1] == '!')
3425 val = tv2bool(rettv);
3426 else
3427 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003428 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003429 if (error)
3430 {
3431 clear_tv(rettv);
3432 ret = FAIL;
3433 }
3434 else
3435 {
3436 while (end_leader > start_leader)
3437 {
3438 --end_leader;
3439 if (*end_leader == '!')
3440 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003441 if (numeric_only)
3442 {
3443 ++end_leader;
3444 break;
3445 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003446#ifdef FEAT_FLOAT
3447 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003448 {
3449 if (in_vim9script())
3450 {
3451 rettv->v_type = VAR_BOOL;
3452 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3453 }
3454 else
3455 f = !f;
3456 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003457 else
3458#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003459 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003460 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003461 type = VAR_BOOL;
3462 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003463 }
3464 else if (*end_leader == '-')
3465 {
3466#ifdef FEAT_FLOAT
3467 if (rettv->v_type == VAR_FLOAT)
3468 f = -f;
3469 else
3470#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003471 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003472 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003473 type = VAR_NUMBER;
3474 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003475 }
3476 }
3477#ifdef FEAT_FLOAT
3478 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003480 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003481 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003483 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003484#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003485 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003486 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003487 if (in_vim9script())
3488 rettv->v_type = type;
3489 else
3490 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003491 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003494 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 return ret;
3496}
3497
3498/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003499 * Call the function referred to in "rettv".
3500 */
3501 static int
3502call_func_rettv(
3503 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003504 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003505 typval_T *rettv,
3506 int evaluate,
3507 dict_T *selfdict,
3508 typval_T *basetv)
3509{
3510 partial_T *pt = NULL;
3511 funcexe_T funcexe;
3512 typval_T functv;
3513 char_u *s;
3514 int ret;
3515
3516 // need to copy the funcref so that we can clear rettv
3517 if (evaluate)
3518 {
3519 functv = *rettv;
3520 rettv->v_type = VAR_UNKNOWN;
3521
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003522 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003523 if (functv.v_type == VAR_PARTIAL)
3524 {
3525 pt = functv.vval.v_partial;
3526 s = partial_name(pt);
3527 }
3528 else
3529 s = functv.vval.v_string;
3530 }
3531 else
3532 s = (char_u *)"";
3533
Bram Moolenaara80faa82020-04-12 19:37:17 +02003534 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003535 funcexe.firstline = curwin->w_cursor.lnum;
3536 funcexe.lastline = curwin->w_cursor.lnum;
3537 funcexe.evaluate = evaluate;
3538 funcexe.partial = pt;
3539 funcexe.selfdict = selfdict;
3540 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003541 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003542
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003543 // Clear the funcref afterwards, so that deleting it while
3544 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003545 if (evaluate)
3546 clear_tv(&functv);
3547
3548 return ret;
3549}
3550
3551/*
3552 * Evaluate "->method()".
3553 * "*arg" points to the '-'.
3554 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3555 */
3556 static int
3557eval_lambda(
3558 char_u **arg,
3559 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003560 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003561 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003562{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003563 int evaluate = evalarg != NULL
3564 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003565 typval_T base = *rettv;
3566 int ret;
3567
3568 // Skip over the ->.
3569 *arg += 2;
3570 rettv->v_type = VAR_UNKNOWN;
3571
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003572 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003573 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003574 return FAIL;
3575 else if (**arg != '(')
3576 {
3577 if (verbose)
3578 {
3579 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003580 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003581 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003582 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003583 }
3584 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003585 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003586 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003587 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003588 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003589
3590 // Clear the funcref afterwards, so that deleting it while
3591 // evaluating the arguments is possible (see test55).
3592 if (evaluate)
3593 clear_tv(&base);
3594
3595 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003596}
3597
3598/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003599 * Evaluate "->method()".
3600 * "*arg" points to the '-'.
3601 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3602 */
3603 static int
3604eval_method(
3605 char_u **arg,
3606 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003607 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003608 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003609{
3610 char_u *name;
3611 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003612 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003613 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003614 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003615 int evaluate = evalarg != NULL
3616 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003617
3618 // Skip over the ->.
3619 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003620 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003621
Bram Moolenaarac92e252019-08-03 21:58:38 +02003622 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003623 len = get_name_len(arg, &alias, evaluate, TRUE);
3624 if (alias != NULL)
3625 name = alias;
3626
3627 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003628 {
3629 if (verbose)
3630 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003631 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003632 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003633 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003634 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003635 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003636 if (**arg != '(')
3637 {
3638 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003639 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003640 ret = FAIL;
3641 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003642 else if (VIM_ISWHITE((*arg)[-1]))
3643 {
3644 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003645 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003646 ret = FAIL;
3647 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003648 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003649 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003650 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003651 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003652
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003653 // Clear the funcref afterwards, so that deleting it while
3654 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003655 if (evaluate)
3656 clear_tv(&base);
3657
Bram Moolenaarac92e252019-08-03 21:58:38 +02003658 return ret;
3659}
3660
3661/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003662 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3663 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003664 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3665 */
3666 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003667eval_index(
3668 char_u **arg,
3669 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003670 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003671 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003672{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003673 int evaluate = evalarg != NULL
3674 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003675 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003676 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003677 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003678 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003679 int keylen = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003680
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003681 if (check_can_index(rettv, evaluate, verbose) == FAIL)
3682 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003683
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003684 init_tv(&var1);
3685 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003686 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003687 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003688 /*
3689 * dict.name
3690 */
3691 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003692 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003693 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003694 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003695 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02003696 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003697 }
3698 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003699 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003700 /*
3701 * something[idx]
3702 *
3703 * Get the (first) variable from inside the [].
3704 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003705 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003706 if (**arg == ':')
3707 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003708 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003709 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003710 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003711 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003712 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003713 clear_tv(&var1);
3714 return FAIL;
3715 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003716
3717 /*
3718 * Get the second variable from inside the [:].
3719 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003720 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003721 if (**arg == ':')
3722 {
3723 range = TRUE;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003724 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003725 if (**arg == ']')
3726 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003727 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003728 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003729 if (!empty1)
3730 clear_tv(&var1);
3731 return FAIL;
3732 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003733 else if (evaluate && tv_get_string_chk(&var2) == 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 if (!empty1)
3737 clear_tv(&var1);
3738 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003739 return FAIL;
3740 }
3741 }
3742
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003743 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003744 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003745 if (**arg != ']')
3746 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003747 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003748 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003749 clear_tv(&var1);
3750 if (range)
3751 clear_tv(&var2);
3752 return FAIL;
3753 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02003754 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003755 }
3756
3757 if (evaluate)
3758 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003759 int res = eval_index_inner(rettv, range,
3760 empty1 ? NULL : &var1, empty2 ? NULL : &var2,
3761 key, keylen, verbose);
3762 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003763 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003764 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003765 clear_tv(&var2);
3766 return res;
3767 }
3768 return OK;
3769}
3770
3771/*
3772 * Check if "rettv" can have an [index] or [sli:ce]
3773 */
3774 int
3775check_can_index(typval_T *rettv, int evaluate, int verbose)
3776{
3777 switch (rettv->v_type)
3778 {
3779 case VAR_FUNC:
3780 case VAR_PARTIAL:
3781 if (verbose)
3782 emsg(_("E695: Cannot index a Funcref"));
3783 return FAIL;
3784 case VAR_FLOAT:
3785#ifdef FEAT_FLOAT
3786 if (verbose)
3787 emsg(_(e_float_as_string));
3788 return FAIL;
3789#endif
3790 case VAR_BOOL:
3791 case VAR_SPECIAL:
3792 case VAR_JOB:
3793 case VAR_CHANNEL:
3794 if (verbose)
3795 emsg(_(e_cannot_index_special_variable));
3796 return FAIL;
3797 case VAR_UNKNOWN:
3798 case VAR_ANY:
3799 case VAR_VOID:
3800 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003801 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003802 emsg(_(e_cannot_index_special_variable));
3803 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003804 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003805 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003806
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003807 case VAR_STRING:
3808 case VAR_LIST:
3809 case VAR_DICT:
3810 case VAR_BLOB:
3811 break;
3812 case VAR_NUMBER:
3813 if (in_vim9script())
3814 emsg(_(e_cannot_index_number));
3815 break;
3816 }
3817 return OK;
3818}
3819
3820/*
3821 * Apply index or range to "rettv".
3822 * "var1" is the first index, NULL for [:expr].
3823 * "var2" is the second index, NULL for [expr] and [expr: ]
3824 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
3825 */
3826 int
3827eval_index_inner(
3828 typval_T *rettv,
3829 int is_range,
3830 typval_T *var1,
3831 typval_T *var2,
3832 char_u *key,
3833 int keylen,
3834 int verbose)
3835{
3836 long n1, n2 = 0;
3837 long len;
3838
3839 n1 = 0;
3840 if (var1 != NULL && rettv->v_type != VAR_DICT)
3841 n1 = tv_get_number(var1);
3842
3843 if (is_range)
3844 {
3845 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003846 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003847 if (verbose)
3848 emsg(_(e_cannot_slice_dictionary));
3849 return FAIL;
3850 }
3851 if (var2 == NULL)
3852 n2 = -1;
3853 else
3854 n2 = tv_get_number(var2);
3855 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01003856
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003857 switch (rettv->v_type)
3858 {
3859 case VAR_UNKNOWN:
3860 case VAR_ANY:
3861 case VAR_VOID:
3862 case VAR_FUNC:
3863 case VAR_PARTIAL:
3864 case VAR_FLOAT:
3865 case VAR_BOOL:
3866 case VAR_SPECIAL:
3867 case VAR_JOB:
3868 case VAR_CHANNEL:
3869 break; // not evaluating, skipping over subscript
3870
3871 case VAR_NUMBER:
3872 case VAR_STRING:
3873 {
3874 char_u *s = tv_get_string(rettv);
3875
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003876 len = (long)STRLEN(s);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003877 if (in_vim9script())
3878 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003879 if (is_range)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003880 s = string_slice(s, n1, n2);
3881 else
3882 s = char_from_string(s, n1);
3883 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003884 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003885 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003886 // The resulting variable is a substring. If the indexes
3887 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003888 if (n1 < 0)
3889 {
3890 n1 = len + n1;
3891 if (n1 < 0)
3892 n1 = 0;
3893 }
3894 if (n2 < 0)
3895 n2 = len + n2;
3896 else if (n2 >= len)
3897 n2 = len;
3898 if (n1 >= len || n2 < 0 || n1 > n2)
3899 s = NULL;
3900 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02003901 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003902 }
3903 else
3904 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003905 // The resulting variable is a string of a single
3906 // character. If the index is too big or negative the
3907 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003908 if (n1 >= len || n1 < 0)
3909 s = NULL;
3910 else
3911 s = vim_strnsave(s + n1, 1);
3912 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003913 clear_tv(rettv);
3914 rettv->v_type = VAR_STRING;
3915 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003916 }
3917 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003918
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003919 case VAR_BLOB:
3920 len = blob_len(rettv->vval.v_blob);
3921 if (is_range)
3922 {
3923 // The resulting variable is a sub-blob. If the indexes
3924 // are out of range the result is empty.
3925 if (n1 < 0)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003926 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003927 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003928 if (n1 < 0)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003929 n1 = 0;
3930 }
3931 if (n2 < 0)
3932 n2 = len + n2;
3933 else if (n2 >= len)
3934 n2 = len - 1;
3935 if (n1 >= len || n2 < 0 || n1 > n2)
3936 {
3937 clear_tv(rettv);
3938 rettv->v_type = VAR_BLOB;
3939 rettv->vval.v_blob = NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003940 }
3941 else
3942 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003943 blob_T *blob = blob_alloc();
3944 long i;
3945
3946 if (blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003947 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003948 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003949 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003950 blob_free(blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003951 return FAIL;
3952 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003953 blob->bv_ga.ga_len = n2 - n1 + 1;
3954 for (i = n1; i <= n2; i++)
3955 blob_set(blob, i - n1,
3956 blob_get(rettv->vval.v_blob, i));
3957
3958 clear_tv(rettv);
3959 rettv_blob_set(rettv, blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003960 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003961 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003962 }
3963 else
3964 {
3965 // The resulting variable is a byte value.
3966 // If the index is too big or negative that is an error.
3967 if (n1 < 0)
3968 n1 = len + n1;
3969 if (n1 < len && n1 >= 0)
3970 {
3971 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003972
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003973 clear_tv(rettv);
3974 rettv->v_type = VAR_NUMBER;
3975 rettv->vval.v_number = v;
3976 }
3977 else
3978 semsg(_(e_blobidx), n1);
3979 }
3980 break;
3981
3982 case VAR_LIST:
3983 if (var1 == NULL)
3984 n1 = 0;
3985 if (var2 == NULL)
3986 n2 = -1;
3987 if (list_slice_or_index(rettv->vval.v_list,
3988 is_range, n1, n2, rettv, verbose) == FAIL)
3989 return FAIL;
3990 break;
3991
3992 case VAR_DICT:
3993 {
3994 dictitem_T *item;
3995 typval_T tmp;
3996
3997 if (key == NULL)
3998 {
3999 key = tv_get_string_chk(var1);
4000 if (key == NULL)
4001 return FAIL;
4002 }
4003
4004 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
4005
4006 if (item == NULL && verbose)
4007 semsg(_(e_dictkey), key);
4008 if (item == NULL)
4009 return FAIL;
4010
4011 copy_tv(&item->di_tv, &tmp);
4012 clear_tv(rettv);
4013 *rettv = tmp;
4014 }
4015 break;
4016 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004017 return OK;
4018}
4019
4020/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004021 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004022 */
4023 char_u *
4024partial_name(partial_T *pt)
4025{
4026 if (pt->pt_name != NULL)
4027 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004028 if (pt->pt_func != NULL)
4029 return pt->pt_func->uf_name;
4030 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004031}
4032
Bram Moolenaarddecc252016-04-06 22:59:37 +02004033 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004034partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004035{
4036 int i;
4037
4038 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004039 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004040 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004041 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004042 if (pt->pt_name != NULL)
4043 {
4044 func_unref(pt->pt_name);
4045 vim_free(pt->pt_name);
4046 }
4047 else
4048 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004049
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004050 // Decrease the reference count for the context of a closure. If down
4051 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004052 if (pt->pt_funcstack != NULL)
4053 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004054 --pt->pt_funcstack->fs_refcount;
4055 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004056 }
4057
Bram Moolenaarddecc252016-04-06 22:59:37 +02004058 vim_free(pt);
4059}
4060
4061/*
4062 * Unreference a closure: decrement the reference count and free it when it
4063 * becomes zero.
4064 */
4065 void
4066partial_unref(partial_T *pt)
4067{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004068 if (pt != NULL)
4069 {
4070 if (--pt->pt_refcount <= 0)
4071 partial_free(pt);
4072
4073 // If the reference count goes down to one, the funcstack may be the
4074 // only reference and can be freed if no other partials reference it.
4075 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4076 funcstack_check_refcount(pt->pt_funcstack);
4077 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004078}
4079
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004080/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004081 * Return the next (unique) copy ID.
4082 * Used for serializing nested structures.
4083 */
4084 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004085get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004086{
4087 current_copyID += COPYID_INC;
4088 return current_copyID;
4089}
4090
4091/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004092 * Garbage collection for lists and dictionaries.
4093 *
4094 * We use reference counts to be able to free most items right away when they
4095 * are no longer used. But for composite items it's possible that it becomes
4096 * unused while the reference count is > 0: When there is a recursive
4097 * reference. Example:
4098 * :let l = [1, 2, 3]
4099 * :let d = {9: l}
4100 * :let l[1] = d
4101 *
4102 * Since this is quite unusual we handle this with garbage collection: every
4103 * once in a while find out which lists and dicts are not referenced from any
4104 * variable.
4105 *
4106 * Here is a good reference text about garbage collection (refers to Python
4107 * but it applies to all reference-counting mechanisms):
4108 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004109 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004110
4111/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004112 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004113 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004114 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004115 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004116 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004117garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004118{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004119 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004120 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004121 buf_T *buf;
4122 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004123 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004124 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004125
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004126 if (!testing)
4127 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004128 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004129 want_garbage_collect = FALSE;
4130 may_garbage_collect = FALSE;
4131 garbage_collect_at_exit = FALSE;
4132 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004133
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004134 // The execution stack can grow big, limit the size.
4135 if (exestack.ga_maxlen - exestack.ga_len > 500)
4136 {
4137 size_t new_len;
4138 char_u *pp;
4139 int n;
4140
4141 // Keep 150% of the current size, with a minimum of the growth size.
4142 n = exestack.ga_len / 2;
4143 if (n < exestack.ga_growsize)
4144 n = exestack.ga_growsize;
4145
4146 // Don't make it bigger though.
4147 if (exestack.ga_len + n < exestack.ga_maxlen)
4148 {
4149 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4150 pp = vim_realloc(exestack.ga_data, new_len);
4151 if (pp == NULL)
4152 return FAIL;
4153 exestack.ga_maxlen = exestack.ga_len + n;
4154 exestack.ga_data = pp;
4155 }
4156 }
4157
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004158 // We advance by two because we add one for items referenced through
4159 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004160 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004161
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004162 /*
4163 * 1. Go through all accessible variables and mark all lists and dicts
4164 * with copyID.
4165 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004166
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004167 // Don't free variables in the previous_funccal list unless they are only
4168 // referenced through previous_funccal. This must be first, because if
4169 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004170 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004171
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004172 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004173 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004174
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004175 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004176 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004177 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4178 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004179
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004180 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004181 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004182 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4183 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004184 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004185 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4186 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004187#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004188 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004189 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4190 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004191 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004192 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004193 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4194 NULL, NULL);
4195#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004196
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004197 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004198 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004199 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4200 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004201 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004202 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004203
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004204 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004205 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004206
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004207 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004208 abort = abort || set_ref_in_functions(copyID);
4209
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004210 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004211 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004212
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004213 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004214 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004215
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004216 // callbacks in buffers
4217 abort = abort || set_ref_in_buffers(copyID);
4218
Bram Moolenaar1dced572012-04-05 16:54:08 +02004219#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004220 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004221#endif
4222
Bram Moolenaardb913952012-06-29 12:54:53 +02004223#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004224 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004225#endif
4226
4227#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004228 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004229#endif
4230
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004231#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004232 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004233 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004234#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004235#ifdef FEAT_NETBEANS_INTG
4236 abort = abort || set_ref_in_nb_channel(copyID);
4237#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004238
Bram Moolenaare3188e22016-05-31 21:13:04 +02004239#ifdef FEAT_TIMERS
4240 abort = abort || set_ref_in_timer(copyID);
4241#endif
4242
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004243#ifdef FEAT_QUICKFIX
4244 abort = abort || set_ref_in_quickfix(copyID);
4245#endif
4246
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004247#ifdef FEAT_TERMINAL
4248 abort = abort || set_ref_in_term(copyID);
4249#endif
4250
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004251#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004252 abort = abort || set_ref_in_popups(copyID);
4253#endif
4254
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004255 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004256 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004257 /*
4258 * 2. Free lists and dictionaries that are not referenced.
4259 */
4260 did_free = free_unref_items(copyID);
4261
4262 /*
4263 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004264 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004265 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004266 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004267 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004268 else if (p_verbose > 0)
4269 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004270 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004271 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004272
4273 return did_free;
4274}
4275
4276/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004277 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004278 */
4279 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004280free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004281{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004282 int did_free = FALSE;
4283
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004284 // Let all "free" functions know that we are here. This means no
4285 // dictionaries, lists, channels or jobs are to be freed, because we will
4286 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004287 in_free_unref_items = TRUE;
4288
4289 /*
4290 * PASS 1: free the contents of the items. We don't free the items
4291 * themselves yet, so that it is possible to decrement refcount counters
4292 */
4293
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004294 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004295 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004296
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004297 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004298 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004299
4300#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004301 // Go through the list of jobs and free items without the copyID. This
4302 // must happen before doing channels, because jobs refer to channels, but
4303 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004304 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4305
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004306 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004307 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4308#endif
4309
4310 /*
4311 * PASS 2: free the items themselves.
4312 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004313 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004314 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004315
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004316#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004317 // Go through the list of jobs and free items without the copyID. This
4318 // must happen before doing channels, because jobs refer to channels, but
4319 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004320 free_unused_jobs(copyID, COPYID_MASK);
4321
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004322 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004323 free_unused_channels(copyID, COPYID_MASK);
4324#endif
4325
4326 in_free_unref_items = FALSE;
4327
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004328 return did_free;
4329}
4330
4331/*
4332 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004333 * "list_stack" is used to add lists to be marked. Can be NULL.
4334 *
4335 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004336 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004337 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004338set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004339{
4340 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004341 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004342 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004343 hashtab_T *cur_ht;
4344 ht_stack_T *ht_stack = NULL;
4345 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004346
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004347 cur_ht = ht;
4348 for (;;)
4349 {
4350 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004351 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004352 // Mark each item in the hashtab. If the item contains a hashtab
4353 // it is added to ht_stack, if it contains a list it is added to
4354 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004355 todo = (int)cur_ht->ht_used;
4356 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4357 if (!HASHITEM_EMPTY(hi))
4358 {
4359 --todo;
4360 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4361 &ht_stack, list_stack);
4362 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004363 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004364
4365 if (ht_stack == NULL)
4366 break;
4367
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004368 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004369 cur_ht = ht_stack->ht;
4370 tempitem = ht_stack;
4371 ht_stack = ht_stack->prev;
4372 free(tempitem);
4373 }
4374
4375 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004376}
4377
4378/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004379 * Mark a dict and its items with "copyID".
4380 * Returns TRUE if setting references failed somehow.
4381 */
4382 int
4383set_ref_in_dict(dict_T *d, int copyID)
4384{
4385 if (d != NULL && d->dv_copyID != copyID)
4386 {
4387 d->dv_copyID = copyID;
4388 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4389 }
4390 return FALSE;
4391}
4392
4393/*
4394 * Mark a list and its items with "copyID".
4395 * Returns TRUE if setting references failed somehow.
4396 */
4397 int
4398set_ref_in_list(list_T *ll, int copyID)
4399{
4400 if (ll != NULL && ll->lv_copyID != copyID)
4401 {
4402 ll->lv_copyID = copyID;
4403 return set_ref_in_list_items(ll, copyID, NULL);
4404 }
4405 return FALSE;
4406}
4407
4408/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004409 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004410 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4411 *
4412 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004413 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004414 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004415set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004416{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004417 listitem_T *li;
4418 int abort = FALSE;
4419 list_T *cur_l;
4420 list_stack_T *list_stack = NULL;
4421 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004422
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004423 cur_l = l;
4424 for (;;)
4425 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004426 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004427 // Mark each item in the list. If the item contains a hashtab
4428 // it is added to ht_stack, if it contains a list it is added to
4429 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004430 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4431 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4432 ht_stack, &list_stack);
4433 if (list_stack == NULL)
4434 break;
4435
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004436 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004437 cur_l = list_stack->list;
4438 tempitem = list_stack;
4439 list_stack = list_stack->prev;
4440 free(tempitem);
4441 }
4442
4443 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004444}
4445
4446/*
4447 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004448 * "list_stack" is used to add lists to be marked. Can be NULL.
4449 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4450 *
4451 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004452 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004453 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004454set_ref_in_item(
4455 typval_T *tv,
4456 int copyID,
4457 ht_stack_T **ht_stack,
4458 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004459{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004460 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004461
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004462 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004463 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004464 dict_T *dd = tv->vval.v_dict;
4465
Bram Moolenaara03f2332016-02-06 18:09:59 +01004466 if (dd != NULL && dd->dv_copyID != copyID)
4467 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004468 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004469 dd->dv_copyID = copyID;
4470 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004471 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004472 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4473 }
4474 else
4475 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004476 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4477
Bram Moolenaara03f2332016-02-06 18:09:59 +01004478 if (newitem == NULL)
4479 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004480 else
4481 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004482 newitem->ht = &dd->dv_hashtab;
4483 newitem->prev = *ht_stack;
4484 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004485 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004486 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004487 }
4488 }
4489 else if (tv->v_type == VAR_LIST)
4490 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004491 list_T *ll = tv->vval.v_list;
4492
Bram Moolenaara03f2332016-02-06 18:09:59 +01004493 if (ll != NULL && ll->lv_copyID != copyID)
4494 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004495 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004496 ll->lv_copyID = copyID;
4497 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004498 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004499 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004500 }
4501 else
4502 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004503 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4504
Bram Moolenaara03f2332016-02-06 18:09:59 +01004505 if (newitem == NULL)
4506 abort = TRUE;
4507 else
4508 {
4509 newitem->list = ll;
4510 newitem->prev = *list_stack;
4511 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004512 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004513 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004514 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004515 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004516 else if (tv->v_type == VAR_FUNC)
4517 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004518 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004519 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004520 else if (tv->v_type == VAR_PARTIAL)
4521 {
4522 partial_T *pt = tv->vval.v_partial;
4523 int i;
4524
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004525 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004526 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004527 // Didn't see this partial yet.
4528 pt->pt_copyID = copyID;
4529
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004530 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004531
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004532 if (pt->pt_dict != NULL)
4533 {
4534 typval_T dtv;
4535
4536 dtv.v_type = VAR_DICT;
4537 dtv.vval.v_dict = pt->pt_dict;
4538 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4539 }
4540
4541 for (i = 0; i < pt->pt_argc; ++i)
4542 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4543 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004544 if (pt->pt_funcstack != NULL)
4545 {
4546 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4547
4548 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4549 abort = abort || set_ref_in_item(stack + i, copyID,
4550 ht_stack, list_stack);
4551 }
4552
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004553 }
4554 }
4555#ifdef FEAT_JOB_CHANNEL
4556 else if (tv->v_type == VAR_JOB)
4557 {
4558 job_T *job = tv->vval.v_job;
4559 typval_T dtv;
4560
4561 if (job != NULL && job->jv_copyID != copyID)
4562 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004563 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004564 if (job->jv_channel != NULL)
4565 {
4566 dtv.v_type = VAR_CHANNEL;
4567 dtv.vval.v_channel = job->jv_channel;
4568 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4569 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004570 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004571 {
4572 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004573 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004574 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4575 }
4576 }
4577 }
4578 else if (tv->v_type == VAR_CHANNEL)
4579 {
4580 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004581 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004582 typval_T dtv;
4583 jsonq_T *jq;
4584 cbq_T *cq;
4585
4586 if (ch != NULL && ch->ch_copyID != copyID)
4587 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004588 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004589 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004590 {
4591 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4592 jq = jq->jq_next)
4593 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4594 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4595 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004596 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004597 {
4598 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004599 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004600 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4601 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004602 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004603 {
4604 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004605 dtv.vval.v_partial =
4606 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004607 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4608 }
4609 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004610 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004611 {
4612 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004613 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004614 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4615 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004616 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004617 {
4618 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004619 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004620 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4621 }
4622 }
4623 }
4624#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004625 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004626}
4627
Bram Moolenaar8c711452005-01-14 21:53:12 +00004628/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004629 * Return a string with the string representation of a variable.
4630 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004631 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004632 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004633 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4634 * stings as "string()", otherwise does not put quotes around strings, as
4635 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004636 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4637 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004638 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004639 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004640 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004641echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004642 typval_T *tv,
4643 char_u **tofree,
4644 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004645 int copyID,
4646 int echo_style,
4647 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004648 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004649{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004650 static int recurse = 0;
4651 char_u *r = NULL;
4652
Bram Moolenaar33570922005-01-25 22:26:29 +00004653 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004654 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004655 if (!did_echo_string_emsg)
4656 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004657 // Only give this message once for a recursive call to avoid
4658 // flooding the user with errors. And stop iterating over lists
4659 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004660 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004661 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004662 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004663 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004664 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004665 }
4666 ++recurse;
4667
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004668 switch (tv->v_type)
4669 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004670 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004671 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004672 {
4673 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004674 r = tv->vval.v_string;
4675 if (r == NULL)
4676 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004677 }
4678 else
4679 {
4680 *tofree = string_quote(tv->vval.v_string, FALSE);
4681 r = *tofree;
4682 }
4683 break;
4684
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004685 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004686 if (echo_style)
4687 {
4688 *tofree = NULL;
4689 r = tv->vval.v_string;
4690 }
4691 else
4692 {
4693 *tofree = string_quote(tv->vval.v_string, TRUE);
4694 r = *tofree;
4695 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004696 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004697
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004698 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004699 {
4700 partial_T *pt = tv->vval.v_partial;
4701 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004702 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004703 garray_T ga;
4704 int i;
4705 char_u *tf;
4706
4707 ga_init2(&ga, 1, 100);
4708 ga_concat(&ga, (char_u *)"function(");
4709 if (fname != NULL)
4710 {
4711 ga_concat(&ga, fname);
4712 vim_free(fname);
4713 }
4714 if (pt != NULL && pt->pt_argc > 0)
4715 {
4716 ga_concat(&ga, (char_u *)", [");
4717 for (i = 0; i < pt->pt_argc; ++i)
4718 {
4719 if (i > 0)
4720 ga_concat(&ga, (char_u *)", ");
4721 ga_concat(&ga,
4722 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4723 vim_free(tf);
4724 }
4725 ga_concat(&ga, (char_u *)"]");
4726 }
4727 if (pt != NULL && pt->pt_dict != NULL)
4728 {
4729 typval_T dtv;
4730
4731 ga_concat(&ga, (char_u *)", ");
4732 dtv.v_type = VAR_DICT;
4733 dtv.vval.v_dict = pt->pt_dict;
4734 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4735 vim_free(tf);
4736 }
4737 ga_concat(&ga, (char_u *)")");
4738
4739 *tofree = ga.ga_data;
4740 r = *tofree;
4741 break;
4742 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004743
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004744 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004745 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004746 break;
4747
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004748 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004749 if (tv->vval.v_list == NULL)
4750 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004751 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004752 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004753 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004754 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004755 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4756 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004757 {
4758 *tofree = NULL;
4759 r = (char_u *)"[...]";
4760 }
4761 else
4762 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004763 int old_copyID = tv->vval.v_list->lv_copyID;
4764
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004765 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004766 *tofree = list2string(tv, copyID, restore_copyID);
4767 if (restore_copyID)
4768 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004769 r = *tofree;
4770 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004771 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004772
Bram Moolenaar8c711452005-01-14 21:53:12 +00004773 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004774 if (tv->vval.v_dict == NULL)
4775 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004776 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004777 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004778 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004779 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004780 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4781 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004782 {
4783 *tofree = NULL;
4784 r = (char_u *)"{...}";
4785 }
4786 else
4787 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004788 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004789
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004790 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004791 *tofree = dict2string(tv, copyID, restore_copyID);
4792 if (restore_copyID)
4793 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004794 r = *tofree;
4795 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004796 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004797
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004798 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004799 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004800 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004801 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004802 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004803 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004804 break;
4805
Bram Moolenaar835dc632016-02-07 14:27:38 +01004806 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004807 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004808 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004809 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004810 if (composite_val)
4811 {
4812 *tofree = string_quote(r, FALSE);
4813 r = *tofree;
4814 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004815 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004816
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004817 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004818#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004819 *tofree = NULL;
4820 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4821 r = numbuf;
4822 break;
4823#endif
4824
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004825 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004826 case VAR_SPECIAL:
4827 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004828 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004829 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004830 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004831
Bram Moolenaar8502c702014-06-17 12:51:16 +02004832 if (--recurse == 0)
4833 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004834 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004835}
4836
4837/*
4838 * Return a string with the string representation of a variable.
4839 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4840 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004841 * Does not put quotes around strings, as ":echo" displays values.
4842 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4843 * May return NULL.
4844 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004845 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004846echo_string(
4847 typval_T *tv,
4848 char_u **tofree,
4849 char_u *numbuf,
4850 int copyID)
4851{
4852 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4853}
4854
4855/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004856 * Return string "str" in ' quotes, doubling ' characters.
4857 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004858 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004859 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004860 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004861string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004862{
Bram Moolenaar33570922005-01-25 22:26:29 +00004863 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004864 char_u *p, *r, *s;
4865
Bram Moolenaar33570922005-01-25 22:26:29 +00004866 len = (function ? 13 : 3);
4867 if (str != NULL)
4868 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004869 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004870 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004871 if (*p == '\'')
4872 ++len;
4873 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004874 s = r = alloc(len);
4875 if (r != NULL)
4876 {
4877 if (function)
4878 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004879 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004880 r += 10;
4881 }
4882 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004883 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004884 if (str != NULL)
4885 for (p = str; *p != NUL; )
4886 {
4887 if (*p == '\'')
4888 *r++ = '\'';
4889 MB_COPY_CHAR(p, r);
4890 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004891 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004892 if (function)
4893 *r++ = ')';
4894 *r++ = NUL;
4895 }
4896 return s;
4897}
4898
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004899#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004900/*
4901 * Convert the string "text" to a floating point number.
4902 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4903 * this always uses a decimal point.
4904 * Returns the length of the text that was consumed.
4905 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004906 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004907string2float(
4908 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004909 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004910{
4911 char *s = (char *)text;
4912 float_T f;
4913
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004914 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004915 if (STRNICMP(text, "inf", 3) == 0)
4916 {
4917 *value = INFINITY;
4918 return 3;
4919 }
4920 if (STRNICMP(text, "-inf", 3) == 0)
4921 {
4922 *value = -INFINITY;
4923 return 4;
4924 }
4925 if (STRNICMP(text, "nan", 3) == 0)
4926 {
4927 *value = NAN;
4928 return 3;
4929 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004930 f = strtod(s, &s);
4931 *value = f;
4932 return (int)((char_u *)s - text);
4933}
4934#endif
4935
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004936/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004938 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004940 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004941var2fpos(
4942 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004943 int dollar_lnum, // TRUE when $ is last line
4944 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004946 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004948 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004950 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004951 if (varp->v_type == VAR_LIST)
4952 {
4953 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004954 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004955 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004956 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004957
4958 l = varp->vval.v_list;
4959 if (l == NULL)
4960 return NULL;
4961
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004962 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004963 pos.lnum = list_find_nr(l, 0L, &error);
4964 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004965 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004966 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004967
Bram Moolenaarec65d772020-08-20 22:29:12 +02004968 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004969 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004970 li = list_find(l, 1L);
4971 if (li != NULL && li->li_tv.v_type == VAR_STRING
4972 && li->li_tv.vval.v_string != NULL
4973 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02004974 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004975 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02004976 }
4977 else
4978 {
4979 pos.col = list_find_nr(l, 1L, &error);
4980 if (error)
4981 return NULL;
4982 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00004983
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004984 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004985 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004986 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004987 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004988
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004989 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004990 pos.coladd = list_find_nr(l, 2L, &error);
4991 if (error)
4992 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004993
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004994 return &pos;
4995 }
4996
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004997 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004998 if (name == NULL)
4999 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005000 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005002 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005003 {
5004 if (VIsual_active)
5005 return &VIsual;
5006 return &curwin->w_cursor;
5007 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005008 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005010 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5012 return NULL;
5013 return pp;
5014 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005015
Bram Moolenaara5525202006-03-02 22:52:09 +00005016 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005017
Bram Moolenaar477933c2007-07-17 14:32:23 +00005018 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005019 {
5020 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005021 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005022 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005023 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005024 // In silent Ex mode topline is zero, but that's not a valid line
5025 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005026 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005027 return &pos;
5028 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005029 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005030 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005031 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005032 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005033 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005034 return &pos;
5035 }
5036 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005037 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005039 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 {
5041 pos.lnum = curbuf->b_ml.ml_line_count;
5042 pos.col = 0;
5043 }
5044 else
5045 {
5046 pos.lnum = curwin->w_cursor.lnum;
5047 pos.col = (colnr_T)STRLEN(ml_get_curline());
5048 }
5049 return &pos;
5050 }
5051 return NULL;
5052}
5053
5054/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005055 * Convert list in "arg" into a position and optional file number.
5056 * When "fnump" is NULL there is no file number, only 3 items.
5057 * Note that the column is passed on as-is, the caller may want to decrement
5058 * it to use 1 for the first column.
5059 * Return FAIL when conversion is not possible, doesn't check the position for
5060 * validity.
5061 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005062 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005063list2fpos(
5064 typval_T *arg,
5065 pos_T *posp,
5066 int *fnump,
5067 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005068{
5069 list_T *l = arg->vval.v_list;
5070 long i = 0;
5071 long n;
5072
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005073 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5074 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005075 if (arg->v_type != VAR_LIST
5076 || l == NULL
5077 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005078 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005079 return FAIL;
5080
5081 if (fnump != NULL)
5082 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005083 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005084 if (n < 0)
5085 return FAIL;
5086 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005087 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005088 *fnump = n;
5089 }
5090
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005091 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005092 if (n < 0)
5093 return FAIL;
5094 posp->lnum = n;
5095
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005096 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005097 if (n < 0)
5098 return FAIL;
5099 posp->col = n;
5100
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005101 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005102 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005103 posp->coladd = 0;
5104 else
5105 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005106
Bram Moolenaar493c1782014-05-28 14:34:46 +02005107 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005108 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005109
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005110 return OK;
5111}
5112
5113/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 * Get the length of an environment variable name.
5115 * Advance "arg" to the first character after the name.
5116 * Return 0 for error.
5117 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005118 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005119get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120{
5121 char_u *p;
5122 int len;
5123
5124 for (p = *arg; vim_isIDc(*p); ++p)
5125 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005126 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 return 0;
5128
5129 len = (int)(p - *arg);
5130 *arg = p;
5131 return len;
5132}
5133
5134/*
5135 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005136 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 * Return 0 if something is wrong.
5138 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005139 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005140get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141{
5142 char_u *p;
5143 int len;
5144
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005145 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005147 {
5148 if (*p == ':')
5149 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005150 // "s:" is start of "s:var", but "n:" is not and can be used in
5151 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005152 len = (int)(p - *arg);
5153 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5154 || len > 1)
5155 break;
5156 }
5157 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005158 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 return 0;
5160
5161 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005162 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163
5164 return len;
5165}
5166
5167/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005168 * Get the length of the name of a variable or function.
5169 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005171 * Return -1 if curly braces expansion failed.
5172 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 * If the name contains 'magic' {}'s, expand them and return the
5174 * expanded name in an allocated string via 'alias' - caller must free.
5175 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005176 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005177get_name_len(
5178 char_u **arg,
5179 char_u **alias,
5180 int evaluate,
5181 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182{
5183 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 char_u *p;
5185 char_u *expr_start;
5186 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005188 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189
5190 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5191 && (*arg)[2] == (int)KE_SNR)
5192 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005193 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 *arg += 3;
5195 return get_id_len(arg) + 3;
5196 }
5197 len = eval_fname_script(*arg);
5198 if (len > 0)
5199 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005200 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 *arg += len;
5202 }
5203
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005205 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005207 p = find_name_end(*arg, &expr_start, &expr_end,
5208 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 if (expr_start != NULL)
5210 {
5211 char_u *temp_string;
5212
5213 if (!evaluate)
5214 {
5215 len += (int)(p - *arg);
5216 *arg = skipwhite(p);
5217 return len;
5218 }
5219
5220 /*
5221 * Include any <SID> etc in the expanded string:
5222 * Thus the -len here.
5223 */
5224 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5225 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005226 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 *alias = temp_string;
5228 *arg = skipwhite(p);
5229 return (int)STRLEN(temp_string);
5230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231
5232 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005233 // Only give an error when there is something, otherwise it will be
5234 // reported at a higher level.
5235 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005236 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237
5238 return len;
5239}
5240
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005241/*
5242 * Find the end of a variable or function name, taking care of magic braces.
5243 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5244 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005245 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005246 * Return a pointer to just after the name. Equal to "arg" if there is no
5247 * valid name.
5248 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005249 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005250find_name_end(
5251 char_u *arg,
5252 char_u **expr_start,
5253 char_u **expr_end,
5254 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005256 int mb_nest = 0;
5257 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005259 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005260 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005262 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005264 *expr_start = NULL;
5265 *expr_end = NULL;
5266 }
5267
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005268 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005269 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5270 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005271 return arg;
5272
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005273 for (p = arg; *p != NUL
5274 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005275 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005276 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005277 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005278 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005279 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005280 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005281 if (*p == '\'')
5282 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005283 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005284 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005285 ;
5286 if (*p == NUL)
5287 break;
5288 }
5289 else if (*p == '"')
5290 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005291 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005292 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005293 if (*p == '\\' && p[1] != NUL)
5294 ++p;
5295 if (*p == NUL)
5296 break;
5297 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005298 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5299 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005300 // "s:" is start of "s:var", but "n:" is not and can be used in
5301 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005302 len = (int)(p - arg);
5303 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005304 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005305 break;
5306 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005307
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005308 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005310 if (*p == '[')
5311 ++br_nest;
5312 else if (*p == ']')
5313 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005315
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005316 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005318 if (*p == '{')
5319 {
5320 mb_nest++;
5321 if (expr_start != NULL && *expr_start == NULL)
5322 *expr_start = p;
5323 }
5324 else if (*p == '}')
5325 {
5326 mb_nest--;
5327 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5328 *expr_end = p;
5329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 }
5332
5333 return p;
5334}
5335
5336/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005337 * Expands out the 'magic' {}'s in a variable/function name.
5338 * Note that this can call itself recursively, to deal with
5339 * constructs like foo{bar}{baz}{bam}
5340 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5341 * "in_start" ^
5342 * "expr_start" ^
5343 * "expr_end" ^
5344 * "in_end" ^
5345 *
5346 * Returns a new allocated string, which the caller must free.
5347 * Returns NULL for failure.
5348 */
5349 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005350make_expanded_name(
5351 char_u *in_start,
5352 char_u *expr_start,
5353 char_u *expr_end,
5354 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005355{
5356 char_u c1;
5357 char_u *retval = NULL;
5358 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005359
5360 if (expr_end == NULL || in_end == NULL)
5361 return NULL;
5362 *expr_start = NUL;
5363 *expr_end = NUL;
5364 c1 = *in_end;
5365 *in_end = NUL;
5366
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005367 temp_result = eval_to_string(expr_start + 1, FALSE);
5368 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005369 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005370 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5371 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005372 if (retval != NULL)
5373 {
5374 STRCPY(retval, in_start);
5375 STRCAT(retval, temp_result);
5376 STRCAT(retval, expr_end + 1);
5377 }
5378 }
5379 vim_free(temp_result);
5380
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005381 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005382 *expr_start = '{';
5383 *expr_end = '}';
5384
5385 if (retval != NULL)
5386 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005387 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005388 if (expr_start != NULL)
5389 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005390 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005391 temp_result = make_expanded_name(retval, expr_start,
5392 expr_end, temp_result);
5393 vim_free(retval);
5394 retval = temp_result;
5395 }
5396 }
5397
5398 return retval;
5399}
5400
5401/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005403 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005405 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005406eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005408 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005409}
5410
5411/*
5412 * Return TRUE if character "c" can be used as the first character in a
5413 * variable or function name (excluding '{' and '}').
5414 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005415 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005416eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005417{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005418 return ASCII_ISALPHA(c) || c == '_';
5419}
5420
5421/*
5422 * Return TRUE if character "c" can be used as the first character of a
5423 * dictionary key.
5424 */
5425 int
5426eval_isdictc(int c)
5427{
5428 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429}
5430
5431/*
Bram Moolenaare3c37d82020-08-15 18:39:05 +02005432 * Return the character "str[index]" where "index" is the character index. If
5433 * "index" is out of range NULL is returned.
5434 */
5435 char_u *
5436char_from_string(char_u *str, varnumber_T index)
5437{
5438 size_t nbyte = 0;
5439 varnumber_T nchar = index;
5440 size_t slen;
5441
5442 if (str == NULL || index < 0)
5443 return NULL;
5444 slen = STRLEN(str);
5445 while (nchar > 0 && nbyte < slen)
5446 {
5447 nbyte += MB_CPTR2LEN(str + nbyte);
5448 --nchar;
5449 }
5450 if (nbyte >= slen)
5451 return NULL;
5452 return vim_strnsave(str + nbyte, MB_CPTR2LEN(str + nbyte));
5453}
5454
5455/*
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005456 * Get the byte index for character index "idx" in string "str" with length
5457 * "str_len".
5458 * If going over the end return "str_len".
5459 * If "idx" is negative count from the end, -1 is the last character.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005460 * When going over the start return -1.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005461 */
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005462 static long
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005463char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
5464{
5465 varnumber_T nchar = idx;
5466 size_t nbyte = 0;
5467
5468 if (nchar >= 0)
5469 {
5470 while (nchar > 0 && nbyte < str_len)
5471 {
5472 nbyte += MB_CPTR2LEN(str + nbyte);
5473 --nchar;
5474 }
5475 }
5476 else
5477 {
5478 nbyte = str_len;
5479 while (nchar < 0 && nbyte > 0)
5480 {
5481 --nbyte;
5482 nbyte -= mb_head_off(str, str + nbyte);
5483 ++nchar;
5484 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005485 if (nchar < 0)
5486 return -1;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005487 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005488 return (long)nbyte;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005489}
5490
5491/*
5492 * Return the slice "str[first:last]" using character indexes.
5493 * Return NULL when the result is empty.
5494 */
5495 char_u *
5496string_slice(char_u *str, varnumber_T first, varnumber_T last)
5497{
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005498 long start_byte, end_byte;
5499 size_t slen;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005500
5501 if (str == NULL)
5502 return NULL;
5503 slen = STRLEN(str);
5504 start_byte = char_idx2byte(str, slen, first);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005505 if (start_byte < 0)
5506 start_byte = 0; // first index very negative: use zero
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005507 if (last == -1)
Bram Moolenaar25660542020-08-28 16:38:11 +02005508 end_byte = (long)slen;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005509 else
5510 {
5511 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005512 if (end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005513 // end index is inclusive
5514 end_byte += MB_CPTR2LEN(str + end_byte);
5515 }
5516
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005517 if (start_byte >= (long)slen || end_byte <= start_byte)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005518 return NULL;
5519 return vim_strnsave(str + start_byte, end_byte - start_byte);
5520}
5521
5522/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005523 * Handle:
5524 * - expr[expr], expr[expr:expr] subscript
5525 * - ".name" lookup
5526 * - function call with Funcref variable: func(expr)
5527 * - method call: var->method()
5528 *
5529 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005530 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005531 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005532handle_subscript(
5533 char_u **arg,
5534 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005535 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005536 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005537{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005538 int evaluate = evalarg != NULL
5539 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005540 int ret = OK;
5541 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005542 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005543 int getnext;
5544 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005545
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005546 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005547 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005548 // When at the end of the line and ".name" or "->{" or "->X" follows in
5549 // the next line then consume the line break.
5550 p = eval_next_non_blank(*arg, evalarg, &getnext);
5551 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005552 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaar9d489562020-07-30 20:08:50 +02005553 || (p[0] == '-' && p[1] == '>'
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005554 && (p[2] == '{' || ASCII_ISALPHA(p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005555 {
5556 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005557 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005558 check_white = FALSE;
5559 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005560
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005561 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5562 || rettv->v_type == VAR_PARTIAL))
5563 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005564 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005565 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5566 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005567
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005568 // Stop the expression evaluation when immediately aborting on
5569 // error, or when an interrupt occurred or an exception was thrown
5570 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005571 if (aborting())
5572 {
5573 if (ret == OK)
5574 clear_tv(rettv);
5575 ret = FAIL;
5576 }
5577 dict_unref(selfdict);
5578 selfdict = NULL;
5579 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005580 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005581 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02005582 *arg = p;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005583 if (ret == OK)
5584 {
5585 if ((*arg)[2] == '{')
5586 // expr->{lambda}()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005587 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005588 else
5589 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005590 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005591 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005592 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005593 // "." is ".name" lookup when we found a dict or when evaluating and
5594 // scriptversion is at least 2, where string concatenation is "..".
5595 else if (**arg == '['
5596 || (**arg == '.' && (rettv->v_type == VAR_DICT
5597 || (!evaluate
5598 && (*arg)[1] != '.'
5599 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005600 {
5601 dict_unref(selfdict);
5602 if (rettv->v_type == VAR_DICT)
5603 {
5604 selfdict = rettv->vval.v_dict;
5605 if (selfdict != NULL)
5606 ++selfdict->dv_refcount;
5607 }
5608 else
5609 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005610 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005611 {
5612 clear_tv(rettv);
5613 ret = FAIL;
5614 }
5615 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005616 else
5617 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005618 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005619
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005620 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5621 // Don't do this when "Func" is already a partial that was bound
5622 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005623 if (selfdict != NULL
5624 && (rettv->v_type == VAR_FUNC
5625 || (rettv->v_type == VAR_PARTIAL
5626 && (rettv->vval.v_partial->pt_auto
5627 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005628 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005629
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005630 dict_unref(selfdict);
5631 return ret;
5632}
5633
5634/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005635 * Make a copy of an item.
5636 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005637 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5638 * reference to an already copied list/dict can be used.
5639 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005640 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005641 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005642item_copy(
5643 typval_T *from,
5644 typval_T *to,
5645 int deep,
5646 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005647{
5648 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005649 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005650
Bram Moolenaar33570922005-01-25 22:26:29 +00005651 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005652 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005653 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005654 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005655 }
5656 ++recurse;
5657
5658 switch (from->v_type)
5659 {
5660 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005661 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005662 case VAR_STRING:
5663 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005664 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005665 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005666 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005667 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005668 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005669 copy_tv(from, to);
5670 break;
5671 case VAR_LIST:
5672 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005673 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005674 if (from->vval.v_list == NULL)
5675 to->vval.v_list = NULL;
5676 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5677 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005678 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005679 to->vval.v_list = from->vval.v_list->lv_copylist;
5680 ++to->vval.v_list->lv_refcount;
5681 }
5682 else
5683 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5684 if (to->vval.v_list == NULL)
5685 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005686 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005687 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005688 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005689 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005690 case VAR_DICT:
5691 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005692 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005693 if (from->vval.v_dict == NULL)
5694 to->vval.v_dict = NULL;
5695 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5696 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005697 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005698 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5699 ++to->vval.v_dict->dv_refcount;
5700 }
5701 else
5702 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5703 if (to->vval.v_dict == NULL)
5704 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005705 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005706 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005707 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005708 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005709 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005710 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005711 }
5712 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005713 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005714}
5715
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005716 void
5717echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5718{
5719 char_u *tofree;
5720 char_u numbuf[NUMBUFLEN];
5721 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5722
5723 if (*atstart)
5724 {
5725 *atstart = FALSE;
5726 // Call msg_start() after eval1(), evaluating the expression
5727 // may cause a message to appear.
5728 if (with_space)
5729 {
5730 // Mark the saved text as finishing the line, so that what
5731 // follows is displayed on a new line when scrolling back
5732 // at the more prompt.
5733 msg_sb_eol();
5734 msg_start();
5735 }
5736 }
5737 else if (with_space)
5738 msg_puts_attr(" ", echo_attr);
5739
5740 if (p != NULL)
5741 for ( ; *p != NUL && !got_int; ++p)
5742 {
5743 if (*p == '\n' || *p == '\r' || *p == TAB)
5744 {
5745 if (*p != TAB && *needclr)
5746 {
5747 // remove any text still there from the command
5748 msg_clr_eos();
5749 *needclr = FALSE;
5750 }
5751 msg_putchar_attr(*p, echo_attr);
5752 }
5753 else
5754 {
5755 if (has_mbyte)
5756 {
5757 int i = (*mb_ptr2len)(p);
5758
5759 (void)msg_outtrans_len_attr(p, i, echo_attr);
5760 p += i - 1;
5761 }
5762 else
5763 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5764 }
5765 }
5766 vim_free(tofree);
5767}
5768
Bram Moolenaare9a41262005-01-15 22:18:47 +00005769/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 * ":echo expr1 ..." print each argument separated with a space, add a
5771 * newline at the end.
5772 * ":echon expr1 ..." print each argument plain.
5773 */
5774 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005775ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776{
5777 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005778 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 char_u *p;
5780 int needclr = TRUE;
5781 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01005782 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005783 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005784 evalarg_T evalarg;
5785
Bram Moolenaare707c882020-07-01 13:07:37 +02005786 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787
5788 if (eap->skip)
5789 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005790 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005792 // If eval1() causes an error message the text from the command may
5793 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005794 need_clr_eos = needclr;
5795
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005797 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 {
5799 /*
5800 * Report the invalid expression unless the expression evaluation
5801 * has been cancelled due to an aborting error, an interrupt, or an
5802 * exception.
5803 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005804 if (!aborting() && did_emsg == did_emsg_before
5805 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005806 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005807 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 break;
5809 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005810 need_clr_eos = FALSE;
5811
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005813 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005814
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005815 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 arg = skipwhite(arg);
5817 }
5818 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02005819 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820
5821 if (eap->skip)
5822 --emsg_skip;
5823 else
5824 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005825 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 if (needclr)
5827 msg_clr_eos();
5828 if (eap->cmdidx == CMD_echo)
5829 msg_end();
5830 }
5831}
5832
5833/*
5834 * ":echohl {name}".
5835 */
5836 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005837ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005839 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840}
5841
5842/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005843 * Returns the :echo attribute
5844 */
5845 int
5846get_echo_attr(void)
5847{
5848 return echo_attr;
5849}
5850
5851/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 * ":execute expr1 ..." execute the result of an expression.
5853 * ":echomsg expr1 ..." Print a message
5854 * ":echoerr expr1 ..." Print an error
5855 * Each gets spaces around each argument and a newline at the end for
5856 * echo commands
5857 */
5858 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005859ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860{
5861 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005862 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 int ret = OK;
5864 char_u *p;
5865 garray_T ga;
5866 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867
5868 ga_init2(&ga, 1, 80);
5869
5870 if (eap->skip)
5871 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02005872 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02005874 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01005875 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877
5878 if (!eap->skip)
5879 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005880 char_u buf[NUMBUFLEN];
5881
5882 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01005883 {
5884 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
5885 {
5886 emsg(_(e_inval_string));
5887 p = NULL;
5888 }
5889 else
5890 p = tv_get_string_buf(&rettv, buf);
5891 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005892 else
5893 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005894 if (p == NULL)
5895 {
5896 clear_tv(&rettv);
5897 ret = FAIL;
5898 break;
5899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 len = (int)STRLEN(p);
5901 if (ga_grow(&ga, len + 2) == FAIL)
5902 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005903 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 ret = FAIL;
5905 break;
5906 }
5907 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 ga.ga_len += len;
5911 }
5912
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005913 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 arg = skipwhite(arg);
5915 }
5916
5917 if (ret != FAIL && ga.ga_data != NULL)
5918 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005919 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
5920 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005921 // Mark the already saved text as finishing the line, so that what
5922 // follows is displayed on a new line when scrolling back at the
5923 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005924 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005925 }
5926
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005928 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005929 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005930 out_flush();
5931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 else if (eap->cmdidx == CMD_echoerr)
5933 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005934 int save_did_emsg = did_emsg;
5935
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005936 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005937 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 if (!force_abort)
5939 did_emsg = save_did_emsg;
5940 }
5941 else if (eap->cmdidx == CMD_execute)
5942 do_cmdline((char_u *)ga.ga_data,
5943 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
5944 }
5945
5946 ga_clear(&ga);
5947
5948 if (eap->skip)
5949 --emsg_skip;
5950
5951 eap->nextcmd = check_nextcmd(arg);
5952}
5953
5954/*
5955 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
5956 * "arg" points to the "&" or '+' when called, to "option" when returning.
5957 * Returns NULL when no option name found. Otherwise pointer to the char
5958 * after the option name.
5959 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005960 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005961find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962{
5963 char_u *p = *arg;
5964
5965 ++p;
5966 if (*p == 'g' && p[1] == ':')
5967 {
5968 *opt_flags = OPT_GLOBAL;
5969 p += 2;
5970 }
5971 else if (*p == 'l' && p[1] == ':')
5972 {
5973 *opt_flags = OPT_LOCAL;
5974 p += 2;
5975 }
5976 else
5977 *opt_flags = 0;
5978
5979 if (!ASCII_ISALPHA(*p))
5980 return NULL;
5981 *arg = p;
5982
5983 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005984 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 else
5986 while (ASCII_ISALPHA(*p))
5987 ++p;
5988 return p;
5989}
5990
5991/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00005992 * Display script name where an item was last set.
5993 * Should only be invoked when 'verbose' is non-zero.
5994 */
5995 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005996last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005997{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005998 char_u *p;
5999
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006000 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006001 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006002 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006003 if (p != NULL)
6004 {
6005 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006006 msg_puts(_("\n\tLast set from "));
6007 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006008 if (script_ctx.sc_lnum > 0)
6009 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006010 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006011 msg_outnum((long)script_ctx.sc_lnum);
6012 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006013 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006014 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006015 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006016 }
6017}
6018
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006019#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020
6021/*
6022 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006023 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 * "flags" can be "g" to do a global substitute.
6025 * Returns an allocated string, NULL for error.
6026 */
6027 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006028do_string_sub(
6029 char_u *str,
6030 char_u *pat,
6031 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006032 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006033 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034{
6035 int sublen;
6036 regmatch_T regmatch;
6037 int i;
6038 int do_all;
6039 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006040 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 garray_T ga;
6042 char_u *ret;
6043 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006044 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006046 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006048 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049
6050 ga_init2(&ga, 1, 200);
6051
6052 do_all = (flags[0] == 'g');
6053
6054 regmatch.rm_ic = p_ic;
6055 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6056 if (regmatch.regprog != NULL)
6057 {
6058 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006059 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6061 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006062 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006063 if (regmatch.startp[0] == regmatch.endp[0])
6064 {
6065 if (zero_width == regmatch.startp[0])
6066 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006067 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006068 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006069 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6070 (size_t)i);
6071 ga.ga_len += i;
6072 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006073 continue;
6074 }
6075 zero_width = regmatch.startp[0];
6076 }
6077
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 /*
6079 * Get some space for a temporary buffer to do the substitution
6080 * into. It will contain:
6081 * - The text up to where the match is.
6082 * - The substituted text.
6083 * - The text after the match.
6084 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006085 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006086 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6088 {
6089 ga_clear(&ga);
6090 break;
6091 }
6092
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006093 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 i = (int)(regmatch.startp[0] - tail);
6095 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006096 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006097 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 + ga.ga_len + i, TRUE, TRUE, FALSE);
6099 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006100 tail = regmatch.endp[0];
6101 if (*tail == NUL)
6102 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 if (!do_all)
6104 break;
6105 }
6106
6107 if (ga.ga_data != NULL)
6108 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6109
Bram Moolenaar473de612013-06-08 18:19:48 +02006110 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 }
6112
6113 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6114 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006115 if (p_cpo == empty_option)
6116 p_cpo = save_cpo;
6117 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006118 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006119 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120
6121 return ret;
6122}