blob: e61207048ccf9782d00cad2ebc7ac00b64e4a34e [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010023#define NAMESPACE_CHAR (char_u *)"abglstvw"
24
Bram Moolenaar532c7802005-01-27 14:44:31 +000025/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000026 * When recursively copying lists and dicts we need to remember which ones we
27 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000028 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000029 */
30static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020031
Bram Moolenaar071d4272004-06-13 20:20:40 +000032/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000033 * Info used by a ":for" loop.
34 */
Bram Moolenaar33570922005-01-25 22:26:29 +000035typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000036{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010037 int fi_semicolon; // TRUE if ending in '; var]'
38 int fi_varcount; // nr of variables in the list
Bram Moolenaarb7a78f72020-06-28 18:43:40 +020039 int fi_break_count; // nr of line breaks encountered
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010040 listwatch_T fi_lw; // keep an eye on the item used.
41 list_T *fi_list; // list being used
42 int fi_bi; // index of blob
43 blob_T *fi_blob; // blob being used
Bram Moolenaar33570922005-01-25 22:26:29 +000044} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000045
Bram Moolenaar48e697e2016-01-23 22:17:30 +010046static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020047static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
48static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
49static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
50static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
51static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
52static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +020053static int eval7_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000054
Bram Moolenaar48e697e2016-01-23 22:17:30 +010055static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020056static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020057
Bram Moolenaare21c1582019-03-02 11:57:09 +010058/*
59 * Return "n1" divided by "n2", taking care of dividing by zero.
60 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020061 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010062num_divide(varnumber_T n1, varnumber_T n2)
63{
64 varnumber_T result;
65
66 if (n2 == 0) // give an error message?
67 {
68 if (n1 == 0)
69 result = VARNUM_MIN; // similar to NaN
70 else if (n1 < 0)
71 result = -VARNUM_MAX;
72 else
73 result = VARNUM_MAX;
74 }
75 else
76 result = n1 / n2;
77
78 return result;
79}
80
81/*
82 * Return "n1" modulus "n2", taking care of dividing by zero.
83 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020084 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010085num_modulus(varnumber_T n1, varnumber_T n2)
86{
87 // Give an error when n2 is 0?
88 return (n2 == 0) ? 0 : (n1 % n2);
89}
90
Bram Moolenaara1fa8922017-01-12 20:06:33 +010091#if defined(EBCDIC) || defined(PROTO)
92/*
93 * Compare struct fst by function name.
94 */
95 static int
96compare_func_name(const void *s1, const void *s2)
97{
98 struct fst *p1 = (struct fst *)s1;
99 struct fst *p2 = (struct fst *)s2;
100
101 return STRCMP(p1->f_name, p2->f_name);
102}
103
104/*
105 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100106 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100107 * On machines using EBCDIC we have to sort it.
108 */
109 static void
110sortFunctions(void)
111{
112 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
113
114 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
115}
116#endif
117
Bram Moolenaar33570922005-01-25 22:26:29 +0000118/*
119 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000120 */
121 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100122eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000123{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200124 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200125 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000126
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200127#ifdef EBCDIC
128 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100129 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200130 */
131 sortFunctions();
132#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000133}
134
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000135#if defined(EXITFREE) || defined(PROTO)
136 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100137eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000138{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200139 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100140 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200141 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000142
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200143 // autoloaded script names
144 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000145
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200146 // unreferenced lists and dicts
147 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200148
149 // functions not garbage collected
150 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000151}
152#endif
153
Bram Moolenaare6b53242020-07-01 17:28:33 +0200154 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200155fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
156{
157 CLEAR_FIELD(*evalarg);
158 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
159 if (eap != NULL && getline_equal(eap->getline, eap->cookie, getsourceline))
160 {
161 evalarg->eval_getline = eap->getline;
162 evalarg->eval_cookie = eap->cookie;
163 }
164}
165
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166/*
167 * Top level evaluation function, returning a boolean.
168 * Sets "error" to TRUE if there was an error.
169 * Return TRUE or FALSE.
170 */
171 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100172eval_to_bool(
173 char_u *arg,
174 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200175 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100176 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177{
Bram Moolenaar33570922005-01-25 22:26:29 +0000178 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200179 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200180 evalarg_T evalarg;
181
Bram Moolenaar37c83712020-06-30 21:18:36 +0200182 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183
184 if (skip)
185 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200186 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 else
189 {
190 *error = FALSE;
191 if (!skip)
192 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200193 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200194 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200195 else
196 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000197 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 }
199 }
200 if (skip)
201 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200202 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200204 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205}
206
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100207/*
208 * Call eval1() and give an error message if not done at a lower level.
209 */
210 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200211eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100212{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100213 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100214 int ret;
215 int did_emsg_before = did_emsg;
216 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200217 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100218
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200219 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
220
221 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100222 if (ret == FAIL)
223 {
224 // Report the invalid expression unless the expression evaluation has
225 // been cancelled due to an aborting error, an interrupt, or an
226 // exception, or we already gave a more specific error.
227 // Also check called_emsg for when using assert_fails().
228 if (!aborting() && did_emsg == did_emsg_before
229 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100230 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100231 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200232 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100233 return ret;
234}
235
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100236/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200237 * Return whether a typval is a valid expression to pass to eval_expr_typval()
238 * or eval_expr_to_bool(). An empty string returns FALSE;
239 */
240 int
241eval_expr_valid_arg(typval_T *tv)
242{
243 return tv->v_type != VAR_UNKNOWN
244 && (tv->v_type != VAR_STRING
245 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
246}
247
248/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100249 * Evaluate an expression, which can be a function, partial or string.
250 * Pass arguments "argv[argc]".
251 * Return the result in "rettv" and OK or FAIL.
252 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200253 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100254eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
255{
256 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100257 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200258 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100259
260 if (expr->v_type == VAR_FUNC)
261 {
262 s = expr->vval.v_string;
263 if (s == NULL || *s == NUL)
264 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200265 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200266 funcexe.evaluate = TRUE;
267 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100268 return FAIL;
269 }
270 else if (expr->v_type == VAR_PARTIAL)
271 {
272 partial_T *partial = expr->vval.v_partial;
273
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200274 if (partial == NULL)
275 return FAIL;
276
Bram Moolenaar822ba242020-05-24 23:00:18 +0200277 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200278 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100279 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200280 if (call_def_function(partial->pt_func, argc, argv,
281 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100282 return FAIL;
283 }
284 else
285 {
286 s = partial_name(partial);
287 if (s == NULL || *s == NUL)
288 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200289 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100290 funcexe.evaluate = TRUE;
291 funcexe.partial = partial;
292 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
293 return FAIL;
294 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100295 }
296 else
297 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100298 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100299 if (s == NULL)
300 return FAIL;
301 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200302 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100303 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200304 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100305 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200306 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100307 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100308 return FAIL;
309 }
310 }
311 return OK;
312}
313
314/*
315 * Like eval_to_bool() but using a typval_T instead of a string.
316 * Works for string, funcref and partial.
317 */
318 int
319eval_expr_to_bool(typval_T *expr, int *error)
320{
321 typval_T rettv;
322 int res;
323
324 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
325 {
326 *error = TRUE;
327 return FALSE;
328 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200329 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100330 clear_tv(&rettv);
331 return res;
332}
333
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334/*
335 * Top level evaluation function, returning a string. If "skip" is TRUE,
336 * only parsing to "nextcmd" is done, without reporting errors. Return
337 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
338 */
339 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100340eval_to_string_skip(
341 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200342 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100343 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344{
Bram Moolenaar33570922005-01-25 22:26:29 +0000345 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200347 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348
Bram Moolenaar37c83712020-06-30 21:18:36 +0200349 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 if (skip)
351 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200352 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 retval = NULL;
354 else
355 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100356 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000357 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 }
359 if (skip)
360 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200361 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362
363 return retval;
364}
365
366/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000367 * Skip over an expression at "*pp".
368 * Return FAIL for an error, OK otherwise.
369 */
370 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200371skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000372{
Bram Moolenaar33570922005-01-25 22:26:29 +0000373 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000374
375 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200376 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000377}
378
379/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200380 * Skip over an expression at "*pp".
381 * If in Vim9 script and line breaks are encountered, the lines are
382 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200383 * "arg" is advanced to just after the expression.
384 * "start" is set to the start of the expression, "end" to just after the end.
385 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200386 * Return FAIL for an error, OK otherwise.
387 */
388 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200389skip_expr_concatenate(
390 char_u **arg,
391 char_u **start,
392 char_u **end,
393 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200394{
395 typval_T rettv;
396 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200397 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200398 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200399 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200400 int evaluate = evalarg == NULL
401 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200402
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200403 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200404 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200405 {
406 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200407 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200408 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200409 ++gap->ga_len;
410 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200411 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200412
413 // Don't evaluate the expression.
414 if (evalarg != NULL)
415 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200416 *arg = skipwhite(*arg);
417 res = eval1(arg, &rettv, evalarg);
418 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200419 if (evalarg != NULL)
420 evalarg->eval_flags = save_flags;
421
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200422 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200423 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200424 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200425 if (evalarg->eval_ga.ga_len == 1)
426 {
427 // just one line, no need to concatenate
428 ga_clear(gap);
429 gap->ga_itemsize = 0;
430 }
431 else
432 {
433 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200434 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200435
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200436 // Line breaks encountered, concatenate all the lines.
437 *((char_u **)gap->ga_data) = *start;
438 p = ga_concat_strings(gap, "");
439
440 // free the lines only when using getsourceline()
441 if (evalarg->eval_cookie != NULL)
442 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200443 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200444 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200445 // Do not free the last line, "arg" points into it, free it
446 // later.
447 vim_free(evalarg->eval_tofree);
448 evalarg->eval_tofree =
449 ((char_u **)gap->ga_data)[gap->ga_len - 1];
450 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200451 ga_clear_strings(gap);
452 }
453 else
454 ga_clear(gap);
455 gap->ga_itemsize = 0;
456 if (p == NULL)
457 return FAIL;
458 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200459 vim_free(evalarg->eval_tofree_lambda);
460 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200461 // Compute "end" relative to the end.
462 *end = *start + STRLEN(*start) - endoff;
463 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200464 }
465
466 return res;
467}
468
469/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200470 * Top level evaluation function, returning a string. Does not handle line
471 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000472 * When "convert" is TRUE convert a List into a sequence of lines and convert
473 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 * Return pointer to allocated memory, or NULL for failure.
475 */
476 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100477eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100478 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100479 int convert,
480 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481{
Bram Moolenaar33570922005-01-25 22:26:29 +0000482 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000484 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000485#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000486 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000487#endif
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100488 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100490 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
491 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 retval = NULL;
493 else
494 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000495 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000496 {
497 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000498 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200499 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200500 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200501 if (tv.vval.v_list->lv_len > 0)
502 ga_append(&ga, NL);
503 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000504 ga_append(&ga, NUL);
505 retval = (char_u *)ga.ga_data;
506 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000507#ifdef FEAT_FLOAT
508 else if (convert && tv.v_type == VAR_FLOAT)
509 {
510 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
511 retval = vim_strsave(numbuf);
512 }
513#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000514 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100515 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000516 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100518 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519
520 return retval;
521}
522
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100523 char_u *
524eval_to_string(
525 char_u *arg,
526 int convert)
527{
528 return eval_to_string_eap(arg, convert, NULL);
529}
530
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000532 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200533 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200534 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 */
536 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100537eval_to_string_safe(
538 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100539 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540{
541 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200542 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200543 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200545 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200546 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000547 if (use_sandbox)
548 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200549 ++textwinlock;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200550 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000551 if (use_sandbox)
552 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200553 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200554 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200555 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 return retval;
557}
558
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559/*
560 * Top level evaluation function, returning a number.
561 * Evaluates "expr" silently.
562 * Returns -1 for an error.
563 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200564 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100565eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566{
Bram Moolenaar33570922005-01-25 22:26:29 +0000567 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200568 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000569 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570
571 ++emsg_off;
572
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200573 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 retval = -1;
575 else
576 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100577 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000578 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 }
580 --emsg_off;
581
582 return retval;
583}
584
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000585/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000586 * Top level evaluation function.
587 * Returns an allocated typval_T with the result.
588 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000589 */
590 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200591eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000592{
593 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200594 evalarg_T evalarg;
595
596 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000597
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200598 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200599 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100600 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000601
Bram Moolenaar37c83712020-06-30 21:18:36 +0200602 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000603 return tv;
604}
605
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100607 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200608 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
609 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000610 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200612 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100613call_vim_function(
614 char_u *func,
615 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200616 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200617 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000619 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200620 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100622 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200623 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200624 funcexe.firstline = curwin->w_cursor.lnum;
625 funcexe.lastline = curwin->w_cursor.lnum;
626 funcexe.evaluate = TRUE;
627 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000628 if (ret == FAIL)
629 clear_tv(rettv);
630
631 return ret;
632}
633
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100634/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100635 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100636 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200637 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
638 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100639 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200640 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100641call_func_retnr(
642 char_u *func,
643 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200644 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100645{
646 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200647 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100648
Bram Moolenaarded27a12018-08-01 19:06:03 +0200649 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100650 return -1;
651
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100652 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100653 clear_tv(&rettv);
654 return retval;
655}
656
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000657/*
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100658 * Call Vim script function like call_func_retnr() and drop the result.
659 * Returns FAIL when calling the function fails.
660 */
661 int
662call_func_noret(
663 char_u *func,
664 int argc,
665 typval_T *argv)
666{
667 typval_T rettv;
668
669 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
670 return FAIL;
671 clear_tv(&rettv);
672 return OK;
673}
674
675/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100676 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100677 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000678 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000679 */
680 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100681call_func_retstr(
682 char_u *func,
683 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200684 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000685{
686 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000687 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000688
Bram Moolenaarded27a12018-08-01 19:06:03 +0200689 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000690 return NULL;
691
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100692 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000693 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 return retval;
695}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000696
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000697/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100698 * Call Vim script function "func" and return the result as a List.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100699 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000700 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000701 */
702 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100703call_func_retlist(
704 char_u *func,
705 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200706 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000707{
708 typval_T rettv;
709
Bram Moolenaarded27a12018-08-01 19:06:03 +0200710 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000711 return NULL;
712
713 if (rettv.v_type != VAR_LIST)
714 {
715 clear_tv(&rettv);
716 return NULL;
717 }
718
719 return rettv.vval.v_list;
720}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722#ifdef FEAT_FOLDING
723/*
724 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
725 * it in "*cp". Doesn't give error messages.
726 */
727 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100728eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729{
Bram Moolenaar33570922005-01-25 22:26:29 +0000730 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200731 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000733 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
734 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735
736 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000737 if (use_sandbox)
738 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200739 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200741 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 retval = 0;
743 else
744 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100745 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000746 if (tv.v_type == VAR_NUMBER)
747 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000748 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 retval = 0;
750 else
751 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100752 // If the result is a string, check if there is a non-digit before
753 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000754 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 if (!VIM_ISDIGIT(*s) && *s != '-')
756 *cp = *s++;
757 retval = atol((char *)s);
758 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000759 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 }
761 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000762 if (use_sandbox)
763 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200764 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200765 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200767 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768}
769#endif
770
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000772 * Get an lval: variable, Dict item or List item that can be assigned a value
773 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
774 * "name.key", "name.key[expr]" etc.
775 * Indexing only works if "name" is an existing List or Dictionary.
776 * "name" points to the start of the name.
777 * If "rettv" is not NULL it points to the value to be assigned.
778 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
779 * wrong; must end in space or cmd separator.
780 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100781 * flags:
782 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100783 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100784 * GLV_NO_AUTOLOAD: do not use script autoloading
785 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000786 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000787 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000788 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000789 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200790 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100791get_lval(
792 char_u *name,
793 typval_T *rettv,
794 lval_T *lp,
795 int unlet,
796 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100797 int flags, // GLV_ values
798 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000799{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000800 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000801 char_u *expr_start, *expr_end;
802 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000803 dictitem_T *v;
804 typval_T var1;
805 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000806 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000807 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000808 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000809 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100810 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100811 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000812
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100813 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200814 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000815
816 if (skip)
817 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100818 // When skipping just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000819 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200820 lp->ll_name_end = find_name_end(name, NULL, NULL,
821 FNE_INCL_BR | fne_flags);
822 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000823 }
824
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100825 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000826 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100827 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000828 if (expr_start != NULL)
829 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100830 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100831 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000832 && *p != '[' && *p != '.')
833 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200834 semsg(_(e_trailing_arg), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000835 return NULL;
836 }
837
838 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
839 if (lp->ll_exp_name == NULL)
840 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100841 // Report an invalid expression in braces, unless the
842 // expression evaluation has been cancelled due to an
843 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000844 if (!aborting() && !quiet)
845 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000846 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100847 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000848 return NULL;
849 }
850 }
851 lp->ll_name = lp->ll_exp_name;
852 }
853 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100854 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000855 lp->ll_name = name;
856
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200857 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100858 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200859 // "a: type" is declaring variable "a" with a type, not "a:".
860 if (p == name + 2 && p[-1] == ':')
861 {
862 --p;
863 lp->ll_name_end = p;
864 }
865 if (*p == ':')
866 {
867 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
868 char_u *tp = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100869
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200870 // parse the type after the name
871 lp->ll_type = parse_type(&tp, &si->sn_type_list);
872 lp->ll_name_end = tp;
873 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100874 }
875 }
876
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100877 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000878 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
879 return p;
880
881 cc = *p;
882 *p = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100883 // Only pass &ht when we would write to the variable, it prevents autoload
884 // as well.
Bram Moolenaar6e65d592017-12-07 22:11:27 +0100885 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
886 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000887 if (v == NULL && !quiet)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200888 semsg(_(e_undefined_variable_str), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000889 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000890 if (v == NULL)
891 return NULL;
892
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +0100893 if (in_vim9script() && (flags & GLV_NO_DECL) == 0)
894 {
895 if (!quiet)
896 semsg(_(e_variable_already_declared), lp->ll_name);
897 return NULL;
898 }
899
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000900 /*
901 * Loop until no more [idx] or .key is following.
902 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000903 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100904 var1.v_type = VAR_UNKNOWN;
905 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000906 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000907 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000908 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
Bram Moolenaar81ed4962020-09-23 15:56:50 +0200909 && !(lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100910 && !(lp->ll_tv->v_type == VAR_BLOB
911 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000912 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000913 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100914 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000915 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000916 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000917 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000918 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000919 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100920 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000921 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000922 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000923
Bram Moolenaar10c65862020-10-08 21:16:42 +0200924 if (in_vim9script() && lp->ll_valtype == NULL
925 && lp->ll_tv == &v->di_tv
926 && ht != NULL && ht == get_script_local_ht())
927 {
928 svar_T *sv = find_typval_in_script(lp->ll_tv);
929
930 // Vim9 script local variable: get the type
931 if (sv != NULL)
932 lp->ll_valtype = sv->sv_type;
933 }
934
Bram Moolenaar8c711452005-01-14 21:53:12 +0000935 len = -1;
936 if (*p == '.')
937 {
938 key = p + 1;
939 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
940 ;
941 if (len == 0)
942 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000943 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100944 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000945 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000946 }
947 p = key + len;
948 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000949 else
950 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100951 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000952 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000953 if (*p == ':')
954 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000955 else
956 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000957 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200958 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000959 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100960 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000961 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100962 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000963 clear_tv(&var1);
964 return NULL;
965 }
Bram Moolenaar6a250262020-08-04 15:53:01 +0200966 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000967 }
968
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100969 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000970 if (*p == ':')
971 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000972 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000973 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000974 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +0200975 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100976 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000977 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000978 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100979 if (rettv != NULL
980 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100981 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100982 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100983 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +0000984 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000985 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100986 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100987 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000988 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000989 }
990 p = skipwhite(p + 1);
991 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000992 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000993 else
994 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000995 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200996 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200997 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000998 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100999 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001000 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001001 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001002 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001003 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001004 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001005 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001006 clear_tv(&var2);
1007 return NULL;
1008 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001009 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001010 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001011 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001012 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001013 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001014
Bram Moolenaar8c711452005-01-14 21:53:12 +00001015 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001016 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001017 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001018 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001019 clear_tv(&var1);
1020 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001021 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001022 }
1023
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001024 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001025 ++p;
1026 }
1027
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001028 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001029 {
1030 if (len == -1)
1031 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001032 // "[key]": get key from "var1"
1033 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001034 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001035 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001036 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001037 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001038 }
1039 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001040 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001041
1042 // a NULL dict is equivalent with an empty dict
1043 if (lp->ll_tv->vval.v_dict == NULL)
1044 {
1045 lp->ll_tv->vval.v_dict = dict_alloc();
1046 if (lp->ll_tv->vval.v_dict == NULL)
1047 {
1048 clear_tv(&var1);
1049 return NULL;
1050 }
1051 ++lp->ll_tv->vval.v_dict->dv_refcount;
1052 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001053 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001054
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001055 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001056
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001057 // When assigning to a scope dictionary check that a function and
1058 // variable name is valid (only variable name unless it is l: or
1059 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001060 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001061 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001062 int prevval;
1063 int wrong;
1064
1065 if (len != -1)
1066 {
1067 prevval = key[len];
1068 key[len] = NUL;
1069 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001070 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001071 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001072 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1073 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001074 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar03290b82020-12-19 16:30:44 +01001075 || !valid_varname(key, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001076 if (len != -1)
1077 key[len] = prevval;
1078 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001079 {
1080 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001081 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001082 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001083 }
1084
Bram Moolenaar10c65862020-10-08 21:16:42 +02001085 if (lp->ll_valtype != NULL)
1086 // use the type of the member
1087 lp->ll_valtype = lp->ll_valtype->tt_member;
1088
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001089 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001090 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001091 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001092 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001093 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001094 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001095 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001096 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001097 return NULL;
1098 }
1099
Bram Moolenaar31b81602019-02-10 22:14:27 +01001100 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001101 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001102 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001103 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001104 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001105 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001106 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001107 }
1108 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001109 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001110 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001111 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001112 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001113 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001114 p = NULL;
1115 break;
1116 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001117 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001118 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001119 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1120 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001121 {
1122 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001123 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001124 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001125
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001126 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001127 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001128 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001129 else if (lp->ll_tv->v_type == VAR_BLOB)
1130 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001131 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1132
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001133 /*
1134 * Get the number and item for the only or first index of the List.
1135 */
1136 if (empty1)
1137 lp->ll_n1 = 0;
1138 else
1139 // is number or string
1140 lp->ll_n1 = (long)tv_get_number(&var1);
1141 clear_tv(&var1);
1142
1143 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001144 || lp->ll_n1 > bloblen
1145 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001146 {
1147 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001148 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001149 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001150 return NULL;
1151 }
1152 if (lp->ll_range && !lp->ll_empty2)
1153 {
1154 lp->ll_n2 = (long)tv_get_number(&var2);
1155 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001156 if (lp->ll_n2 < 0
1157 || lp->ll_n2 >= bloblen
1158 || lp->ll_n2 < lp->ll_n1)
1159 {
1160 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001161 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001162 return NULL;
1163 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001164 }
1165 lp->ll_blob = lp->ll_tv->vval.v_blob;
1166 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001167 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001168 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001169 else
1170 {
1171 /*
1172 * Get the number and item for the only or first index of the List.
1173 */
1174 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001175 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001176 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001177 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001178 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001179 clear_tv(&var1);
1180
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001181 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001182 lp->ll_list = lp->ll_tv->vval.v_list;
1183 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1184 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001185 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001186 if (lp->ll_n1 < 0)
1187 {
1188 lp->ll_n1 = 0;
1189 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1190 }
1191 }
1192 if (lp->ll_li == NULL)
1193 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001194 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001195 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001196 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001197 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001198 }
1199
Bram Moolenaar10c65862020-10-08 21:16:42 +02001200 if (lp->ll_valtype != NULL)
1201 // use the type of the member
1202 lp->ll_valtype = lp->ll_valtype->tt_member;
1203
Bram Moolenaar8c711452005-01-14 21:53:12 +00001204 /*
1205 * May need to find the item or absolute index for the second
1206 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001207 * When no index given: "lp->ll_empty2" is TRUE.
1208 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001209 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001210 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001211 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001212 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001213 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001214 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001215 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001216 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001217 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001218 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001219 {
1220 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001221 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001222 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001223 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001224 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001225 }
1226
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001227 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001228 if (lp->ll_n1 < 0)
1229 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1230 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001231 {
1232 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001233 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001234 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001235 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001236 }
1237
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001238 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001239 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001240 }
1241
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001242 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001243 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001244 return p;
1245}
1246
1247/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001248 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001249 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001250 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001251clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001252{
1253 vim_free(lp->ll_exp_name);
1254 vim_free(lp->ll_newkey);
1255}
1256
1257/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001258 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001259 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001260 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1261 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001262 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001263 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001264set_var_lval(
1265 lval_T *lp,
1266 char_u *endp,
1267 typval_T *rettv,
1268 int copy,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001269 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar7454a062016-01-30 15:14:10 +01001270 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001271{
1272 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001273 listitem_T *ri;
1274 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001275
1276 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001277 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001278 cc = *endp;
1279 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001280 if (lp->ll_blob != NULL)
1281 {
1282 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001283
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001284 if (op != NULL && *op != '=')
1285 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001286 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001287 return;
1288 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001289 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001290 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001291
1292 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1293 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001294 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001295
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001296 if (lp->ll_empty2)
1297 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1298
1299 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001300 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001301 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001302 return;
1303 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001304 if (lp->ll_empty2)
1305 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001306
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001307 ir = 0;
1308 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1309 blob_set(lp->ll_blob, il,
1310 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001311 }
1312 else
1313 {
1314 val = (int)tv_get_number_chk(rettv, &error);
1315 if (!error)
1316 {
1317 garray_T *gap = &lp->ll_blob->bv_ga;
1318
1319 // Allow for appending a byte. Setting a byte beyond
1320 // the end is an error otherwise.
1321 if (lp->ll_n1 < gap->ga_len
1322 || (lp->ll_n1 == gap->ga_len
1323 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1324 {
1325 blob_set(lp->ll_blob, lp->ll_n1, val);
1326 if (lp->ll_n1 == gap->ga_len)
1327 ++gap->ga_len;
1328 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001329 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001330 }
1331 }
1332 }
1333 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001334 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001335 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001336
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001337 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar9937a052019-06-15 15:45:06 +02001338 {
1339 emsg(_(e_cannot_mod));
1340 *endp = cc;
1341 return;
1342 }
1343
Bram Moolenaarff697e62019-02-12 22:28:33 +01001344 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001345 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001346 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar79518e22017-02-17 16:31:35 +01001347 &tv, &di, TRUE, FALSE) == OK)
1348 {
1349 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001350 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1351 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001352 && tv_op(&tv, rettv, op) == OK)
1353 set_var(lp->ll_name, &tv, FALSE);
1354 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001355 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001356 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001357 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001358 {
1359 if (lp->ll_type != NULL
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001360 && check_typval_type(lp->ll_type, rettv, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001361 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001362 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001363 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001364 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001365 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001366 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001367 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001368 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001369 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001370 else if (lp->ll_range)
1371 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001372 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001373 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001374
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001375 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar9937a052019-06-15 15:45:06 +02001376 {
1377 emsg(_("E996: Cannot lock a range"));
1378 return;
1379 }
1380
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001381 /*
1382 * Check whether any of the list items is locked
1383 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001384 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001385 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001386 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001387 return;
1388 ri = ri->li_next;
1389 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1390 break;
1391 ll_li = ll_li->li_next;
1392 ++ll_n1;
1393 }
1394
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001395 /*
1396 * Assign the List values to the list items.
1397 */
1398 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001399 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001400 if (op != NULL && *op != '=')
1401 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1402 else
1403 {
1404 clear_tv(&lp->ll_li->li_tv);
1405 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1406 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001407 ri = ri->li_next;
1408 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1409 break;
1410 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001411 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001412 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001413 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001414 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001415 ri = NULL;
1416 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001417 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001418 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001419 lp->ll_li = lp->ll_li->li_next;
1420 ++lp->ll_n1;
1421 }
1422 if (ri != NULL)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001423 emsg(_(e_list_value_has_more_items_than_targets));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001424 else if (lp->ll_empty2
1425 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001426 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001427 emsg(_(e_list_value_does_not_have_enough_items));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001428 }
1429 else
1430 {
1431 /*
1432 * Assign to a List or Dictionary item.
1433 */
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001434 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar9937a052019-06-15 15:45:06 +02001435 {
1436 emsg(_("E996: Cannot lock a list or dict"));
1437 return;
1438 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001439
1440 if (lp->ll_valtype != NULL
1441 && check_typval_type(lp->ll_valtype, rettv, 0) == FAIL)
1442 return;
1443
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001444 if (lp->ll_newkey != NULL)
1445 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001446 if (op != NULL && *op != '=')
1447 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001448 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001449 return;
1450 }
1451
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001452 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001453 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001454 if (di == NULL)
1455 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001456 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1457 {
1458 vim_free(di);
1459 return;
1460 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001461 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001462 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001463 else if (op != NULL && *op != '=')
1464 {
1465 tv_op(lp->ll_tv, rettv, op);
1466 return;
1467 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001468 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001469 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001470
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001471 /*
1472 * Assign the value to the variable or list item.
1473 */
1474 if (copy)
1475 copy_tv(rettv, lp->ll_tv);
1476 else
1477 {
1478 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001479 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001480 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001481 }
1482 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001483}
1484
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001485/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001486 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1487 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001488 * Returns OK or FAIL.
1489 */
1490 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001491tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001492{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001493 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001494 char_u numbuf[NUMBUFLEN];
1495 char_u *s;
1496
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001497 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001498 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001499 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001500 {
1501 switch (tv1->v_type)
1502 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001503 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001504 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001505 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001506 case VAR_DICT:
1507 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001508 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001509 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001510 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001511 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001512 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001513 break;
1514
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001515 case VAR_BLOB:
1516 if (*op != '+' || tv2->v_type != VAR_BLOB)
1517 break;
1518 // BLOB += BLOB
1519 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1520 {
1521 blob_T *b1 = tv1->vval.v_blob;
1522 blob_T *b2 = tv2->vval.v_blob;
1523 int i, len = blob_len(b2);
1524 for (i = 0; i < len; i++)
1525 ga_append(&b1->bv_ga, blob_get(b2, i));
1526 }
1527 return OK;
1528
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001529 case VAR_LIST:
1530 if (*op != '+' || tv2->v_type != VAR_LIST)
1531 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001532 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001533 if (tv2->vval.v_list != NULL)
1534 {
1535 if (tv1->vval.v_list == NULL)
1536 {
1537 tv1->vval.v_list = tv2->vval.v_list;
1538 ++tv1->vval.v_list->lv_refcount;
1539 }
1540 else
1541 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1542 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001543 return OK;
1544
1545 case VAR_NUMBER:
1546 case VAR_STRING:
1547 if (tv2->v_type == VAR_LIST)
1548 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001549 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001550 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001551 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001552 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001553#ifdef FEAT_FLOAT
1554 if (tv2->v_type == VAR_FLOAT)
1555 {
1556 float_T f = n;
1557
Bram Moolenaarff697e62019-02-12 22:28:33 +01001558 if (*op == '%')
1559 break;
1560 switch (*op)
1561 {
1562 case '+': f += tv2->vval.v_float; break;
1563 case '-': f -= tv2->vval.v_float; break;
1564 case '*': f *= tv2->vval.v_float; break;
1565 case '/': f /= tv2->vval.v_float; break;
1566 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001567 clear_tv(tv1);
1568 tv1->v_type = VAR_FLOAT;
1569 tv1->vval.v_float = f;
1570 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001571 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001572#endif
1573 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001574 switch (*op)
1575 {
1576 case '+': n += tv_get_number(tv2); break;
1577 case '-': n -= tv_get_number(tv2); break;
1578 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001579 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1580 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001581 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001582 clear_tv(tv1);
1583 tv1->v_type = VAR_NUMBER;
1584 tv1->vval.v_number = n;
1585 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001586 }
1587 else
1588 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001589 if (tv2->v_type == VAR_FLOAT)
1590 break;
1591
Bram Moolenaarff697e62019-02-12 22:28:33 +01001592 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001593 s = tv_get_string(tv1);
1594 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001595 clear_tv(tv1);
1596 tv1->v_type = VAR_STRING;
1597 tv1->vval.v_string = s;
1598 }
1599 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001600
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001601 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001602#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001603 {
1604 float_T f;
1605
Bram Moolenaarff697e62019-02-12 22:28:33 +01001606 if (*op == '%' || *op == '.'
1607 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001608 && tv2->v_type != VAR_NUMBER
1609 && tv2->v_type != VAR_STRING))
1610 break;
1611 if (tv2->v_type == VAR_FLOAT)
1612 f = tv2->vval.v_float;
1613 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001614 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001615 switch (*op)
1616 {
1617 case '+': tv1->vval.v_float += f; break;
1618 case '-': tv1->vval.v_float -= f; break;
1619 case '*': tv1->vval.v_float *= f; break;
1620 case '/': tv1->vval.v_float /= f; break;
1621 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001622 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001623#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001624 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001625 }
1626 }
1627
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001628 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001629 return FAIL;
1630}
1631
1632/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001633 * Evaluate the expression used in a ":for var in expr" command.
1634 * "arg" points to "var".
1635 * Set "*errp" to TRUE for an error, FALSE otherwise;
1636 * Return a pointer that holds the info. Null when there is an error.
1637 */
1638 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001639eval_for_line(
1640 char_u *arg,
1641 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001642 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001643 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001644{
Bram Moolenaar33570922005-01-25 22:26:29 +00001645 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001646 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001647 typval_T tv;
1648 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001649 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001650
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001651 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001652
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001653 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001654 if (fi == NULL)
1655 return NULL;
1656
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001657 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001658 if (expr == NULL)
1659 return fi;
1660
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001661 expr = skipwhite_and_linebreak(expr, evalarg);
1662 if (expr[0] != 'i' || expr[1] != 'n'
1663 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001664 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001665 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001666 return fi;
1667 }
1668
1669 if (skip)
1670 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001671 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1672 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001673 {
1674 *errp = FALSE;
1675 if (!skip)
1676 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001677 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001678 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001679 l = tv.vval.v_list;
1680 if (l == NULL)
1681 {
1682 // a null list is like an empty list: do nothing
1683 clear_tv(&tv);
1684 }
1685 else
1686 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001687 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001688 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001689
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001690 // No need to increment the refcount, it's already set for
1691 // the list being used in "tv".
1692 fi->fi_list = l;
1693 list_add_watch(l, &fi->fi_lw);
1694 fi->fi_lw.lw_item = l->lv_first;
1695 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001696 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001697 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001698 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001699 fi->fi_bi = 0;
1700 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001701 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001702 typval_T btv;
1703
1704 // Make a copy, so that the iteration still works when the
1705 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001706 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001707 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001708 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001709 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001710 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001711 else
1712 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001713 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001714 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001715 }
1716 }
1717 }
1718 if (skip)
1719 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001720 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001721
1722 return fi;
1723}
1724
1725/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001726 * Used when looping over a :for line, skip the "in expr" part.
1727 */
1728 void
1729skip_for_lines(void *fi_void, evalarg_T *evalarg)
1730{
1731 forinfo_T *fi = (forinfo_T *)fi_void;
1732 int i;
1733
1734 for (i = 0; i < fi->fi_break_count; ++i)
1735 eval_next_line(evalarg);
1736}
1737
1738/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001739 * Use the first item in a ":for" list. Advance to the next.
1740 * Assign the values to the variable (list). "arg" points to the first one.
1741 * Return TRUE when a valid item was found, FALSE when at end of list or
1742 * something wrong.
1743 */
1744 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001745next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001746{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001747 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001748 int result;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001749 int flag = in_vim9script() ? ASSIGN_NO_DECL : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001750 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001751
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001752 if (fi->fi_blob != NULL)
1753 {
1754 typval_T tv;
1755
1756 if (fi->fi_bi >= blob_len(fi->fi_blob))
1757 return FALSE;
1758 tv.v_type = VAR_NUMBER;
1759 tv.v_lock = VAR_FIXED;
1760 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1761 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001762 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001763 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001764 }
1765
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001766 item = fi->fi_lw.lw_item;
1767 if (item == NULL)
1768 result = FALSE;
1769 else
1770 {
1771 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001772 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001773 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001774 }
1775 return result;
1776}
1777
1778/*
1779 * Free the structure used to store info used by ":for".
1780 */
1781 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001782free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001783{
Bram Moolenaar33570922005-01-25 22:26:29 +00001784 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001785
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001786 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001787 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001788 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001789 list_unref(fi->fi_list);
1790 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001791 if (fi != NULL && fi->fi_blob != NULL)
1792 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001793 vim_free(fi);
1794}
1795
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001797set_context_for_expression(
1798 expand_T *xp,
1799 char_u *arg,
1800 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001802 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001804 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001806 if (cmdidx == CMD_let || cmdidx == CMD_var
1807 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001808 {
1809 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001810 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001811 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001812 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001813 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001814 {
1815 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001816 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001817 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001818 break;
1819 }
1820 return;
1821 }
1822 }
1823 else
1824 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1825 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 while ((xp->xp_pattern = vim_strpbrk(arg,
1827 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1828 {
1829 c = *xp->xp_pattern;
1830 if (c == '&')
1831 {
1832 c = xp->xp_pattern[1];
1833 if (c == '&')
1834 {
1835 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001836 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 }
1838 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001839 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001841 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1842 xp->xp_pattern += 2;
1843
1844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 }
1846 else if (c == '$')
1847 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001848 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 xp->xp_context = EXPAND_ENV_VARS;
1850 }
1851 else if (c == '=')
1852 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001853 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 xp->xp_context = EXPAND_EXPRESSION;
1855 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001856 else if (c == '#'
1857 && xp->xp_context == EXPAND_EXPRESSION)
1858 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001859 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001860 break;
1861 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001862 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 && xp->xp_context == EXPAND_FUNCTIONS
1864 && vim_strchr(xp->xp_pattern, '(') == NULL)
1865 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001866 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 break;
1868 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001869 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001871 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 {
1873 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1874 if (c == '\\' && xp->xp_pattern[1] != NUL)
1875 ++xp->xp_pattern;
1876 xp->xp_context = EXPAND_NOTHING;
1877 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001878 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001880 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1882 /* skip */ ;
1883 xp->xp_context = EXPAND_NOTHING;
1884 }
1885 else if (c == '|')
1886 {
1887 if (xp->xp_pattern[1] == '|')
1888 {
1889 ++xp->xp_pattern;
1890 xp->xp_context = EXPAND_EXPRESSION;
1891 }
1892 else
1893 xp->xp_context = EXPAND_COMMANDS;
1894 }
1895 else
1896 xp->xp_context = EXPAND_EXPRESSION;
1897 }
1898 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001899 // Doesn't look like something valid, expand as an expression
1900 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001901 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 arg = xp->xp_pattern;
1903 if (*arg != NUL)
1904 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1905 /* skip */ ;
1906 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01001907
1908 // ":exe one two" completes "two"
1909 if ((cmdidx == CMD_execute
1910 || cmdidx == CMD_echo
1911 || cmdidx == CMD_echon
1912 || cmdidx == CMD_echomsg)
1913 && xp->xp_context == EXPAND_EXPRESSION)
1914 {
1915 for (;;)
1916 {
1917 char_u *n = skiptowhite(arg);
1918
1919 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
1920 break;
1921 arg = skipwhite(n);
1922 }
1923 }
1924
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 xp->xp_pattern = arg;
1926}
1927
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001929 * Return TRUE if "pat" matches "text".
1930 * Does not use 'cpo' and always uses 'magic'.
1931 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001932 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001933pattern_match(char_u *pat, char_u *text, int ic)
1934{
1935 int matches = FALSE;
1936 char_u *save_cpo;
1937 regmatch_T regmatch;
1938
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001939 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001940 save_cpo = p_cpo;
1941 p_cpo = (char_u *)"";
1942 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1943 if (regmatch.regprog != NULL)
1944 {
1945 regmatch.rm_ic = ic;
1946 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1947 vim_regfree(regmatch.regprog);
1948 }
1949 p_cpo = save_cpo;
1950 return matches;
1951}
1952
1953/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001954 * Handle a name followed by "(". Both for just "name(arg)" and for
1955 * "expr->name(arg)".
1956 * Returns OK or FAIL.
1957 */
1958 static int
1959eval_func(
1960 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02001961 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001962 char_u *name,
1963 int name_len,
1964 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001965 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001966 typval_T *basetv) // "expr" for "expr->name(arg)"
1967{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001968 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001969 char_u *s = name;
1970 int len = name_len;
1971 partial_T *partial;
1972 int ret = OK;
1973
1974 if (!evaluate)
1975 check_vars(s, len);
1976
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001977 // If "s" is the name of a variable of type VAR_FUNC
1978 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001979 s = deref_func_name(s, &len, &partial, !evaluate);
1980
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001981 // Need to make a copy, in case evaluating the arguments makes
1982 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001983 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001984 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001985 ret = FAIL;
1986 else
1987 {
1988 funcexe_T funcexe;
1989
1990 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001991 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001992 funcexe.firstline = curwin->w_cursor.lnum;
1993 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001994 funcexe.evaluate = evaluate;
1995 funcexe.partial = partial;
1996 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02001997 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001998 }
1999 vim_free(s);
2000
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002001 // If evaluate is FALSE rettv->v_type was not set in
2002 // get_func_tv, but it's needed in handle_subscript() to parse
2003 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002004 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2005 {
2006 rettv->vval.v_string = NULL;
2007 rettv->v_type = VAR_FUNC;
2008 }
2009
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002010 // Stop the expression evaluation when immediately
2011 // aborting on error, or when an interrupt occurred or
2012 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002013 if (evaluate && aborting())
2014 {
2015 if (ret == OK)
2016 clear_tv(rettv);
2017 ret = FAIL;
2018 }
2019 return ret;
2020}
2021
2022/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002023 * Get the next line source line without advancing. But do skip over comment
2024 * lines.
2025 */
2026 static char_u *
2027getline_peek_skip_comments(evalarg_T *evalarg)
2028{
2029 for (;;)
2030 {
2031 char_u *next = getline_peek(evalarg->eval_getline,
2032 evalarg->eval_cookie);
2033 char_u *p;
2034
2035 if (next == NULL)
2036 break;
2037 p = skipwhite(next);
2038 if (*p != NUL && !vim9_comment_start(p))
2039 return next;
2040 (void)eval_next_line(evalarg);
2041 }
2042 return NULL;
2043}
2044
2045/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002046 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2047 * comment) and there is a next line, return the next line (skipping blanks)
2048 * and set "getnext".
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002049 * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
2050 * "arg" must point somewhere inside a line, not at the start.
2051 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002052 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002053eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2054{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002055 char_u *p = skipwhite(arg);
2056
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002057 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002058 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002059 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002060 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaar9d489562020-07-30 20:08:50 +02002061 && (*p == NUL || (VIM_ISWHITE(p[-1]) && vim9_comment_start(p))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002062 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002063 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002064
2065 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002066 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002067 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002068 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002069
Bram Moolenaar9d489562020-07-30 20:08:50 +02002070 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002071 {
2072 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002073 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002074 }
2075 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002076 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002077}
2078
2079/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002080 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002081 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002082 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002083eval_next_line(evalarg_T *evalarg)
2084{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002085 garray_T *gap = &evalarg->eval_ga;
2086 char_u *line;
2087
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002088 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002089 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2090 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002091 else
2092 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002093 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002094 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2095 {
2096 // Going to concatenate the lines after parsing.
2097 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2098 ++gap->ga_len;
2099 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002100 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002101 {
2102 vim_free(evalarg->eval_tofree);
2103 evalarg->eval_tofree = line;
2104 }
2105 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002106}
2107
Bram Moolenaare6b53242020-07-01 17:28:33 +02002108/*
2109 * Call eval_next_non_blank() and get the next line if needed.
2110 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002111 char_u *
2112skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2113{
2114 int getnext;
2115 char_u *p = skipwhite(arg);
2116
Bram Moolenaar962d7212020-07-04 14:15:00 +02002117 if (evalarg == NULL)
2118 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002119 eval_next_non_blank(p, evalarg, &getnext);
2120 if (getnext)
2121 return eval_next_line(evalarg);
2122 return p;
2123}
2124
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002125/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002126 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002127 */
2128 void
2129clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2130{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002131 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002132 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002133 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002134 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002135 if (eap != NULL)
2136 {
2137 // We may need to keep the original command line, e.g. for
2138 // ":let" it has the variable names. But we may also need the
2139 // new one, "nextcmd" points into it. Keep both.
2140 vim_free(eap->cmdline_tofree);
2141 eap->cmdline_tofree = *eap->cmdlinep;
2142 *eap->cmdlinep = evalarg->eval_tofree;
2143 }
2144 else
2145 vim_free(evalarg->eval_tofree);
2146 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002147 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002148
2149 vim_free(evalarg->eval_tofree_lambda);
2150 evalarg->eval_tofree_lambda = NULL;
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002151 }
2152}
2153
2154/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002156 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2158 */
2159
2160/*
2161 * Handle zero level expression.
2162 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002163 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002164 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002165 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 * Return OK or FAIL.
2167 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002168 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002169eval0(
2170 char_u *arg,
2171 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002172 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002173 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174{
2175 int ret;
2176 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002177 int did_emsg_before = did_emsg;
2178 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002179 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180
2181 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002182 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002183 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002184
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002185 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 {
2187 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002188 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 /*
2190 * Report the invalid expression unless the expression evaluation has
2191 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002192 * exception, or we already gave a more specific error.
2193 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002195 if (!aborting()
2196 && did_emsg == did_emsg_before
2197 && called_emsg == called_emsg_before
2198 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002199 semsg(_(e_invexpr2), arg);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002200
2201 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002202 // a next command to avoid more errors, unless "|" is following, which
2203 // could only be a command separator.
2204 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2205 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002206 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002208
2209 if (eap != NULL)
2210 eap->nextcmd = check_nextcmd(p);
2211
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 return ret;
2213}
2214
2215/*
2216 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002217 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002218 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 *
2220 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002221 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002223 * Note: "rettv.v_lock" is not set.
2224 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 * Return OK or FAIL.
2226 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002227 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002228eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002230 char_u *p;
2231 int getnext;
2232
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002233 CLEAR_POINTER(rettv);
2234
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 /*
2236 * Get the first variable.
2237 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002238 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 return FAIL;
2240
Bram Moolenaar793648f2020-06-26 21:28:25 +02002241 p = eval_next_non_blank(*arg, evalarg, &getnext);
2242 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002244 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002245 int result;
2246 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002247 evalarg_T *evalarg_used = evalarg;
2248 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002249 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002250 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002251 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002252
2253 if (evalarg == NULL)
2254 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002255 CLEAR_FIELD(local_evalarg);
2256 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002257 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002258 orig_flags = evalarg_used->eval_flags;
2259 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002260
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002261 if (getnext)
2262 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002263 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002264 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002265 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002266 {
2267 error_white_both(p, 1);
2268 clear_tv(rettv);
2269 return FAIL;
2270 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002271 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002272 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002273
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002275 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002277 int error = FALSE;
2278
Bram Moolenaar13106602020-10-04 16:06:05 +02002279 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002280 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002281 else if (vim9script)
2282 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002283 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002285 if (error || !op_falsy || !result)
2286 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002287 if (error)
2288 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 }
2290
2291 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002292 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002294 if (op_falsy)
2295 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002296 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002297 {
2298 error_white_both(p, 1);
2299 clear_tv(rettv);
2300 return FAIL;
2301 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002302 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002303 evalarg_used->eval_flags = (op_falsy ? !result : result)
2304 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2305 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002306 {
2307 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002309 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002310 if (!op_falsy || !result)
2311 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002313 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002315 /*
2316 * Check for the ":".
2317 */
2318 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2319 if (*p != ':')
2320 {
2321 emsg(_(e_missing_colon));
2322 if (evaluate && result)
2323 clear_tv(rettv);
2324 evalarg_used->eval_flags = orig_flags;
2325 return FAIL;
2326 }
2327 if (getnext)
2328 *arg = eval_next_line(evalarg_used);
2329 else
2330 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002331 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002332 {
2333 error_white_both(p, 1);
2334 clear_tv(rettv);
2335 evalarg_used->eval_flags = orig_flags;
2336 return FAIL;
2337 }
2338 *arg = p;
2339 }
2340
2341 /*
2342 * Get the third variable. Recursive!
2343 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002344 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002345 {
2346 error_white_both(p, 1);
2347 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002348 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002349 return FAIL;
2350 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002351 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2352 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002353 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002354 if (eval1(arg, &var2, evalarg_used) == FAIL)
2355 {
2356 if (evaluate && result)
2357 clear_tv(rettv);
2358 evalarg_used->eval_flags = orig_flags;
2359 return FAIL;
2360 }
2361 if (evaluate && !result)
2362 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002364
2365 if (evalarg == NULL)
2366 clear_evalarg(&local_evalarg, NULL);
2367 else
2368 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 }
2370
2371 return OK;
2372}
2373
2374/*
2375 * Handle first level expression:
2376 * expr2 || expr2 || expr2 logical OR
2377 *
2378 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002379 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 *
2381 * Return OK or FAIL.
2382 */
2383 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002384eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002386 char_u *p;
2387 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388
2389 /*
2390 * Get the first variable.
2391 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002392 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 return FAIL;
2394
2395 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002396 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002398 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002399 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002401 evalarg_T *evalarg_used = evalarg;
2402 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002403 int evaluate;
2404 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002405 long result = FALSE;
2406 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002407 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002408 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002409
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002410 if (evalarg == NULL)
2411 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002412 CLEAR_FIELD(local_evalarg);
2413 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002414 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002415 orig_flags = evalarg_used->eval_flags;
2416 evaluate = orig_flags & EVAL_EVALUATE;
2417 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002418 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002419 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002420 result = tv_get_bool_chk(rettv, &error);
2421 else if (tv_get_number_chk(rettv, &error) != 0)
2422 result = TRUE;
2423 clear_tv(rettv);
2424 if (error)
2425 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 }
2427
2428 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002429 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002431 while (p[0] == '|' && p[1] == '|')
2432 {
2433 if (getnext)
2434 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002435 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002436 {
2437 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2438 {
2439 error_white_both(p, 2);
2440 clear_tv(rettv);
2441 return FAIL;
2442 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002443 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002444 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002445
2446 /*
2447 * Get the second variable.
2448 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002449 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2450 {
2451 error_white_both(p, 2);
2452 clear_tv(rettv);
2453 return FAIL;
2454 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002455 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2456 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002457 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002458 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002459 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002460
2461 /*
2462 * Compute the result.
2463 */
2464 if (evaluate && !result)
2465 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002466 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002467 result = tv_get_bool_chk(&var2, &error);
2468 else if (tv_get_number_chk(&var2, &error) != 0)
2469 result = TRUE;
2470 clear_tv(&var2);
2471 if (error)
2472 return FAIL;
2473 }
2474 if (evaluate)
2475 {
2476 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002477 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002478 rettv->v_type = VAR_BOOL;
2479 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002480 }
2481 else
2482 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002483 rettv->v_type = VAR_NUMBER;
2484 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002485 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002486 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002487
2488 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002490
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002491 if (evalarg == NULL)
2492 clear_evalarg(&local_evalarg, NULL);
2493 else
2494 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 }
2496
2497 return OK;
2498}
2499
2500/*
2501 * Handle second level expression:
2502 * expr3 && expr3 && expr3 logical AND
2503 *
2504 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002505 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 *
2507 * Return OK or FAIL.
2508 */
2509 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002510eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002512 char_u *p;
2513 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514
2515 /*
2516 * Get the first variable.
2517 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002518 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 return FAIL;
2520
2521 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002522 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002524 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002525 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002527 evalarg_T *evalarg_used = evalarg;
2528 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002529 int orig_flags;
2530 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002531 long result = TRUE;
2532 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002533 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002534 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002535
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002536 if (evalarg == NULL)
2537 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002538 CLEAR_FIELD(local_evalarg);
2539 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002540 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002541 orig_flags = evalarg_used->eval_flags;
2542 evaluate = orig_flags & EVAL_EVALUATE;
2543 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002544 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002545 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002546 result = tv_get_bool_chk(rettv, &error);
2547 else if (tv_get_number_chk(rettv, &error) == 0)
2548 result = FALSE;
2549 clear_tv(rettv);
2550 if (error)
2551 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 }
2553
2554 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002555 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002557 while (p[0] == '&' && p[1] == '&')
2558 {
2559 if (getnext)
2560 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002561 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002562 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002563 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002564 {
2565 error_white_both(p, 2);
2566 clear_tv(rettv);
2567 return FAIL;
2568 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002569 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002570 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002571
2572 /*
2573 * Get the second variable.
2574 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002575 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2576 {
2577 error_white_both(p, 2);
2578 clear_tv(rettv);
2579 return FAIL;
2580 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002581 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2582 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002583 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002584 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002585 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002586 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002587
2588 /*
2589 * Compute the result.
2590 */
2591 if (evaluate && result)
2592 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002593 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002594 result = tv_get_bool_chk(&var2, &error);
2595 else if (tv_get_number_chk(&var2, &error) == 0)
2596 result = FALSE;
2597 clear_tv(&var2);
2598 if (error)
2599 return FAIL;
2600 }
2601 if (evaluate)
2602 {
2603 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002604 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002605 rettv->v_type = VAR_BOOL;
2606 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002607 }
2608 else
2609 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002610 rettv->v_type = VAR_NUMBER;
2611 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002612 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002613 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002614
2615 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002617
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002618 if (evalarg == NULL)
2619 clear_evalarg(&local_evalarg, NULL);
2620 else
2621 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 }
2623
2624 return OK;
2625}
2626
2627/*
2628 * Handle third level expression:
2629 * var1 == var2
2630 * var1 =~ var2
2631 * var1 != var2
2632 * var1 !~ var2
2633 * var1 > var2
2634 * var1 >= var2
2635 * var1 < var2
2636 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002637 * var1 is var2
2638 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 *
2640 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002641 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 *
2643 * Return OK or FAIL.
2644 */
2645 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002646eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002649 int getnext;
Bram Moolenaar87396072019-12-31 22:36:18 +01002650 exptype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002652 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653
2654 /*
2655 * Get the first variable.
2656 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002657 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 return FAIL;
2659
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002660 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002661 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662
2663 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002664 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002666 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002668 typval_T var2;
2669 int ic;
2670 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002671 int evaluate = evalarg == NULL
2672 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002673
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002674 if (getnext)
2675 *arg = eval_next_line(evalarg);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002676 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2677 {
2678 error_white_both(p, len);
2679 clear_tv(rettv);
2680 return FAIL;
2681 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002682
Bram Moolenaar696ba232020-07-29 21:20:41 +02002683 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2684 {
2685 semsg(_(e_invexpr2), p);
2686 clear_tv(rettv);
2687 return FAIL;
2688 }
2689
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002690 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 if (p[len] == '?')
2692 {
2693 ic = TRUE;
2694 ++len;
2695 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002696 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 else if (p[len] == '#')
2698 {
2699 ic = FALSE;
2700 ++len;
2701 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002702 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002704 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705
2706 /*
2707 * Get the second variable.
2708 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002709 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2710 {
2711 error_white_both(p, 1);
2712 clear_tv(rettv);
2713 return FAIL;
2714 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002715 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002716 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002718 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 return FAIL;
2720 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002721 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002722 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002723 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002724
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002725 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002726 {
2727 ret = FAIL;
2728 clear_tv(rettv);
2729 }
2730 else
2731 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002732 clear_tv(&var2);
2733 return ret;
2734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 }
2736
2737 return OK;
2738}
2739
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002740/*
2741 * Make a copy of blob "tv1" and append blob "tv2".
2742 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002743 void
2744eval_addblob(typval_T *tv1, typval_T *tv2)
2745{
2746 blob_T *b1 = tv1->vval.v_blob;
2747 blob_T *b2 = tv2->vval.v_blob;
2748 blob_T *b = blob_alloc();
2749 int i;
2750
2751 if (b != NULL)
2752 {
2753 for (i = 0; i < blob_len(b1); i++)
2754 ga_append(&b->bv_ga, blob_get(b1, i));
2755 for (i = 0; i < blob_len(b2); i++)
2756 ga_append(&b->bv_ga, blob_get(b2, i));
2757
2758 clear_tv(tv1);
2759 rettv_blob_set(tv1, b);
2760 }
2761}
2762
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002763/*
2764 * Make a copy of list "tv1" and append list "tv2".
2765 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002766 int
2767eval_addlist(typval_T *tv1, typval_T *tv2)
2768{
2769 typval_T var3;
2770
2771 // concatenate Lists
2772 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2773 {
2774 clear_tv(tv1);
2775 clear_tv(tv2);
2776 return FAIL;
2777 }
2778 clear_tv(tv1);
2779 *tv1 = var3;
2780 return OK;
2781}
2782
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783/*
2784 * Handle fourth level expression:
2785 * + number addition
2786 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002787 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002788 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 *
2790 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002791 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 *
2793 * Return OK or FAIL.
2794 */
2795 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002796eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 /*
2799 * Get the first variable.
2800 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002801 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 return FAIL;
2803
2804 /*
2805 * Repeat computing, until no '+', '-' or '.' is following.
2806 */
2807 for (;;)
2808 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002809 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002810 int getnext;
2811 char_u *p;
2812 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002813 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002814 int concat;
2815 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002816 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002817
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002818 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarce2c5442020-11-28 21:21:17 +01002819 // "+=" and "-=" are assignment
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002820 p = eval_next_non_blank(*arg, evalarg, &getnext);
2821 op = *p;
2822 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaarce2c5442020-11-28 21:21:17 +01002823 if ((op != '+' && op != '-' && !concat) || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002825
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002826 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002827 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002828 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002829 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002830 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002831 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002832 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002833 {
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002834 error_white_both(p, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002835 clear_tv(rettv);
2836 return FAIL;
2837 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002838 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002839 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002840 if ((op != '+' || (rettv->v_type != VAR_LIST
2841 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002842#ifdef FEAT_FLOAT
2843 && (op == '.' || rettv->v_type != VAR_FLOAT)
2844#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002845 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002846 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002847 int error = FALSE;
2848
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002849 // For "list + ...", an illegal use of the first operand as
2850 // a number cannot be determined before evaluating the 2nd
2851 // operand: if this is also a list, all is ok.
2852 // For "something . ...", "something - ..." or "non-list + ...",
2853 // we know that the first operand needs to be a string or number
2854 // without evaluating the 2nd operand. So check before to avoid
2855 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002856 if (op != '.')
2857 tv_get_number_chk(rettv, &error);
2858 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002859 {
2860 clear_tv(rettv);
2861 return FAIL;
2862 }
2863 }
2864
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 /*
2866 * Get the second variable.
2867 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002868 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002869 {
2870 error_white_both(p, oplen);
2871 clear_tv(rettv);
2872 return FAIL;
2873 }
2874 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002875 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002877 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 return FAIL;
2879 }
2880
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002881 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 {
2883 /*
2884 * Compute the result.
2885 */
2886 if (op == '.')
2887 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002888 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2889 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002890 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002891
Bram Moolenaar2e086612020-08-25 22:37:48 +02002892 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002893 || var2.v_type == VAR_CHANNEL
2894 || var2.v_type == VAR_JOB))
2895 emsg(_(e_inval_string));
2896#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02002897 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002898 {
2899 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2900 var2.vval.v_float);
2901 s2 = buf2;
2902 }
2903#endif
2904 else
2905 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002906 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002907 {
2908 clear_tv(rettv);
2909 clear_tv(&var2);
2910 return FAIL;
2911 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002912 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002913 clear_tv(rettv);
2914 rettv->v_type = VAR_STRING;
2915 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002917 else if (op == '+' && rettv->v_type == VAR_BLOB
2918 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002919 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002920 else if (op == '+' && rettv->v_type == VAR_LIST
2921 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002922 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002923 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002924 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 else
2927 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002928 int error = FALSE;
2929 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002930#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002931 float_T f1 = 0, f2 = 0;
2932
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002933 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002934 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002935 f1 = rettv->vval.v_float;
2936 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002937 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002938 else
2939#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002940 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002941 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002942 if (error)
2943 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002944 // This can only happen for "list + non-list". For
2945 // "non-list + ..." or "something - ...", we returned
2946 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002947 clear_tv(rettv);
2948 return FAIL;
2949 }
2950#ifdef FEAT_FLOAT
2951 if (var2.v_type == VAR_FLOAT)
2952 f1 = n1;
2953#endif
2954 }
2955#ifdef FEAT_FLOAT
2956 if (var2.v_type == VAR_FLOAT)
2957 {
2958 f2 = var2.vval.v_float;
2959 n2 = 0;
2960 }
2961 else
2962#endif
2963 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002964 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002965 if (error)
2966 {
2967 clear_tv(rettv);
2968 clear_tv(&var2);
2969 return FAIL;
2970 }
2971#ifdef FEAT_FLOAT
2972 if (rettv->v_type == VAR_FLOAT)
2973 f2 = n2;
2974#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002975 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002976 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002977
2978#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002979 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002980 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2981 {
2982 if (op == '+')
2983 f1 = f1 + f2;
2984 else
2985 f1 = f1 - f2;
2986 rettv->v_type = VAR_FLOAT;
2987 rettv->vval.v_float = f1;
2988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002990#endif
2991 {
2992 if (op == '+')
2993 n1 = n1 + n2;
2994 else
2995 n1 = n1 - n2;
2996 rettv->v_type = VAR_NUMBER;
2997 rettv->vval.v_number = n1;
2998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003000 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 }
3002 }
3003 return OK;
3004}
3005
3006/*
3007 * Handle fifth level expression:
3008 * * number multiplication
3009 * / number division
3010 * % number modulo
3011 *
3012 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003013 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 *
3015 * Return OK or FAIL.
3016 */
3017 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003018eval6(
3019 char_u **arg,
3020 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003021 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003022 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003024#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003025 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003026#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027
3028 /*
3029 * Get the first variable.
3030 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003031 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 return FAIL;
3033
3034 /*
3035 * Repeat computing, until no '*', '/' or '%' is following.
3036 */
3037 for (;;)
3038 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003039 int evaluate;
3040 int getnext;
3041 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003042 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003043 int op;
3044 varnumber_T n1, n2;
3045#ifdef FEAT_FLOAT
3046 float_T f1, f2;
3047#endif
3048 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003049
Bram Moolenaar9d489562020-07-30 20:08:50 +02003050 p = eval_next_non_blank(*arg, evalarg, &getnext);
3051 op = *p;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02003052 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003054
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003055 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003056 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003057 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003058 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003059 {
3060 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3061 {
3062 error_white_both(p, 1);
3063 clear_tv(rettv);
3064 return FAIL;
3065 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003066 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003069#ifdef FEAT_FLOAT
3070 f1 = 0;
3071 f2 = 0;
3072#endif
3073 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003074 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003076#ifdef FEAT_FLOAT
3077 if (rettv->v_type == VAR_FLOAT)
3078 {
3079 f1 = rettv->vval.v_float;
3080 use_float = TRUE;
3081 n1 = 0;
3082 }
3083 else
3084#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003085 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003086 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003087 if (error)
3088 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 }
3090 else
3091 n1 = 0;
3092
3093 /*
3094 * Get the second variable.
3095 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003096 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3097 {
3098 error_white_both(p, 1);
3099 clear_tv(rettv);
3100 return FAIL;
3101 }
3102 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003103 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 return FAIL;
3105
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003106 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003108#ifdef FEAT_FLOAT
3109 if (var2.v_type == VAR_FLOAT)
3110 {
3111 if (!use_float)
3112 {
3113 f1 = n1;
3114 use_float = TRUE;
3115 }
3116 f2 = var2.vval.v_float;
3117 n2 = 0;
3118 }
3119 else
3120#endif
3121 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003122 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003123 clear_tv(&var2);
3124 if (error)
3125 return FAIL;
3126#ifdef FEAT_FLOAT
3127 if (use_float)
3128 f2 = n2;
3129#endif
3130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131
3132 /*
3133 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003134 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003136#ifdef FEAT_FLOAT
3137 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003139 if (op == '*')
3140 f1 = f1 * f2;
3141 else if (op == '/')
3142 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003143# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003144 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003145 if (f2 == 0.0)
3146 {
3147 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003148 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003149 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003150 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003151 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003152 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003153 }
3154 else
3155 f1 = f1 / f2;
3156# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003157 // We rely on the floating point library to handle divide
3158 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003159 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003160# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003163 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003164 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003165 return FAIL;
3166 }
3167 rettv->v_type = VAR_FLOAT;
3168 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 }
3170 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003171#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003173 if (op == '*')
3174 n1 = n1 * n2;
3175 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01003176 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01003178 n1 = num_modulus(n1, n2);
3179
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003180 rettv->v_type = VAR_NUMBER;
3181 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 }
3184 }
3185
3186 return OK;
3187}
3188
3189/*
3190 * Handle sixth level expression:
3191 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003192 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003193 * "string" string constant
3194 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 * &option-name option value
3196 * @r register contents
3197 * identifier variable value
3198 * function() function call
3199 * $VAR environment variable
3200 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003201 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003202 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003203 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003204 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 *
3206 * Also handle:
3207 * ! in front logical NOT
3208 * - in front unary minus
3209 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003210 * trailing [] subscript in String or List
3211 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003212 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 *
3214 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003215 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 *
3217 * Return OK or FAIL.
3218 */
3219 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003220eval7(
3221 char_u **arg,
3222 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003223 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003224 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003226 int evaluate = evalarg != NULL
3227 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 int len;
3229 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 char_u *start_leader, *end_leader;
3231 int ret = OK;
3232 char_u *alias;
3233
3234 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003235 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003236 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003238 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239
3240 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003241 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 */
3243 start_leader = *arg;
3244 while (**arg == '!' || **arg == '-' || **arg == '+')
3245 *arg = skipwhite(*arg + 1);
3246 end_leader = *arg;
3247
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003248 if (**arg == '.' && (!isdigit(*(*arg + 1))
3249#ifdef FEAT_FLOAT
3250 || current_sctx.sc_version < 2
3251#endif
3252 ))
3253 {
3254 semsg(_(e_invexpr2), *arg);
3255 ++*arg;
3256 return FAIL;
3257 }
3258
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 switch (**arg)
3260 {
3261 /*
3262 * Number constant.
3263 */
3264 case '0':
3265 case '1':
3266 case '2':
3267 case '3':
3268 case '4':
3269 case '5':
3270 case '6':
3271 case '7':
3272 case '8':
3273 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003274 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003275
3276 // Apply prefixed "-" and "+" now. Matters especially when
3277 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003278 if (ret == OK && evaluate && end_leader > start_leader
3279 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003280 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 break;
3282
3283 /*
3284 * String constant: "string".
3285 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003286 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 break;
3288
3289 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003290 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003292 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003293 break;
3294
3295 /*
3296 * List: [expr, expr]
3297 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003298 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 break;
3300
3301 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003302 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003303 */
Bram Moolenaare0de1712020-12-02 17:36:54 +01003304 case '#': if (!in_vim9script() && (*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003305 {
3306 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003307 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003308 }
3309 else
3310 ret = NOTDONE;
3311 break;
3312
3313 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003314 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003315 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003316 */
Bram Moolenaarc2ca9352020-11-25 21:30:11 +01003317 case '{': ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003318 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003319 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003320 break;
3321
3322 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003323 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003325 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 break;
3327
3328 /*
3329 * Environment variable: $VAR.
3330 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003331 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 break;
3333
3334 /*
3335 * Register contents: @r.
3336 */
3337 case '@': ++*arg;
3338 if (evaluate)
3339 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003340 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02003341 rettv->vval.v_string = get_reg_contents(**arg,
3342 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 }
3344 if (**arg != NUL)
3345 ++*arg;
3346 break;
3347
3348 /*
3349 * nested expression: (expression).
3350 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003351 case '(': {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003352 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003353 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003354
Bram Moolenaar9215f012020-06-27 21:18:00 +02003355 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003356 if (**arg == ')')
3357 ++*arg;
3358 else if (ret == OK)
3359 {
3360 emsg(_(e_missing_close));
3361 clear_tv(rettv);
3362 ret = FAIL;
3363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 }
3365 break;
3366
Bram Moolenaar8c711452005-01-14 21:53:12 +00003367 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 break;
3369 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003370
3371 if (ret == NOTDONE)
3372 {
3373 /*
3374 * Must be a variable or function name.
3375 * Can also be a curly-braces kind of name: {expr}.
3376 */
3377 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003378 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003379 if (alias != NULL)
3380 s = alias;
3381
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003382 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003383 ret = FAIL;
3384 else
3385 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003386 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3387
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003388 if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
3389 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003390 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003391 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003392 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003393 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003394 else if (flags & EVAL_CONSTANT)
3395 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003396 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003397 {
3398 // get the value of "true", "false" or a variable
3399 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3400 {
3401 rettv->v_type = VAR_BOOL;
3402 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003403 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003404 }
3405 else if (len == 5 && in_vim9script()
3406 && STRNCMP(s, "false", 4) == 0)
3407 {
3408 rettv->v_type = VAR_BOOL;
3409 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003410 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003411 }
3412 else
3413 ret = eval_variable(s, len, rettv, NULL, TRUE, FALSE);
3414 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003415 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003416 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003417 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003418 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003419 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003420 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003421 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003422 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003423 }
3424
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003425 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3426 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003427 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003428 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429
3430 /*
3431 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3432 */
3433 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003434 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003435 return ret;
3436}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003437
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003438/*
3439 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003440 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003441 * Adjusts "end_leaderp" until it is at "start_leader".
3442 */
3443 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003444eval7_leader(
3445 typval_T *rettv,
3446 int numeric_only,
3447 char_u *start_leader,
3448 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003449{
3450 char_u *end_leader = *end_leaderp;
3451 int ret = OK;
3452 int error = FALSE;
3453 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003454 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003455#ifdef FEAT_FLOAT
3456 float_T f = 0.0;
3457
3458 if (rettv->v_type == VAR_FLOAT)
3459 f = rettv->vval.v_float;
3460 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003461#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003462 {
3463 while (VIM_ISWHITE(end_leader[-1]))
3464 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003465 if (in_vim9script() && end_leader[-1] == '!')
3466 val = tv2bool(rettv);
3467 else
3468 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003469 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003470 if (error)
3471 {
3472 clear_tv(rettv);
3473 ret = FAIL;
3474 }
3475 else
3476 {
3477 while (end_leader > start_leader)
3478 {
3479 --end_leader;
3480 if (*end_leader == '!')
3481 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003482 if (numeric_only)
3483 {
3484 ++end_leader;
3485 break;
3486 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003487#ifdef FEAT_FLOAT
3488 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003489 {
3490 if (in_vim9script())
3491 {
3492 rettv->v_type = VAR_BOOL;
3493 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3494 }
3495 else
3496 f = !f;
3497 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003498 else
3499#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003500 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003501 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003502 type = VAR_BOOL;
3503 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003504 }
3505 else if (*end_leader == '-')
3506 {
3507#ifdef FEAT_FLOAT
3508 if (rettv->v_type == VAR_FLOAT)
3509 f = -f;
3510 else
3511#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003512 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003513 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003514 type = VAR_NUMBER;
3515 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003516 }
3517 }
3518#ifdef FEAT_FLOAT
3519 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003521 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003522 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003524 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003525#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003526 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003527 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003528 if (in_vim9script())
3529 rettv->v_type = type;
3530 else
3531 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003532 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003535 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 return ret;
3537}
3538
3539/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003540 * Call the function referred to in "rettv".
3541 */
3542 static int
3543call_func_rettv(
3544 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003545 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003546 typval_T *rettv,
3547 int evaluate,
3548 dict_T *selfdict,
3549 typval_T *basetv)
3550{
3551 partial_T *pt = NULL;
3552 funcexe_T funcexe;
3553 typval_T functv;
3554 char_u *s;
3555 int ret;
3556
3557 // need to copy the funcref so that we can clear rettv
3558 if (evaluate)
3559 {
3560 functv = *rettv;
3561 rettv->v_type = VAR_UNKNOWN;
3562
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003563 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003564 if (functv.v_type == VAR_PARTIAL)
3565 {
3566 pt = functv.vval.v_partial;
3567 s = partial_name(pt);
3568 }
3569 else
3570 s = functv.vval.v_string;
3571 }
3572 else
3573 s = (char_u *)"";
3574
Bram Moolenaara80faa82020-04-12 19:37:17 +02003575 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003576 funcexe.firstline = curwin->w_cursor.lnum;
3577 funcexe.lastline = curwin->w_cursor.lnum;
3578 funcexe.evaluate = evaluate;
3579 funcexe.partial = pt;
3580 funcexe.selfdict = selfdict;
3581 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003582 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003583
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003584 // Clear the funcref afterwards, so that deleting it while
3585 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003586 if (evaluate)
3587 clear_tv(&functv);
3588
3589 return ret;
3590}
3591
3592/*
3593 * Evaluate "->method()".
3594 * "*arg" points to the '-'.
3595 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3596 */
3597 static int
3598eval_lambda(
3599 char_u **arg,
3600 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003601 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003602 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003603{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003604 int evaluate = evalarg != NULL
3605 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003606 typval_T base = *rettv;
3607 int ret;
3608
3609 // Skip over the ->.
3610 *arg += 2;
3611 rettv->v_type = VAR_UNKNOWN;
3612
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003613 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003614 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003615 return FAIL;
3616 else if (**arg != '(')
3617 {
3618 if (verbose)
3619 {
3620 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003621 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003622 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003623 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003624 }
3625 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003626 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003627 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003628 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003629 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003630
3631 // Clear the funcref afterwards, so that deleting it while
3632 // evaluating the arguments is possible (see test55).
3633 if (evaluate)
3634 clear_tv(&base);
3635
3636 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003637}
3638
3639/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003640 * Evaluate "->method()".
3641 * "*arg" points to the '-'.
3642 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3643 */
3644 static int
3645eval_method(
3646 char_u **arg,
3647 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003648 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003649 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003650{
3651 char_u *name;
3652 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003653 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003654 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003655 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003656 int evaluate = evalarg != NULL
3657 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003658
3659 // Skip over the ->.
3660 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003661 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003662
Bram Moolenaarac92e252019-08-03 21:58:38 +02003663 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003664 len = get_name_len(arg, &alias, evaluate, TRUE);
3665 if (alias != NULL)
3666 name = alias;
3667
3668 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003669 {
3670 if (verbose)
3671 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003672 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003673 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003674 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003675 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003676 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003677 if (**arg != '(')
3678 {
3679 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003680 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003681 ret = FAIL;
3682 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003683 else if (VIM_ISWHITE((*arg)[-1]))
3684 {
3685 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003686 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003687 ret = FAIL;
3688 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003689 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003690 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003691 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003692 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003693
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003694 // Clear the funcref afterwards, so that deleting it while
3695 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003696 if (evaluate)
3697 clear_tv(&base);
3698
Bram Moolenaarac92e252019-08-03 21:58:38 +02003699 return ret;
3700}
3701
3702/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003703 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3704 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003705 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3706 */
3707 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003708eval_index(
3709 char_u **arg,
3710 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003711 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003712 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003713{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003714 int evaluate = evalarg != NULL
3715 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003716 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003717 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003718 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003719 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003720 int keylen = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003721
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003722 if (check_can_index(rettv, evaluate, verbose) == FAIL)
3723 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003724
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003725 init_tv(&var1);
3726 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003727 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003728 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003729 /*
3730 * dict.name
3731 */
3732 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003733 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003734 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003735 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003736 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02003737 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003738 }
3739 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003740 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003741 /*
3742 * something[idx]
3743 *
3744 * Get the (first) variable from inside the [].
3745 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003746 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003747 if (**arg == ':')
3748 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003749 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003750 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003751 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003752 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003753 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003754 clear_tv(&var1);
3755 return FAIL;
3756 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003757
3758 /*
3759 * Get the second variable from inside the [:].
3760 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003761 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003762 if (**arg == ':')
3763 {
3764 range = TRUE;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003765 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003766 if (**arg == ']')
3767 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003768 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003769 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003770 if (!empty1)
3771 clear_tv(&var1);
3772 return FAIL;
3773 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003774 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003775 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003776 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003777 if (!empty1)
3778 clear_tv(&var1);
3779 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003780 return FAIL;
3781 }
3782 }
3783
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003784 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003785 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003786 if (**arg != ']')
3787 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003788 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003789 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003790 clear_tv(&var1);
3791 if (range)
3792 clear_tv(&var2);
3793 return FAIL;
3794 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02003795 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003796 }
3797
3798 if (evaluate)
3799 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003800 int res = eval_index_inner(rettv, range,
3801 empty1 ? NULL : &var1, empty2 ? NULL : &var2,
3802 key, keylen, verbose);
3803 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003804 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003805 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003806 clear_tv(&var2);
3807 return res;
3808 }
3809 return OK;
3810}
3811
3812/*
3813 * Check if "rettv" can have an [index] or [sli:ce]
3814 */
3815 int
3816check_can_index(typval_T *rettv, int evaluate, int verbose)
3817{
3818 switch (rettv->v_type)
3819 {
3820 case VAR_FUNC:
3821 case VAR_PARTIAL:
3822 if (verbose)
3823 emsg(_("E695: Cannot index a Funcref"));
3824 return FAIL;
3825 case VAR_FLOAT:
3826#ifdef FEAT_FLOAT
3827 if (verbose)
3828 emsg(_(e_float_as_string));
3829 return FAIL;
3830#endif
3831 case VAR_BOOL:
3832 case VAR_SPECIAL:
3833 case VAR_JOB:
3834 case VAR_CHANNEL:
3835 if (verbose)
3836 emsg(_(e_cannot_index_special_variable));
3837 return FAIL;
3838 case VAR_UNKNOWN:
3839 case VAR_ANY:
3840 case VAR_VOID:
3841 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003842 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003843 emsg(_(e_cannot_index_special_variable));
3844 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003845 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003846 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003847
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003848 case VAR_STRING:
3849 case VAR_LIST:
3850 case VAR_DICT:
3851 case VAR_BLOB:
3852 break;
3853 case VAR_NUMBER:
3854 if (in_vim9script())
3855 emsg(_(e_cannot_index_number));
3856 break;
3857 }
3858 return OK;
3859}
3860
3861/*
3862 * Apply index or range to "rettv".
3863 * "var1" is the first index, NULL for [:expr].
3864 * "var2" is the second index, NULL for [expr] and [expr: ]
3865 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
3866 */
3867 int
3868eval_index_inner(
3869 typval_T *rettv,
3870 int is_range,
3871 typval_T *var1,
3872 typval_T *var2,
3873 char_u *key,
3874 int keylen,
3875 int verbose)
3876{
3877 long n1, n2 = 0;
3878 long len;
3879
3880 n1 = 0;
3881 if (var1 != NULL && rettv->v_type != VAR_DICT)
3882 n1 = tv_get_number(var1);
3883
3884 if (is_range)
3885 {
3886 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003887 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003888 if (verbose)
3889 emsg(_(e_cannot_slice_dictionary));
3890 return FAIL;
3891 }
3892 if (var2 == NULL)
3893 n2 = -1;
3894 else
3895 n2 = tv_get_number(var2);
3896 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01003897
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003898 switch (rettv->v_type)
3899 {
3900 case VAR_UNKNOWN:
3901 case VAR_ANY:
3902 case VAR_VOID:
3903 case VAR_FUNC:
3904 case VAR_PARTIAL:
3905 case VAR_FLOAT:
3906 case VAR_BOOL:
3907 case VAR_SPECIAL:
3908 case VAR_JOB:
3909 case VAR_CHANNEL:
3910 break; // not evaluating, skipping over subscript
3911
3912 case VAR_NUMBER:
3913 case VAR_STRING:
3914 {
3915 char_u *s = tv_get_string(rettv);
3916
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003917 len = (long)STRLEN(s);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003918 if (in_vim9script())
3919 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003920 if (is_range)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003921 s = string_slice(s, n1, n2);
3922 else
3923 s = char_from_string(s, n1);
3924 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003925 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003926 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003927 // The resulting variable is a substring. If the indexes
3928 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003929 if (n1 < 0)
3930 {
3931 n1 = len + n1;
3932 if (n1 < 0)
3933 n1 = 0;
3934 }
3935 if (n2 < 0)
3936 n2 = len + n2;
3937 else if (n2 >= len)
3938 n2 = len;
3939 if (n1 >= len || n2 < 0 || n1 > n2)
3940 s = NULL;
3941 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02003942 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003943 }
3944 else
3945 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003946 // The resulting variable is a string of a single
3947 // character. If the index is too big or negative the
3948 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003949 if (n1 >= len || n1 < 0)
3950 s = NULL;
3951 else
3952 s = vim_strnsave(s + n1, 1);
3953 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003954 clear_tv(rettv);
3955 rettv->v_type = VAR_STRING;
3956 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003957 }
3958 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003959
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003960 case VAR_BLOB:
3961 len = blob_len(rettv->vval.v_blob);
3962 if (is_range)
3963 {
3964 // The resulting variable is a sub-blob. If the indexes
3965 // are out of range the result is empty.
3966 if (n1 < 0)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003967 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003968 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003969 if (n1 < 0)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003970 n1 = 0;
3971 }
3972 if (n2 < 0)
3973 n2 = len + n2;
3974 else if (n2 >= len)
3975 n2 = len - 1;
3976 if (n1 >= len || n2 < 0 || n1 > n2)
3977 {
3978 clear_tv(rettv);
3979 rettv->v_type = VAR_BLOB;
3980 rettv->vval.v_blob = NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003981 }
3982 else
3983 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003984 blob_T *blob = blob_alloc();
3985 long i;
3986
3987 if (blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003988 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003989 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003990 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003991 blob_free(blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003992 return FAIL;
3993 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003994 blob->bv_ga.ga_len = n2 - n1 + 1;
3995 for (i = n1; i <= n2; i++)
3996 blob_set(blob, i - n1,
3997 blob_get(rettv->vval.v_blob, i));
3998
3999 clear_tv(rettv);
4000 rettv_blob_set(rettv, blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004001 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004002 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004003 }
4004 else
4005 {
4006 // The resulting variable is a byte value.
4007 // If the index is too big or negative that is an error.
4008 if (n1 < 0)
4009 n1 = len + n1;
4010 if (n1 < len && n1 >= 0)
4011 {
4012 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004013
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004014 clear_tv(rettv);
4015 rettv->v_type = VAR_NUMBER;
4016 rettv->vval.v_number = v;
4017 }
4018 else
4019 semsg(_(e_blobidx), n1);
4020 }
4021 break;
4022
4023 case VAR_LIST:
4024 if (var1 == NULL)
4025 n1 = 0;
4026 if (var2 == NULL)
4027 n2 = -1;
4028 if (list_slice_or_index(rettv->vval.v_list,
4029 is_range, n1, n2, rettv, verbose) == FAIL)
4030 return FAIL;
4031 break;
4032
4033 case VAR_DICT:
4034 {
4035 dictitem_T *item;
4036 typval_T tmp;
4037
4038 if (key == NULL)
4039 {
4040 key = tv_get_string_chk(var1);
4041 if (key == NULL)
4042 return FAIL;
4043 }
4044
4045 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
4046
4047 if (item == NULL && verbose)
4048 semsg(_(e_dictkey), key);
4049 if (item == NULL)
4050 return FAIL;
4051
4052 copy_tv(&item->di_tv, &tmp);
4053 clear_tv(rettv);
4054 *rettv = tmp;
4055 }
4056 break;
4057 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004058 return OK;
4059}
4060
4061/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004062 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004063 */
4064 char_u *
4065partial_name(partial_T *pt)
4066{
4067 if (pt->pt_name != NULL)
4068 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004069 if (pt->pt_func != NULL)
4070 return pt->pt_func->uf_name;
4071 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004072}
4073
Bram Moolenaarddecc252016-04-06 22:59:37 +02004074 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004075partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004076{
4077 int i;
4078
4079 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004080 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004081 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004082 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004083 if (pt->pt_name != NULL)
4084 {
4085 func_unref(pt->pt_name);
4086 vim_free(pt->pt_name);
4087 }
4088 else
4089 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004090
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004091 // Decrease the reference count for the context of a closure. If down
4092 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004093 if (pt->pt_funcstack != NULL)
4094 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004095 --pt->pt_funcstack->fs_refcount;
4096 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004097 }
4098
Bram Moolenaarddecc252016-04-06 22:59:37 +02004099 vim_free(pt);
4100}
4101
4102/*
4103 * Unreference a closure: decrement the reference count and free it when it
4104 * becomes zero.
4105 */
4106 void
4107partial_unref(partial_T *pt)
4108{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004109 if (pt != NULL)
4110 {
4111 if (--pt->pt_refcount <= 0)
4112 partial_free(pt);
4113
4114 // If the reference count goes down to one, the funcstack may be the
4115 // only reference and can be freed if no other partials reference it.
4116 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4117 funcstack_check_refcount(pt->pt_funcstack);
4118 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004119}
4120
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004121/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004122 * Return the next (unique) copy ID.
4123 * Used for serializing nested structures.
4124 */
4125 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004126get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004127{
4128 current_copyID += COPYID_INC;
4129 return current_copyID;
4130}
4131
4132/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004133 * Garbage collection for lists and dictionaries.
4134 *
4135 * We use reference counts to be able to free most items right away when they
4136 * are no longer used. But for composite items it's possible that it becomes
4137 * unused while the reference count is > 0: When there is a recursive
4138 * reference. Example:
4139 * :let l = [1, 2, 3]
4140 * :let d = {9: l}
4141 * :let l[1] = d
4142 *
4143 * Since this is quite unusual we handle this with garbage collection: every
4144 * once in a while find out which lists and dicts are not referenced from any
4145 * variable.
4146 *
4147 * Here is a good reference text about garbage collection (refers to Python
4148 * but it applies to all reference-counting mechanisms):
4149 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004150 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004151
4152/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004153 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004154 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004155 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004156 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004157 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004158garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004159{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004160 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004161 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004162 buf_T *buf;
4163 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004164 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004165 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004166
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004167 if (!testing)
4168 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004169 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004170 want_garbage_collect = FALSE;
4171 may_garbage_collect = FALSE;
4172 garbage_collect_at_exit = FALSE;
4173 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004174
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004175 // The execution stack can grow big, limit the size.
4176 if (exestack.ga_maxlen - exestack.ga_len > 500)
4177 {
4178 size_t new_len;
4179 char_u *pp;
4180 int n;
4181
4182 // Keep 150% of the current size, with a minimum of the growth size.
4183 n = exestack.ga_len / 2;
4184 if (n < exestack.ga_growsize)
4185 n = exestack.ga_growsize;
4186
4187 // Don't make it bigger though.
4188 if (exestack.ga_len + n < exestack.ga_maxlen)
4189 {
4190 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4191 pp = vim_realloc(exestack.ga_data, new_len);
4192 if (pp == NULL)
4193 return FAIL;
4194 exestack.ga_maxlen = exestack.ga_len + n;
4195 exestack.ga_data = pp;
4196 }
4197 }
4198
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004199 // We advance by two because we add one for items referenced through
4200 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004201 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004202
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004203 /*
4204 * 1. Go through all accessible variables and mark all lists and dicts
4205 * with copyID.
4206 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004207
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004208 // Don't free variables in the previous_funccal list unless they are only
4209 // referenced through previous_funccal. This must be first, because if
4210 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004211 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004212
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004213 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004214 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004215
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004216 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004217 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004218 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4219 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004220
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004221 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004222 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004223 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4224 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004225 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004226 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4227 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004228#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004229 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004230 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4231 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004232 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004233 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004234 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4235 NULL, NULL);
4236#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004237
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004238 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004239 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004240 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4241 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004242 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004243 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004244
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004245 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004246 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004247
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004248 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004249 abort = abort || set_ref_in_functions(copyID);
4250
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004251 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004252 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004253
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004254 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004255 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004256
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004257 // callbacks in buffers
4258 abort = abort || set_ref_in_buffers(copyID);
4259
Bram Moolenaar1dced572012-04-05 16:54:08 +02004260#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004261 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004262#endif
4263
Bram Moolenaardb913952012-06-29 12:54:53 +02004264#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004265 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004266#endif
4267
4268#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004269 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004270#endif
4271
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004272#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004273 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004274 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004275#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004276#ifdef FEAT_NETBEANS_INTG
4277 abort = abort || set_ref_in_nb_channel(copyID);
4278#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004279
Bram Moolenaare3188e22016-05-31 21:13:04 +02004280#ifdef FEAT_TIMERS
4281 abort = abort || set_ref_in_timer(copyID);
4282#endif
4283
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004284#ifdef FEAT_QUICKFIX
4285 abort = abort || set_ref_in_quickfix(copyID);
4286#endif
4287
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004288#ifdef FEAT_TERMINAL
4289 abort = abort || set_ref_in_term(copyID);
4290#endif
4291
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004292#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004293 abort = abort || set_ref_in_popups(copyID);
4294#endif
4295
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004296 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004297 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004298 /*
4299 * 2. Free lists and dictionaries that are not referenced.
4300 */
4301 did_free = free_unref_items(copyID);
4302
4303 /*
4304 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004305 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004306 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004307 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004308 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004309 else if (p_verbose > 0)
4310 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004311 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004312 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004313
4314 return did_free;
4315}
4316
4317/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004318 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004319 */
4320 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004321free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004322{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004323 int did_free = FALSE;
4324
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004325 // Let all "free" functions know that we are here. This means no
4326 // dictionaries, lists, channels or jobs are to be freed, because we will
4327 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004328 in_free_unref_items = TRUE;
4329
4330 /*
4331 * PASS 1: free the contents of the items. We don't free the items
4332 * themselves yet, so that it is possible to decrement refcount counters
4333 */
4334
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004335 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004336 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004337
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004338 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004339 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004340
4341#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004342 // Go through the list of jobs and free items without the copyID. This
4343 // must happen before doing channels, because jobs refer to channels, but
4344 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004345 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4346
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004347 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004348 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4349#endif
4350
4351 /*
4352 * PASS 2: free the items themselves.
4353 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004354 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004355 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004356
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004357#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004358 // Go through the list of jobs and free items without the copyID. This
4359 // must happen before doing channels, because jobs refer to channels, but
4360 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004361 free_unused_jobs(copyID, COPYID_MASK);
4362
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004363 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004364 free_unused_channels(copyID, COPYID_MASK);
4365#endif
4366
4367 in_free_unref_items = FALSE;
4368
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004369 return did_free;
4370}
4371
4372/*
4373 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004374 * "list_stack" is used to add lists to be marked. Can be NULL.
4375 *
4376 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004377 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004378 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004379set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004380{
4381 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004382 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004383 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004384 hashtab_T *cur_ht;
4385 ht_stack_T *ht_stack = NULL;
4386 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004387
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004388 cur_ht = ht;
4389 for (;;)
4390 {
4391 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004392 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004393 // Mark each item in the hashtab. If the item contains a hashtab
4394 // it is added to ht_stack, if it contains a list it is added to
4395 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004396 todo = (int)cur_ht->ht_used;
4397 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4398 if (!HASHITEM_EMPTY(hi))
4399 {
4400 --todo;
4401 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4402 &ht_stack, list_stack);
4403 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004404 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004405
4406 if (ht_stack == NULL)
4407 break;
4408
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004409 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004410 cur_ht = ht_stack->ht;
4411 tempitem = ht_stack;
4412 ht_stack = ht_stack->prev;
4413 free(tempitem);
4414 }
4415
4416 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004417}
4418
4419/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004420 * Mark a dict and its items with "copyID".
4421 * Returns TRUE if setting references failed somehow.
4422 */
4423 int
4424set_ref_in_dict(dict_T *d, int copyID)
4425{
4426 if (d != NULL && d->dv_copyID != copyID)
4427 {
4428 d->dv_copyID = copyID;
4429 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4430 }
4431 return FALSE;
4432}
4433
4434/*
4435 * Mark a list and its items with "copyID".
4436 * Returns TRUE if setting references failed somehow.
4437 */
4438 int
4439set_ref_in_list(list_T *ll, int copyID)
4440{
4441 if (ll != NULL && ll->lv_copyID != copyID)
4442 {
4443 ll->lv_copyID = copyID;
4444 return set_ref_in_list_items(ll, copyID, NULL);
4445 }
4446 return FALSE;
4447}
4448
4449/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004450 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004451 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4452 *
4453 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004454 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004455 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004456set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004457{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004458 listitem_T *li;
4459 int abort = FALSE;
4460 list_T *cur_l;
4461 list_stack_T *list_stack = NULL;
4462 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004463
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004464 cur_l = l;
4465 for (;;)
4466 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004467 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004468 // Mark each item in the list. If the item contains a hashtab
4469 // it is added to ht_stack, if it contains a list it is added to
4470 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004471 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4472 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4473 ht_stack, &list_stack);
4474 if (list_stack == NULL)
4475 break;
4476
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004477 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004478 cur_l = list_stack->list;
4479 tempitem = list_stack;
4480 list_stack = list_stack->prev;
4481 free(tempitem);
4482 }
4483
4484 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004485}
4486
4487/*
4488 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004489 * "list_stack" is used to add lists to be marked. Can be NULL.
4490 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4491 *
4492 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004493 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004494 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004495set_ref_in_item(
4496 typval_T *tv,
4497 int copyID,
4498 ht_stack_T **ht_stack,
4499 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004500{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004501 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004502
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004503 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004504 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004505 dict_T *dd = tv->vval.v_dict;
4506
Bram Moolenaara03f2332016-02-06 18:09:59 +01004507 if (dd != NULL && dd->dv_copyID != copyID)
4508 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004509 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004510 dd->dv_copyID = copyID;
4511 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004512 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004513 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4514 }
4515 else
4516 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004517 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4518
Bram Moolenaara03f2332016-02-06 18:09:59 +01004519 if (newitem == NULL)
4520 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004521 else
4522 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004523 newitem->ht = &dd->dv_hashtab;
4524 newitem->prev = *ht_stack;
4525 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004526 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004527 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004528 }
4529 }
4530 else if (tv->v_type == VAR_LIST)
4531 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004532 list_T *ll = tv->vval.v_list;
4533
Bram Moolenaara03f2332016-02-06 18:09:59 +01004534 if (ll != NULL && ll->lv_copyID != copyID)
4535 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004536 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004537 ll->lv_copyID = copyID;
4538 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004539 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004540 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004541 }
4542 else
4543 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004544 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4545
Bram Moolenaara03f2332016-02-06 18:09:59 +01004546 if (newitem == NULL)
4547 abort = TRUE;
4548 else
4549 {
4550 newitem->list = ll;
4551 newitem->prev = *list_stack;
4552 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004553 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004554 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004555 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004556 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004557 else if (tv->v_type == VAR_FUNC)
4558 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004559 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004560 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004561 else if (tv->v_type == VAR_PARTIAL)
4562 {
4563 partial_T *pt = tv->vval.v_partial;
4564 int i;
4565
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004566 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004567 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004568 // Didn't see this partial yet.
4569 pt->pt_copyID = copyID;
4570
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004571 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004572
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004573 if (pt->pt_dict != NULL)
4574 {
4575 typval_T dtv;
4576
4577 dtv.v_type = VAR_DICT;
4578 dtv.vval.v_dict = pt->pt_dict;
4579 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4580 }
4581
4582 for (i = 0; i < pt->pt_argc; ++i)
4583 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4584 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004585 if (pt->pt_funcstack != NULL)
4586 {
4587 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4588
4589 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4590 abort = abort || set_ref_in_item(stack + i, copyID,
4591 ht_stack, list_stack);
4592 }
4593
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004594 }
4595 }
4596#ifdef FEAT_JOB_CHANNEL
4597 else if (tv->v_type == VAR_JOB)
4598 {
4599 job_T *job = tv->vval.v_job;
4600 typval_T dtv;
4601
4602 if (job != NULL && job->jv_copyID != copyID)
4603 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004604 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004605 if (job->jv_channel != NULL)
4606 {
4607 dtv.v_type = VAR_CHANNEL;
4608 dtv.vval.v_channel = job->jv_channel;
4609 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4610 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004611 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004612 {
4613 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004614 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004615 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4616 }
4617 }
4618 }
4619 else if (tv->v_type == VAR_CHANNEL)
4620 {
4621 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004622 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004623 typval_T dtv;
4624 jsonq_T *jq;
4625 cbq_T *cq;
4626
4627 if (ch != NULL && ch->ch_copyID != copyID)
4628 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004629 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004630 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004631 {
4632 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4633 jq = jq->jq_next)
4634 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4635 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4636 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004637 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004638 {
4639 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004640 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004641 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4642 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004643 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004644 {
4645 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004646 dtv.vval.v_partial =
4647 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004648 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4649 }
4650 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004651 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004652 {
4653 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004654 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004655 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4656 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004657 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004658 {
4659 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004660 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004661 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4662 }
4663 }
4664 }
4665#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004666 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004667}
4668
Bram Moolenaar8c711452005-01-14 21:53:12 +00004669/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004670 * Return a string with the string representation of a variable.
4671 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004672 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004673 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004674 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004675 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02004676 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004677 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4678 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004679 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004680 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004681 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004682echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004683 typval_T *tv,
4684 char_u **tofree,
4685 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004686 int copyID,
4687 int echo_style,
4688 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004689 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004690{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004691 static int recurse = 0;
4692 char_u *r = NULL;
4693
Bram Moolenaar33570922005-01-25 22:26:29 +00004694 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004695 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004696 if (!did_echo_string_emsg)
4697 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004698 // Only give this message once for a recursive call to avoid
4699 // flooding the user with errors. And stop iterating over lists
4700 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004701 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004702 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004703 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004704 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004705 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004706 }
4707 ++recurse;
4708
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004709 switch (tv->v_type)
4710 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004711 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004712 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004713 {
4714 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004715 r = tv->vval.v_string;
4716 if (r == NULL)
4717 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004718 }
4719 else
4720 {
4721 *tofree = string_quote(tv->vval.v_string, FALSE);
4722 r = *tofree;
4723 }
4724 break;
4725
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004726 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004727 if (echo_style)
4728 {
4729 *tofree = NULL;
4730 r = tv->vval.v_string;
4731 }
4732 else
4733 {
4734 *tofree = string_quote(tv->vval.v_string, TRUE);
4735 r = *tofree;
4736 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004737 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004738
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004739 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004740 {
4741 partial_T *pt = tv->vval.v_partial;
4742 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004743 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004744 garray_T ga;
4745 int i;
4746 char_u *tf;
4747
4748 ga_init2(&ga, 1, 100);
4749 ga_concat(&ga, (char_u *)"function(");
4750 if (fname != NULL)
4751 {
4752 ga_concat(&ga, fname);
4753 vim_free(fname);
4754 }
4755 if (pt != NULL && pt->pt_argc > 0)
4756 {
4757 ga_concat(&ga, (char_u *)", [");
4758 for (i = 0; i < pt->pt_argc; ++i)
4759 {
4760 if (i > 0)
4761 ga_concat(&ga, (char_u *)", ");
4762 ga_concat(&ga,
4763 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4764 vim_free(tf);
4765 }
4766 ga_concat(&ga, (char_u *)"]");
4767 }
4768 if (pt != NULL && pt->pt_dict != NULL)
4769 {
4770 typval_T dtv;
4771
4772 ga_concat(&ga, (char_u *)", ");
4773 dtv.v_type = VAR_DICT;
4774 dtv.vval.v_dict = pt->pt_dict;
4775 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4776 vim_free(tf);
4777 }
4778 ga_concat(&ga, (char_u *)")");
4779
4780 *tofree = ga.ga_data;
4781 r = *tofree;
4782 break;
4783 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004784
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004785 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004786 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004787 break;
4788
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004789 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004790 if (tv->vval.v_list == NULL)
4791 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004792 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004793 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004794 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004795 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004796 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4797 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004798 {
4799 *tofree = NULL;
4800 r = (char_u *)"[...]";
4801 }
4802 else
4803 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004804 int old_copyID = tv->vval.v_list->lv_copyID;
4805
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004806 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004807 *tofree = list2string(tv, copyID, restore_copyID);
4808 if (restore_copyID)
4809 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004810 r = *tofree;
4811 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004812 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004813
Bram Moolenaar8c711452005-01-14 21:53:12 +00004814 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004815 if (tv->vval.v_dict == NULL)
4816 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004817 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004818 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004819 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004820 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004821 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4822 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004823 {
4824 *tofree = NULL;
4825 r = (char_u *)"{...}";
4826 }
4827 else
4828 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004829 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004830
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004831 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004832 *tofree = dict2string(tv, copyID, restore_copyID);
4833 if (restore_copyID)
4834 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004835 r = *tofree;
4836 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004837 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004838
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004839 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004840 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004841 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004842 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004843 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004844 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004845 break;
4846
Bram Moolenaar835dc632016-02-07 14:27:38 +01004847 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004848 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004849 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004850 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004851 if (composite_val)
4852 {
4853 *tofree = string_quote(r, FALSE);
4854 r = *tofree;
4855 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004856 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004857
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004858 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004859#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004860 *tofree = NULL;
4861 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4862 r = numbuf;
4863 break;
4864#endif
4865
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004866 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004867 case VAR_SPECIAL:
4868 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004869 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004870 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004871 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004872
Bram Moolenaar8502c702014-06-17 12:51:16 +02004873 if (--recurse == 0)
4874 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004875 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004876}
4877
4878/*
4879 * Return a string with the string representation of a variable.
4880 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4881 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004882 * Does not put quotes around strings, as ":echo" displays values.
4883 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4884 * May return NULL.
4885 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004886 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004887echo_string(
4888 typval_T *tv,
4889 char_u **tofree,
4890 char_u *numbuf,
4891 int copyID)
4892{
4893 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4894}
4895
4896/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004897 * Return string "str" in ' quotes, doubling ' characters.
4898 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004899 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004900 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004901 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004902string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004903{
Bram Moolenaar33570922005-01-25 22:26:29 +00004904 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004905 char_u *p, *r, *s;
4906
Bram Moolenaar33570922005-01-25 22:26:29 +00004907 len = (function ? 13 : 3);
4908 if (str != NULL)
4909 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004910 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004911 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004912 if (*p == '\'')
4913 ++len;
4914 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004915 s = r = alloc(len);
4916 if (r != NULL)
4917 {
4918 if (function)
4919 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004920 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004921 r += 10;
4922 }
4923 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004924 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004925 if (str != NULL)
4926 for (p = str; *p != NUL; )
4927 {
4928 if (*p == '\'')
4929 *r++ = '\'';
4930 MB_COPY_CHAR(p, r);
4931 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004932 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004933 if (function)
4934 *r++ = ')';
4935 *r++ = NUL;
4936 }
4937 return s;
4938}
4939
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004940#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004941/*
4942 * Convert the string "text" to a floating point number.
4943 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4944 * this always uses a decimal point.
4945 * Returns the length of the text that was consumed.
4946 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004947 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004948string2float(
4949 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004950 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004951{
4952 char *s = (char *)text;
4953 float_T f;
4954
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004955 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004956 if (STRNICMP(text, "inf", 3) == 0)
4957 {
4958 *value = INFINITY;
4959 return 3;
4960 }
4961 if (STRNICMP(text, "-inf", 3) == 0)
4962 {
4963 *value = -INFINITY;
4964 return 4;
4965 }
4966 if (STRNICMP(text, "nan", 3) == 0)
4967 {
4968 *value = NAN;
4969 return 3;
4970 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004971 f = strtod(s, &s);
4972 *value = f;
4973 return (int)((char_u *)s - text);
4974}
4975#endif
4976
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004977/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004979 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004981 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004982var2fpos(
4983 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004984 int dollar_lnum, // TRUE when $ is last line
4985 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004987 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004989 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004991 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004992 if (varp->v_type == VAR_LIST)
4993 {
4994 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004995 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004996 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004997 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004998
4999 l = varp->vval.v_list;
5000 if (l == NULL)
5001 return NULL;
5002
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005003 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005004 pos.lnum = list_find_nr(l, 0L, &error);
5005 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005006 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005007 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005008
Bram Moolenaarec65d772020-08-20 22:29:12 +02005009 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005010 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005011 li = list_find(l, 1L);
5012 if (li != NULL && li->li_tv.v_type == VAR_STRING
5013 && li->li_tv.vval.v_string != NULL
5014 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005015 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005016 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005017 }
5018 else
5019 {
5020 pos.col = list_find_nr(l, 1L, &error);
5021 if (error)
5022 return NULL;
5023 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005024
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005025 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005026 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005027 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005028 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005029
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005030 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005031 pos.coladd = list_find_nr(l, 2L, &error);
5032 if (error)
5033 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005034
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005035 return &pos;
5036 }
5037
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005038 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005039 if (name == NULL)
5040 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005041 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005043 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005044 {
5045 if (VIsual_active)
5046 return &VIsual;
5047 return &curwin->w_cursor;
5048 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005049 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005051 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5053 return NULL;
5054 return pp;
5055 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005056
Bram Moolenaara5525202006-03-02 22:52:09 +00005057 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005058
Bram Moolenaar477933c2007-07-17 14:32:23 +00005059 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005060 {
5061 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005062 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005063 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005064 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005065 // In silent Ex mode topline is zero, but that's not a valid line
5066 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005067 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005068 return &pos;
5069 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005070 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005071 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005072 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005073 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005074 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005075 return &pos;
5076 }
5077 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005078 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005080 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 {
5082 pos.lnum = curbuf->b_ml.ml_line_count;
5083 pos.col = 0;
5084 }
5085 else
5086 {
5087 pos.lnum = curwin->w_cursor.lnum;
5088 pos.col = (colnr_T)STRLEN(ml_get_curline());
5089 }
5090 return &pos;
5091 }
5092 return NULL;
5093}
5094
5095/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005096 * Convert list in "arg" into a position and optional file number.
5097 * When "fnump" is NULL there is no file number, only 3 items.
5098 * Note that the column is passed on as-is, the caller may want to decrement
5099 * it to use 1 for the first column.
5100 * Return FAIL when conversion is not possible, doesn't check the position for
5101 * validity.
5102 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005103 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005104list2fpos(
5105 typval_T *arg,
5106 pos_T *posp,
5107 int *fnump,
5108 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005109{
5110 list_T *l = arg->vval.v_list;
5111 long i = 0;
5112 long n;
5113
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005114 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5115 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005116 if (arg->v_type != VAR_LIST
5117 || l == NULL
5118 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005119 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005120 return FAIL;
5121
5122 if (fnump != NULL)
5123 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005124 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005125 if (n < 0)
5126 return FAIL;
5127 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005128 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005129 *fnump = n;
5130 }
5131
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005132 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005133 if (n < 0)
5134 return FAIL;
5135 posp->lnum = n;
5136
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005137 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005138 if (n < 0)
5139 return FAIL;
5140 posp->col = n;
5141
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005142 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005143 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005144 posp->coladd = 0;
5145 else
5146 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005147
Bram Moolenaar493c1782014-05-28 14:34:46 +02005148 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005149 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005150
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005151 return OK;
5152}
5153
5154/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 * Get the length of an environment variable name.
5156 * Advance "arg" to the first character after the name.
5157 * Return 0 for error.
5158 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005159 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005160get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161{
5162 char_u *p;
5163 int len;
5164
5165 for (p = *arg; vim_isIDc(*p); ++p)
5166 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005167 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 return 0;
5169
5170 len = (int)(p - *arg);
5171 *arg = p;
5172 return len;
5173}
5174
5175/*
5176 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005177 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 * Return 0 if something is wrong.
5179 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005180 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005181get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182{
5183 char_u *p;
5184 int len;
5185
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005186 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005188 {
5189 if (*p == ':')
5190 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005191 // "s:" is start of "s:var", but "n:" is not and can be used in
5192 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005193 len = (int)(p - *arg);
5194 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5195 || len > 1)
5196 break;
5197 }
5198 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005199 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 return 0;
5201
5202 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005203 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204
5205 return len;
5206}
5207
5208/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005209 * Get the length of the name of a variable or function.
5210 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005212 * Return -1 if curly braces expansion failed.
5213 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 * If the name contains 'magic' {}'s, expand them and return the
5215 * expanded name in an allocated string via 'alias' - caller must free.
5216 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005217 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005218get_name_len(
5219 char_u **arg,
5220 char_u **alias,
5221 int evaluate,
5222 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223{
5224 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 char_u *p;
5226 char_u *expr_start;
5227 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005229 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230
5231 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5232 && (*arg)[2] == (int)KE_SNR)
5233 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005234 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 *arg += 3;
5236 return get_id_len(arg) + 3;
5237 }
5238 len = eval_fname_script(*arg);
5239 if (len > 0)
5240 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005241 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 *arg += len;
5243 }
5244
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005246 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005248 p = find_name_end(*arg, &expr_start, &expr_end,
5249 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 if (expr_start != NULL)
5251 {
5252 char_u *temp_string;
5253
5254 if (!evaluate)
5255 {
5256 len += (int)(p - *arg);
5257 *arg = skipwhite(p);
5258 return len;
5259 }
5260
5261 /*
5262 * Include any <SID> etc in the expanded string:
5263 * Thus the -len here.
5264 */
5265 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5266 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005267 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 *alias = temp_string;
5269 *arg = skipwhite(p);
5270 return (int)STRLEN(temp_string);
5271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272
5273 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005274 // Only give an error when there is something, otherwise it will be
5275 // reported at a higher level.
5276 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005277 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278
5279 return len;
5280}
5281
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005282/*
5283 * Find the end of a variable or function name, taking care of magic braces.
5284 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5285 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005286 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005287 * Return a pointer to just after the name. Equal to "arg" if there is no
5288 * valid name.
5289 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005290 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005291find_name_end(
5292 char_u *arg,
5293 char_u **expr_start,
5294 char_u **expr_end,
5295 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005297 int mb_nest = 0;
5298 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005300 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005301 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005303 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005305 *expr_start = NULL;
5306 *expr_end = NULL;
5307 }
5308
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005309 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005310 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5311 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005312 return arg;
5313
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005314 for (p = arg; *p != NUL
5315 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005316 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005317 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005318 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005319 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005320 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005321 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005322 if (*p == '\'')
5323 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005324 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005325 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005326 ;
5327 if (*p == NUL)
5328 break;
5329 }
5330 else if (*p == '"')
5331 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005332 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005333 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005334 if (*p == '\\' && p[1] != NUL)
5335 ++p;
5336 if (*p == NUL)
5337 break;
5338 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005339 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5340 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005341 // "s:" is start of "s:var", but "n:" is not and can be used in
5342 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005343 len = (int)(p - arg);
5344 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005345 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005346 break;
5347 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005348
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005349 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005351 if (*p == '[')
5352 ++br_nest;
5353 else if (*p == ']')
5354 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005356
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005357 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005359 if (*p == '{')
5360 {
5361 mb_nest++;
5362 if (expr_start != NULL && *expr_start == NULL)
5363 *expr_start = p;
5364 }
5365 else if (*p == '}')
5366 {
5367 mb_nest--;
5368 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5369 *expr_end = p;
5370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 }
5373
5374 return p;
5375}
5376
5377/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005378 * Expands out the 'magic' {}'s in a variable/function name.
5379 * Note that this can call itself recursively, to deal with
5380 * constructs like foo{bar}{baz}{bam}
5381 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5382 * "in_start" ^
5383 * "expr_start" ^
5384 * "expr_end" ^
5385 * "in_end" ^
5386 *
5387 * Returns a new allocated string, which the caller must free.
5388 * Returns NULL for failure.
5389 */
5390 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005391make_expanded_name(
5392 char_u *in_start,
5393 char_u *expr_start,
5394 char_u *expr_end,
5395 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005396{
5397 char_u c1;
5398 char_u *retval = NULL;
5399 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005400
5401 if (expr_end == NULL || in_end == NULL)
5402 return NULL;
5403 *expr_start = NUL;
5404 *expr_end = NUL;
5405 c1 = *in_end;
5406 *in_end = NUL;
5407
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005408 temp_result = eval_to_string(expr_start + 1, FALSE);
5409 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005410 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005411 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5412 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005413 if (retval != NULL)
5414 {
5415 STRCPY(retval, in_start);
5416 STRCAT(retval, temp_result);
5417 STRCAT(retval, expr_end + 1);
5418 }
5419 }
5420 vim_free(temp_result);
5421
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005422 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005423 *expr_start = '{';
5424 *expr_end = '}';
5425
5426 if (retval != NULL)
5427 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005428 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005429 if (expr_start != NULL)
5430 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005431 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005432 temp_result = make_expanded_name(retval, expr_start,
5433 expr_end, temp_result);
5434 vim_free(retval);
5435 retval = temp_result;
5436 }
5437 }
5438
5439 return retval;
5440}
5441
5442/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005444 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005446 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005447eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005449 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005450}
5451
5452/*
5453 * Return TRUE if character "c" can be used as the first character in a
5454 * variable or function name (excluding '{' and '}').
5455 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005456 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005457eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005458{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005459 return ASCII_ISALPHA(c) || c == '_';
5460}
5461
5462/*
5463 * Return TRUE if character "c" can be used as the first character of a
5464 * dictionary key.
5465 */
5466 int
5467eval_isdictc(int c)
5468{
5469 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470}
5471
5472/*
Bram Moolenaare3c37d82020-08-15 18:39:05 +02005473 * Return the character "str[index]" where "index" is the character index. If
5474 * "index" is out of range NULL is returned.
5475 */
5476 char_u *
5477char_from_string(char_u *str, varnumber_T index)
5478{
5479 size_t nbyte = 0;
5480 varnumber_T nchar = index;
5481 size_t slen;
5482
5483 if (str == NULL || index < 0)
5484 return NULL;
5485 slen = STRLEN(str);
5486 while (nchar > 0 && nbyte < slen)
5487 {
5488 nbyte += MB_CPTR2LEN(str + nbyte);
5489 --nchar;
5490 }
5491 if (nbyte >= slen)
5492 return NULL;
5493 return vim_strnsave(str + nbyte, MB_CPTR2LEN(str + nbyte));
5494}
5495
5496/*
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005497 * Get the byte index for character index "idx" in string "str" with length
5498 * "str_len".
5499 * If going over the end return "str_len".
5500 * If "idx" is negative count from the end, -1 is the last character.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005501 * When going over the start return -1.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005502 */
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005503 static long
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005504char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
5505{
5506 varnumber_T nchar = idx;
5507 size_t nbyte = 0;
5508
5509 if (nchar >= 0)
5510 {
5511 while (nchar > 0 && nbyte < str_len)
5512 {
5513 nbyte += MB_CPTR2LEN(str + nbyte);
5514 --nchar;
5515 }
5516 }
5517 else
5518 {
5519 nbyte = str_len;
5520 while (nchar < 0 && nbyte > 0)
5521 {
5522 --nbyte;
5523 nbyte -= mb_head_off(str, str + nbyte);
5524 ++nchar;
5525 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005526 if (nchar < 0)
5527 return -1;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005528 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005529 return (long)nbyte;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005530}
5531
5532/*
5533 * Return the slice "str[first:last]" using character indexes.
5534 * Return NULL when the result is empty.
5535 */
5536 char_u *
5537string_slice(char_u *str, varnumber_T first, varnumber_T last)
5538{
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005539 long start_byte, end_byte;
5540 size_t slen;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005541
5542 if (str == NULL)
5543 return NULL;
5544 slen = STRLEN(str);
5545 start_byte = char_idx2byte(str, slen, first);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005546 if (start_byte < 0)
5547 start_byte = 0; // first index very negative: use zero
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005548 if (last == -1)
Bram Moolenaar25660542020-08-28 16:38:11 +02005549 end_byte = (long)slen;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005550 else
5551 {
5552 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005553 if (end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005554 // end index is inclusive
5555 end_byte += MB_CPTR2LEN(str + end_byte);
5556 }
5557
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005558 if (start_byte >= (long)slen || end_byte <= start_byte)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005559 return NULL;
5560 return vim_strnsave(str + start_byte, end_byte - start_byte);
5561}
5562
5563/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005564 * Handle:
5565 * - expr[expr], expr[expr:expr] subscript
5566 * - ".name" lookup
5567 * - function call with Funcref variable: func(expr)
5568 * - method call: var->method()
5569 *
5570 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005571 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005572 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005573handle_subscript(
5574 char_u **arg,
5575 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005576 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005577 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005578{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005579 int evaluate = evalarg != NULL
5580 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005581 int ret = OK;
5582 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005583 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005584 int getnext;
5585 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005586
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005587 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005588 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005589 // When at the end of the line and ".name" or "->{" or "->X" follows in
5590 // the next line then consume the line break.
5591 p = eval_next_non_blank(*arg, evalarg, &getnext);
5592 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005593 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaar9d489562020-07-30 20:08:50 +02005594 || (p[0] == '-' && p[1] == '>'
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005595 && (p[2] == '{' || ASCII_ISALPHA(p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005596 {
5597 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005598 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005599 check_white = FALSE;
5600 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005601
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005602 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5603 || rettv->v_type == VAR_PARTIAL))
5604 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005605 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005606 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5607 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005608
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005609 // Stop the expression evaluation when immediately aborting on
5610 // error, or when an interrupt occurred or an exception was thrown
5611 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005612 if (aborting())
5613 {
5614 if (ret == OK)
5615 clear_tv(rettv);
5616 ret = FAIL;
5617 }
5618 dict_unref(selfdict);
5619 selfdict = NULL;
5620 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005621 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005622 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02005623 *arg = p;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005624 if (ret == OK)
5625 {
5626 if ((*arg)[2] == '{')
5627 // expr->{lambda}()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005628 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005629 else
5630 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005631 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005632 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005633 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005634 // "." is ".name" lookup when we found a dict or when evaluating and
5635 // scriptversion is at least 2, where string concatenation is "..".
5636 else if (**arg == '['
5637 || (**arg == '.' && (rettv->v_type == VAR_DICT
5638 || (!evaluate
5639 && (*arg)[1] != '.'
5640 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005641 {
5642 dict_unref(selfdict);
5643 if (rettv->v_type == VAR_DICT)
5644 {
5645 selfdict = rettv->vval.v_dict;
5646 if (selfdict != NULL)
5647 ++selfdict->dv_refcount;
5648 }
5649 else
5650 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005651 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005652 {
5653 clear_tv(rettv);
5654 ret = FAIL;
5655 }
5656 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005657 else
5658 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005659 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005660
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005661 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5662 // Don't do this when "Func" is already a partial that was bound
5663 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005664 if (selfdict != NULL
5665 && (rettv->v_type == VAR_FUNC
5666 || (rettv->v_type == VAR_PARTIAL
5667 && (rettv->vval.v_partial->pt_auto
5668 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005669 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005670
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005671 dict_unref(selfdict);
5672 return ret;
5673}
5674
5675/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005676 * Make a copy of an item.
5677 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005678 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5679 * reference to an already copied list/dict can be used.
5680 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005681 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005682 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005683item_copy(
5684 typval_T *from,
5685 typval_T *to,
5686 int deep,
5687 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005688{
5689 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005690 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005691
Bram Moolenaar33570922005-01-25 22:26:29 +00005692 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005693 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005694 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005695 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005696 }
5697 ++recurse;
5698
5699 switch (from->v_type)
5700 {
5701 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005702 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005703 case VAR_STRING:
5704 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005705 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005706 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005707 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005708 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005709 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005710 copy_tv(from, to);
5711 break;
5712 case VAR_LIST:
5713 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005714 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005715 if (from->vval.v_list == NULL)
5716 to->vval.v_list = NULL;
5717 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5718 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005719 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005720 to->vval.v_list = from->vval.v_list->lv_copylist;
5721 ++to->vval.v_list->lv_refcount;
5722 }
5723 else
5724 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5725 if (to->vval.v_list == NULL)
5726 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005727 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005728 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005729 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005730 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005731 case VAR_DICT:
5732 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005733 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005734 if (from->vval.v_dict == NULL)
5735 to->vval.v_dict = NULL;
5736 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5737 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005738 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005739 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5740 ++to->vval.v_dict->dv_refcount;
5741 }
5742 else
5743 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5744 if (to->vval.v_dict == NULL)
5745 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005746 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005747 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005748 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005749 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005750 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005751 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005752 }
5753 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005754 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005755}
5756
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005757 void
5758echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5759{
5760 char_u *tofree;
5761 char_u numbuf[NUMBUFLEN];
5762 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5763
5764 if (*atstart)
5765 {
5766 *atstart = FALSE;
5767 // Call msg_start() after eval1(), evaluating the expression
5768 // may cause a message to appear.
5769 if (with_space)
5770 {
5771 // Mark the saved text as finishing the line, so that what
5772 // follows is displayed on a new line when scrolling back
5773 // at the more prompt.
5774 msg_sb_eol();
5775 msg_start();
5776 }
5777 }
5778 else if (with_space)
5779 msg_puts_attr(" ", echo_attr);
5780
5781 if (p != NULL)
5782 for ( ; *p != NUL && !got_int; ++p)
5783 {
5784 if (*p == '\n' || *p == '\r' || *p == TAB)
5785 {
5786 if (*p != TAB && *needclr)
5787 {
5788 // remove any text still there from the command
5789 msg_clr_eos();
5790 *needclr = FALSE;
5791 }
5792 msg_putchar_attr(*p, echo_attr);
5793 }
5794 else
5795 {
5796 if (has_mbyte)
5797 {
5798 int i = (*mb_ptr2len)(p);
5799
5800 (void)msg_outtrans_len_attr(p, i, echo_attr);
5801 p += i - 1;
5802 }
5803 else
5804 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5805 }
5806 }
5807 vim_free(tofree);
5808}
5809
Bram Moolenaare9a41262005-01-15 22:18:47 +00005810/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 * ":echo expr1 ..." print each argument separated with a space, add a
5812 * newline at the end.
5813 * ":echon expr1 ..." print each argument plain.
5814 */
5815 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005816ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817{
5818 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005819 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 char_u *p;
5821 int needclr = TRUE;
5822 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01005823 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005824 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005825 evalarg_T evalarg;
5826
Bram Moolenaare707c882020-07-01 13:07:37 +02005827 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828
5829 if (eap->skip)
5830 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005831 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005833 // If eval1() causes an error message the text from the command may
5834 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005835 need_clr_eos = needclr;
5836
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005838 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 {
5840 /*
5841 * Report the invalid expression unless the expression evaluation
5842 * has been cancelled due to an aborting error, an interrupt, or an
5843 * exception.
5844 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005845 if (!aborting() && did_emsg == did_emsg_before
5846 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005847 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005848 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 break;
5850 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005851 need_clr_eos = FALSE;
5852
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005854 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005855
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005856 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 arg = skipwhite(arg);
5858 }
5859 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02005860 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861
5862 if (eap->skip)
5863 --emsg_skip;
5864 else
5865 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005866 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 if (needclr)
5868 msg_clr_eos();
5869 if (eap->cmdidx == CMD_echo)
5870 msg_end();
5871 }
5872}
5873
5874/*
5875 * ":echohl {name}".
5876 */
5877 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005878ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005880 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881}
5882
5883/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005884 * Returns the :echo attribute
5885 */
5886 int
5887get_echo_attr(void)
5888{
5889 return echo_attr;
5890}
5891
5892/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 * ":execute expr1 ..." execute the result of an expression.
5894 * ":echomsg expr1 ..." Print a message
5895 * ":echoerr expr1 ..." Print an error
5896 * Each gets spaces around each argument and a newline at the end for
5897 * echo commands
5898 */
5899 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005900ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901{
5902 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005903 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 int ret = OK;
5905 char_u *p;
5906 garray_T ga;
5907 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908
5909 ga_init2(&ga, 1, 80);
5910
5911 if (eap->skip)
5912 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02005913 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02005915 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01005916 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918
5919 if (!eap->skip)
5920 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005921 char_u buf[NUMBUFLEN];
5922
5923 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01005924 {
5925 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
5926 {
5927 emsg(_(e_inval_string));
5928 p = NULL;
5929 }
5930 else
5931 p = tv_get_string_buf(&rettv, buf);
5932 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005933 else
5934 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005935 if (p == NULL)
5936 {
5937 clear_tv(&rettv);
5938 ret = FAIL;
5939 break;
5940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 len = (int)STRLEN(p);
5942 if (ga_grow(&ga, len + 2) == FAIL)
5943 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005944 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 ret = FAIL;
5946 break;
5947 }
5948 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 ga.ga_len += len;
5952 }
5953
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005954 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 arg = skipwhite(arg);
5956 }
5957
5958 if (ret != FAIL && ga.ga_data != NULL)
5959 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005960 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
5961 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005962 // Mark the already saved text as finishing the line, so that what
5963 // follows is displayed on a new line when scrolling back at the
5964 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005965 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005966 }
5967
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005969 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005970 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005971 out_flush();
5972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 else if (eap->cmdidx == CMD_echoerr)
5974 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005975 int save_did_emsg = did_emsg;
5976
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005977 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005978 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 if (!force_abort)
5980 did_emsg = save_did_emsg;
5981 }
5982 else if (eap->cmdidx == CMD_execute)
5983 do_cmdline((char_u *)ga.ga_data,
5984 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
5985 }
5986
5987 ga_clear(&ga);
5988
5989 if (eap->skip)
5990 --emsg_skip;
5991
5992 eap->nextcmd = check_nextcmd(arg);
5993}
5994
5995/*
5996 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
5997 * "arg" points to the "&" or '+' when called, to "option" when returning.
5998 * Returns NULL when no option name found. Otherwise pointer to the char
5999 * after the option name.
6000 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006001 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006002find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003{
6004 char_u *p = *arg;
6005
6006 ++p;
6007 if (*p == 'g' && p[1] == ':')
6008 {
6009 *opt_flags = OPT_GLOBAL;
6010 p += 2;
6011 }
6012 else if (*p == 'l' && p[1] == ':')
6013 {
6014 *opt_flags = OPT_LOCAL;
6015 p += 2;
6016 }
6017 else
6018 *opt_flags = 0;
6019
6020 if (!ASCII_ISALPHA(*p))
6021 return NULL;
6022 *arg = p;
6023
6024 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006025 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 else
6027 while (ASCII_ISALPHA(*p))
6028 ++p;
6029 return p;
6030}
6031
6032/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006033 * Display script name where an item was last set.
6034 * Should only be invoked when 'verbose' is non-zero.
6035 */
6036 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006037last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006038{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006039 char_u *p;
6040
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006041 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006042 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006043 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006044 if (p != NULL)
6045 {
6046 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006047 msg_puts(_("\n\tLast set from "));
6048 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006049 if (script_ctx.sc_lnum > 0)
6050 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006051 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006052 msg_outnum((long)script_ctx.sc_lnum);
6053 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006054 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006055 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006056 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006057 }
6058}
6059
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006060#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061
6062/*
6063 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006064 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 * "flags" can be "g" to do a global substitute.
6066 * Returns an allocated string, NULL for error.
6067 */
6068 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006069do_string_sub(
6070 char_u *str,
6071 char_u *pat,
6072 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006073 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006074 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075{
6076 int sublen;
6077 regmatch_T regmatch;
6078 int i;
6079 int do_all;
6080 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006081 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 garray_T ga;
6083 char_u *ret;
6084 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006085 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006087 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006089 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090
6091 ga_init2(&ga, 1, 200);
6092
6093 do_all = (flags[0] == 'g');
6094
6095 regmatch.rm_ic = p_ic;
6096 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6097 if (regmatch.regprog != NULL)
6098 {
6099 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006100 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6102 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006103 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006104 if (regmatch.startp[0] == regmatch.endp[0])
6105 {
6106 if (zero_width == regmatch.startp[0])
6107 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006108 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006109 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006110 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6111 (size_t)i);
6112 ga.ga_len += i;
6113 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006114 continue;
6115 }
6116 zero_width = regmatch.startp[0];
6117 }
6118
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 /*
6120 * Get some space for a temporary buffer to do the substitution
6121 * into. It will contain:
6122 * - The text up to where the match is.
6123 * - The substituted text.
6124 * - The text after the match.
6125 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006126 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006127 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6129 {
6130 ga_clear(&ga);
6131 break;
6132 }
6133
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006134 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 i = (int)(regmatch.startp[0] - tail);
6136 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006137 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006138 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 + ga.ga_len + i, TRUE, TRUE, FALSE);
6140 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006141 tail = regmatch.endp[0];
6142 if (*tail == NUL)
6143 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 if (!do_all)
6145 break;
6146 }
6147
6148 if (ga.ga_data != NULL)
6149 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6150
Bram Moolenaar473de612013-06-08 18:19:48 +02006151 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 }
6153
6154 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6155 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006156 if (p_cpo == empty_option)
6157 p_cpo = save_cpo;
6158 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006159 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006160 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161
6162 return ret;
6163}