blob: 98d16c82629cb49d253d79b28d1bf9509cd61071 [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 Moolenaar7454a062016-01-30 15:14:10 +0100371skip_expr(char_u **pp)
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 Moolenaar5409f5d2020-06-24 18:37:35 +0200376 return eval1(pp, &rettv, NULL);
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 Moolenaar7454a062016-01-30 15:14:10 +0100477eval_to_string(
478 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100479 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480{
Bram Moolenaar33570922005-01-25 22:26:29 +0000481 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000483 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000484#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000485 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200488 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 retval = NULL;
490 else
491 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000492 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000493 {
494 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000495 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200496 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200497 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200498 if (tv.vval.v_list->lv_len > 0)
499 ga_append(&ga, NL);
500 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000501 ga_append(&ga, NUL);
502 retval = (char_u *)ga.ga_data;
503 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000504#ifdef FEAT_FLOAT
505 else if (convert && tv.v_type == VAR_FLOAT)
506 {
507 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
508 retval = vim_strsave(numbuf);
509 }
510#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000511 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100512 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000513 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 }
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200515 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516
517 return retval;
518}
519
520/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000521 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200522 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200523 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 */
525 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100526eval_to_string_safe(
527 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100528 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529{
530 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200531 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200532 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200534 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200535 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000536 if (use_sandbox)
537 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200538 ++textwinlock;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200539 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000540 if (use_sandbox)
541 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200542 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200543 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200544 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 return retval;
546}
547
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548/*
549 * Top level evaluation function, returning a number.
550 * Evaluates "expr" silently.
551 * Returns -1 for an error.
552 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200553 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100554eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555{
Bram Moolenaar33570922005-01-25 22:26:29 +0000556 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200557 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000558 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559
560 ++emsg_off;
561
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200562 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 retval = -1;
564 else
565 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100566 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000567 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 }
569 --emsg_off;
570
571 return retval;
572}
573
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000574/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000575 * Top level evaluation function.
576 * Returns an allocated typval_T with the result.
577 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000578 */
579 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200580eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000581{
582 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200583 evalarg_T evalarg;
584
585 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000586
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200587 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200588 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100589 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000590
Bram Moolenaar37c83712020-06-30 21:18:36 +0200591 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000592 return tv;
593}
594
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100596 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200597 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
598 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000599 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200601 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100602call_vim_function(
603 char_u *func,
604 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200605 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200606 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000608 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200609 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100611 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200612 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200613 funcexe.firstline = curwin->w_cursor.lnum;
614 funcexe.lastline = curwin->w_cursor.lnum;
615 funcexe.evaluate = TRUE;
616 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000617 if (ret == FAIL)
618 clear_tv(rettv);
619
620 return ret;
621}
622
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100623/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100624 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100625 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200626 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
627 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100628 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200629 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100630call_func_retnr(
631 char_u *func,
632 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200633 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100634{
635 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200636 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100637
Bram Moolenaarded27a12018-08-01 19:06:03 +0200638 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100639 return -1;
640
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100641 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100642 clear_tv(&rettv);
643 return retval;
644}
645
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000646/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100647 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000648 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200649 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
650 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000651 */
652 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100653call_func_retstr(
654 char_u *func,
655 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200656 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000657{
658 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000659 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000660
Bram Moolenaarded27a12018-08-01 19:06:03 +0200661 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000662 return NULL;
663
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100664 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000665 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 return retval;
667}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000668
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000669/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100670 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200671 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
672 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000673 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000674 */
675 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100676call_func_retlist(
677 char_u *func,
678 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200679 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000680{
681 typval_T rettv;
682
Bram Moolenaarded27a12018-08-01 19:06:03 +0200683 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000684 return NULL;
685
686 if (rettv.v_type != VAR_LIST)
687 {
688 clear_tv(&rettv);
689 return NULL;
690 }
691
692 return rettv.vval.v_list;
693}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695#ifdef FEAT_FOLDING
696/*
697 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
698 * it in "*cp". Doesn't give error messages.
699 */
700 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100701eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702{
Bram Moolenaar33570922005-01-25 22:26:29 +0000703 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200704 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000706 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
707 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708
709 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000710 if (use_sandbox)
711 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200712 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200714 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 retval = 0;
716 else
717 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100718 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000719 if (tv.v_type == VAR_NUMBER)
720 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000721 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 retval = 0;
723 else
724 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100725 // If the result is a string, check if there is a non-digit before
726 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000727 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 if (!VIM_ISDIGIT(*s) && *s != '-')
729 *cp = *s++;
730 retval = atol((char *)s);
731 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000732 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 }
734 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000735 if (use_sandbox)
736 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200737 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200738 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200740 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741}
742#endif
743
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000745 * Get an lval: variable, Dict item or List item that can be assigned a value
746 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
747 * "name.key", "name.key[expr]" etc.
748 * Indexing only works if "name" is an existing List or Dictionary.
749 * "name" points to the start of the name.
750 * If "rettv" is not NULL it points to the value to be assigned.
751 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
752 * wrong; must end in space or cmd separator.
753 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100754 * flags:
755 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100756 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100757 * GLV_NO_AUTOLOAD: do not use script autoloading
758 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000759 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000760 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000761 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000762 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200763 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100764get_lval(
765 char_u *name,
766 typval_T *rettv,
767 lval_T *lp,
768 int unlet,
769 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100770 int flags, // GLV_ values
771 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000772{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000773 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000774 char_u *expr_start, *expr_end;
775 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000776 dictitem_T *v;
777 typval_T var1;
778 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000779 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000780 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000781 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000782 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +0000783 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100784 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000785
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100786 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200787 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000788
789 if (skip)
790 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100791 // When skipping just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000792 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200793 lp->ll_name_end = find_name_end(name, NULL, NULL,
794 FNE_INCL_BR | fne_flags);
795 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000796 }
797
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100798 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000799 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100800 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000801 if (expr_start != NULL)
802 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100803 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100804 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000805 && *p != '[' && *p != '.')
806 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200807 semsg(_(e_trailing_arg), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000808 return NULL;
809 }
810
811 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
812 if (lp->ll_exp_name == NULL)
813 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100814 // Report an invalid expression in braces, unless the
815 // expression evaluation has been cancelled due to an
816 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000817 if (!aborting() && !quiet)
818 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000819 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100820 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000821 return NULL;
822 }
823 }
824 lp->ll_name = lp->ll_exp_name;
825 }
826 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100827 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000828 lp->ll_name = name;
829
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200830 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100831 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200832 // "a: type" is declaring variable "a" with a type, not "a:".
833 if (p == name + 2 && p[-1] == ':')
834 {
835 --p;
836 lp->ll_name_end = p;
837 }
838 if (*p == ':')
839 {
840 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
841 char_u *tp = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100842
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200843 // parse the type after the name
844 lp->ll_type = parse_type(&tp, &si->sn_type_list);
845 lp->ll_name_end = tp;
846 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100847 }
848 }
849
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100850 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000851 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
852 return p;
853
854 cc = *p;
855 *p = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100856 // Only pass &ht when we would write to the variable, it prevents autoload
857 // as well.
Bram Moolenaar6e65d592017-12-07 22:11:27 +0100858 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
859 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000860 if (v == NULL && !quiet)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200861 semsg(_(e_undefined_variable_str), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000862 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000863 if (v == NULL)
864 return NULL;
865
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000866 /*
867 * Loop until no more [idx] or .key is following.
868 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000869 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100870 var1.v_type = VAR_UNKNOWN;
871 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000872 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000873 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000874 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
Bram Moolenaar81ed4962020-09-23 15:56:50 +0200875 && !(lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100876 && !(lp->ll_tv->v_type == VAR_BLOB
877 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000878 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000879 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100880 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000881 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000882 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000883 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000884 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000885 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100886 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000887 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000888 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000889
Bram Moolenaar10c65862020-10-08 21:16:42 +0200890 if (in_vim9script() && lp->ll_valtype == NULL
891 && lp->ll_tv == &v->di_tv
892 && ht != NULL && ht == get_script_local_ht())
893 {
894 svar_T *sv = find_typval_in_script(lp->ll_tv);
895
896 // Vim9 script local variable: get the type
897 if (sv != NULL)
898 lp->ll_valtype = sv->sv_type;
899 }
900
Bram Moolenaar8c711452005-01-14 21:53:12 +0000901 len = -1;
902 if (*p == '.')
903 {
904 key = p + 1;
905 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
906 ;
907 if (len == 0)
908 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000909 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100910 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000911 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000912 }
913 p = key + len;
914 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000915 else
916 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100917 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000918 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000919 if (*p == ':')
920 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000921 else
922 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000923 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200924 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000925 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100926 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000927 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100928 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000929 clear_tv(&var1);
930 return NULL;
931 }
Bram Moolenaar6a250262020-08-04 15:53:01 +0200932 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000933 }
934
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100935 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000936 if (*p == ':')
937 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000938 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000939 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000940 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +0200941 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100942 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000943 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000944 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100945 if (rettv != NULL
946 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100947 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100948 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100949 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +0000950 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000951 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100952 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100953 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000954 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000955 }
956 p = skipwhite(p + 1);
957 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000958 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000959 else
960 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000961 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200962 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200963 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000964 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100965 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000966 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000967 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100968 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000969 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100970 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100971 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000972 clear_tv(&var2);
973 return NULL;
974 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000975 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000976 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000977 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000978 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000979 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000980
Bram Moolenaar8c711452005-01-14 21:53:12 +0000981 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000982 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000983 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100984 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100985 clear_tv(&var1);
986 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000987 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000988 }
989
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100990 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000991 ++p;
992 }
993
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000994 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000995 {
996 if (len == -1)
997 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100998 // "[key]": get key from "var1"
999 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001000 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001001 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001002 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001003 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001004 }
1005 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001006 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001007
1008 // a NULL dict is equivalent with an empty dict
1009 if (lp->ll_tv->vval.v_dict == NULL)
1010 {
1011 lp->ll_tv->vval.v_dict = dict_alloc();
1012 if (lp->ll_tv->vval.v_dict == NULL)
1013 {
1014 clear_tv(&var1);
1015 return NULL;
1016 }
1017 ++lp->ll_tv->vval.v_dict->dv_refcount;
1018 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001019 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001020
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001021 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001022
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001023 // When assigning to a scope dictionary check that a function and
1024 // variable name is valid (only variable name unless it is l: or
1025 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001026 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001027 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001028 int prevval;
1029 int wrong;
1030
1031 if (len != -1)
1032 {
1033 prevval = key[len];
1034 key[len] = NUL;
1035 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001036 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001037 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001038 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1039 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001040 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001041 || !valid_varname(key);
1042 if (len != -1)
1043 key[len] = prevval;
1044 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001045 {
1046 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001047 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001048 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001049 }
1050
Bram Moolenaar10c65862020-10-08 21:16:42 +02001051 if (lp->ll_valtype != NULL)
1052 // use the type of the member
1053 lp->ll_valtype = lp->ll_valtype->tt_member;
1054
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001055 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001056 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001057 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001058 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001059 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001060 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001061 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001062 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001063 return NULL;
1064 }
1065
Bram Moolenaar31b81602019-02-10 22:14:27 +01001066 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001067 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001068 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001069 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001070 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001071 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001072 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001073 }
1074 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001075 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001076 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001077 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001078 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001079 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001080 p = NULL;
1081 break;
1082 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001083 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001084 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001085 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1086 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001087 {
1088 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001089 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001090 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001091
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001092 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001093 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001094 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001095 else if (lp->ll_tv->v_type == VAR_BLOB)
1096 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001097 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1098
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001099 /*
1100 * Get the number and item for the only or first index of the List.
1101 */
1102 if (empty1)
1103 lp->ll_n1 = 0;
1104 else
1105 // is number or string
1106 lp->ll_n1 = (long)tv_get_number(&var1);
1107 clear_tv(&var1);
1108
1109 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001110 || lp->ll_n1 > bloblen
1111 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001112 {
1113 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001114 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001115 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001116 return NULL;
1117 }
1118 if (lp->ll_range && !lp->ll_empty2)
1119 {
1120 lp->ll_n2 = (long)tv_get_number(&var2);
1121 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001122 if (lp->ll_n2 < 0
1123 || lp->ll_n2 >= bloblen
1124 || lp->ll_n2 < lp->ll_n1)
1125 {
1126 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001127 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001128 return NULL;
1129 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001130 }
1131 lp->ll_blob = lp->ll_tv->vval.v_blob;
1132 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001133 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001134 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001135 else
1136 {
1137 /*
1138 * Get the number and item for the only or first index of the List.
1139 */
1140 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001141 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001142 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001143 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001144 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001145 clear_tv(&var1);
1146
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001147 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001148 lp->ll_list = lp->ll_tv->vval.v_list;
1149 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1150 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001151 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001152 if (lp->ll_n1 < 0)
1153 {
1154 lp->ll_n1 = 0;
1155 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1156 }
1157 }
1158 if (lp->ll_li == NULL)
1159 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001160 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001161 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001162 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001163 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001164 }
1165
Bram Moolenaar10c65862020-10-08 21:16:42 +02001166 if (lp->ll_valtype != NULL)
1167 // use the type of the member
1168 lp->ll_valtype = lp->ll_valtype->tt_member;
1169
Bram Moolenaar8c711452005-01-14 21:53:12 +00001170 /*
1171 * May need to find the item or absolute index for the second
1172 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001173 * When no index given: "lp->ll_empty2" is TRUE.
1174 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001175 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001176 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001177 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001178 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001179 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001180 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001181 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001182 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001183 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001184 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001185 {
1186 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001187 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001188 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001189 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001190 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001191 }
1192
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001193 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001194 if (lp->ll_n1 < 0)
1195 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1196 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001197 {
1198 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001199 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001200 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001201 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001202 }
1203
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001204 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001205 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001206 }
1207
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001208 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001209 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001210 return p;
1211}
1212
1213/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001214 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001215 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001216 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001217clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001218{
1219 vim_free(lp->ll_exp_name);
1220 vim_free(lp->ll_newkey);
1221}
1222
1223/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001224 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001225 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001226 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1227 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001228 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001229 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001230set_var_lval(
1231 lval_T *lp,
1232 char_u *endp,
1233 typval_T *rettv,
1234 int copy,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001235 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar7454a062016-01-30 15:14:10 +01001236 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001237{
1238 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001239 listitem_T *ri;
1240 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001241
1242 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001243 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001244 cc = *endp;
1245 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001246 if (lp->ll_blob != NULL)
1247 {
1248 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001249
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001250 if (op != NULL && *op != '=')
1251 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001252 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001253 return;
1254 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001255 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001256 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001257
1258 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1259 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001260 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001261
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001262 if (lp->ll_empty2)
1263 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1264
1265 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001266 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001267 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001268 return;
1269 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001270 if (lp->ll_empty2)
1271 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001272
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001273 ir = 0;
1274 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1275 blob_set(lp->ll_blob, il,
1276 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001277 }
1278 else
1279 {
1280 val = (int)tv_get_number_chk(rettv, &error);
1281 if (!error)
1282 {
1283 garray_T *gap = &lp->ll_blob->bv_ga;
1284
1285 // Allow for appending a byte. Setting a byte beyond
1286 // the end is an error otherwise.
1287 if (lp->ll_n1 < gap->ga_len
1288 || (lp->ll_n1 == gap->ga_len
1289 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1290 {
1291 blob_set(lp->ll_blob, lp->ll_n1, val);
1292 if (lp->ll_n1 == gap->ga_len)
1293 ++gap->ga_len;
1294 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001295 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001296 }
1297 }
1298 }
1299 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001300 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001301 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001302
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001303 if (flags & ASSIGN_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001304 {
1305 emsg(_(e_cannot_mod));
1306 *endp = cc;
1307 return;
1308 }
1309
Bram Moolenaarff697e62019-02-12 22:28:33 +01001310 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001311 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001312 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar79518e22017-02-17 16:31:35 +01001313 &tv, &di, TRUE, FALSE) == OK)
1314 {
1315 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001316 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1317 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001318 && tv_op(&tv, rettv, op) == OK)
1319 set_var(lp->ll_name, &tv, FALSE);
1320 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001321 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001322 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001323 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001324 {
1325 if (lp->ll_type != NULL
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001326 && check_typval_type(lp->ll_type, rettv, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001327 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001328 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001329 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001330 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001331 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001332 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001333 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001334 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001335 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001336 else if (lp->ll_range)
1337 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001338 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001339 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001340
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001341 if (flags & ASSIGN_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001342 {
1343 emsg(_("E996: Cannot lock a range"));
1344 return;
1345 }
1346
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001347 /*
1348 * Check whether any of the list items is locked
1349 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001350 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001351 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001352 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001353 return;
1354 ri = ri->li_next;
1355 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1356 break;
1357 ll_li = ll_li->li_next;
1358 ++ll_n1;
1359 }
1360
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001361 /*
1362 * Assign the List values to the list items.
1363 */
1364 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001365 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001366 if (op != NULL && *op != '=')
1367 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1368 else
1369 {
1370 clear_tv(&lp->ll_li->li_tv);
1371 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1372 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001373 ri = ri->li_next;
1374 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1375 break;
1376 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001377 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001378 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001379 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001380 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001381 ri = NULL;
1382 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001383 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001384 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001385 lp->ll_li = lp->ll_li->li_next;
1386 ++lp->ll_n1;
1387 }
1388 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001389 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001390 else if (lp->ll_empty2
1391 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001392 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001393 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001394 }
1395 else
1396 {
1397 /*
1398 * Assign to a List or Dictionary item.
1399 */
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001400 if (flags & ASSIGN_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001401 {
1402 emsg(_("E996: Cannot lock a list or dict"));
1403 return;
1404 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001405
1406 if (lp->ll_valtype != NULL
1407 && check_typval_type(lp->ll_valtype, rettv, 0) == FAIL)
1408 return;
1409
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001410 if (lp->ll_newkey != NULL)
1411 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001412 if (op != NULL && *op != '=')
1413 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001414 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001415 return;
1416 }
1417
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001418 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001419 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001420 if (di == NULL)
1421 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001422 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1423 {
1424 vim_free(di);
1425 return;
1426 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001427 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001428 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001429 else if (op != NULL && *op != '=')
1430 {
1431 tv_op(lp->ll_tv, rettv, op);
1432 return;
1433 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001434 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001435 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001436
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001437 /*
1438 * Assign the value to the variable or list item.
1439 */
1440 if (copy)
1441 copy_tv(rettv, lp->ll_tv);
1442 else
1443 {
1444 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001445 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001446 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001447 }
1448 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001449}
1450
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001451/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001452 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1453 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001454 * Returns OK or FAIL.
1455 */
1456 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001457tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001458{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001459 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001460 char_u numbuf[NUMBUFLEN];
1461 char_u *s;
1462
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001463 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001464 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001465 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001466 {
1467 switch (tv1->v_type)
1468 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001469 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001470 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001471 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001472 case VAR_DICT:
1473 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001474 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001475 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001476 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001477 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001478 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001479 break;
1480
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001481 case VAR_BLOB:
1482 if (*op != '+' || tv2->v_type != VAR_BLOB)
1483 break;
1484 // BLOB += BLOB
1485 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1486 {
1487 blob_T *b1 = tv1->vval.v_blob;
1488 blob_T *b2 = tv2->vval.v_blob;
1489 int i, len = blob_len(b2);
1490 for (i = 0; i < len; i++)
1491 ga_append(&b1->bv_ga, blob_get(b2, i));
1492 }
1493 return OK;
1494
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001495 case VAR_LIST:
1496 if (*op != '+' || tv2->v_type != VAR_LIST)
1497 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001498 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001499 if (tv2->vval.v_list != NULL)
1500 {
1501 if (tv1->vval.v_list == NULL)
1502 {
1503 tv1->vval.v_list = tv2->vval.v_list;
1504 ++tv1->vval.v_list->lv_refcount;
1505 }
1506 else
1507 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1508 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001509 return OK;
1510
1511 case VAR_NUMBER:
1512 case VAR_STRING:
1513 if (tv2->v_type == VAR_LIST)
1514 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001515 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001516 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001517 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001518 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001519#ifdef FEAT_FLOAT
1520 if (tv2->v_type == VAR_FLOAT)
1521 {
1522 float_T f = n;
1523
Bram Moolenaarff697e62019-02-12 22:28:33 +01001524 if (*op == '%')
1525 break;
1526 switch (*op)
1527 {
1528 case '+': f += tv2->vval.v_float; break;
1529 case '-': f -= tv2->vval.v_float; break;
1530 case '*': f *= tv2->vval.v_float; break;
1531 case '/': f /= tv2->vval.v_float; break;
1532 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001533 clear_tv(tv1);
1534 tv1->v_type = VAR_FLOAT;
1535 tv1->vval.v_float = f;
1536 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001537 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001538#endif
1539 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001540 switch (*op)
1541 {
1542 case '+': n += tv_get_number(tv2); break;
1543 case '-': n -= tv_get_number(tv2); break;
1544 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001545 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1546 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001547 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001548 clear_tv(tv1);
1549 tv1->v_type = VAR_NUMBER;
1550 tv1->vval.v_number = n;
1551 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001552 }
1553 else
1554 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001555 if (tv2->v_type == VAR_FLOAT)
1556 break;
1557
Bram Moolenaarff697e62019-02-12 22:28:33 +01001558 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001559 s = tv_get_string(tv1);
1560 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001561 clear_tv(tv1);
1562 tv1->v_type = VAR_STRING;
1563 tv1->vval.v_string = s;
1564 }
1565 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001566
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001567 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001568#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001569 {
1570 float_T f;
1571
Bram Moolenaarff697e62019-02-12 22:28:33 +01001572 if (*op == '%' || *op == '.'
1573 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001574 && tv2->v_type != VAR_NUMBER
1575 && tv2->v_type != VAR_STRING))
1576 break;
1577 if (tv2->v_type == VAR_FLOAT)
1578 f = tv2->vval.v_float;
1579 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001580 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001581 switch (*op)
1582 {
1583 case '+': tv1->vval.v_float += f; break;
1584 case '-': tv1->vval.v_float -= f; break;
1585 case '*': tv1->vval.v_float *= f; break;
1586 case '/': tv1->vval.v_float /= f; break;
1587 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001588 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001589#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001590 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001591 }
1592 }
1593
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001594 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001595 return FAIL;
1596}
1597
1598/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001599 * Evaluate the expression used in a ":for var in expr" command.
1600 * "arg" points to "var".
1601 * Set "*errp" to TRUE for an error, FALSE otherwise;
1602 * Return a pointer that holds the info. Null when there is an error.
1603 */
1604 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001605eval_for_line(
1606 char_u *arg,
1607 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001608 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001609 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001610{
Bram Moolenaar33570922005-01-25 22:26:29 +00001611 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001612 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001613 typval_T tv;
1614 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001615 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001616
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001617 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001618
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001619 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001620 if (fi == NULL)
1621 return NULL;
1622
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001623 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001624 if (expr == NULL)
1625 return fi;
1626
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001627 expr = skipwhite_and_linebreak(expr, evalarg);
1628 if (expr[0] != 'i' || expr[1] != 'n'
1629 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001630 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001631 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001632 return fi;
1633 }
1634
1635 if (skip)
1636 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001637 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1638 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001639 {
1640 *errp = FALSE;
1641 if (!skip)
1642 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001643 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001644 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001645 l = tv.vval.v_list;
1646 if (l == NULL)
1647 {
1648 // a null list is like an empty list: do nothing
1649 clear_tv(&tv);
1650 }
1651 else
1652 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001653 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001654 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001655
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001656 // No need to increment the refcount, it's already set for
1657 // the list being used in "tv".
1658 fi->fi_list = l;
1659 list_add_watch(l, &fi->fi_lw);
1660 fi->fi_lw.lw_item = l->lv_first;
1661 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001662 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001663 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001664 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001665 fi->fi_bi = 0;
1666 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001667 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001668 typval_T btv;
1669
1670 // Make a copy, so that the iteration still works when the
1671 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001672 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001673 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001674 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001675 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001676 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001677 else
1678 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001679 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001680 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001681 }
1682 }
1683 }
1684 if (skip)
1685 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001686 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001687
1688 return fi;
1689}
1690
1691/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001692 * Used when looping over a :for line, skip the "in expr" part.
1693 */
1694 void
1695skip_for_lines(void *fi_void, evalarg_T *evalarg)
1696{
1697 forinfo_T *fi = (forinfo_T *)fi_void;
1698 int i;
1699
1700 for (i = 0; i < fi->fi_break_count; ++i)
1701 eval_next_line(evalarg);
1702}
1703
1704/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001705 * Use the first item in a ":for" list. Advance to the next.
1706 * Assign the values to the variable (list). "arg" points to the first one.
1707 * Return TRUE when a valid item was found, FALSE when at end of list or
1708 * something wrong.
1709 */
1710 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001711next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001712{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001713 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001714 int result;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001715 int flag = in_vim9script() ? ASSIGN_NO_DECL : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001716 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001717
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001718 if (fi->fi_blob != NULL)
1719 {
1720 typval_T tv;
1721
1722 if (fi->fi_bi >= blob_len(fi->fi_blob))
1723 return FALSE;
1724 tv.v_type = VAR_NUMBER;
1725 tv.v_lock = VAR_FIXED;
1726 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1727 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001728 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001729 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001730 }
1731
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001732 item = fi->fi_lw.lw_item;
1733 if (item == NULL)
1734 result = FALSE;
1735 else
1736 {
1737 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001738 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001739 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001740 }
1741 return result;
1742}
1743
1744/*
1745 * Free the structure used to store info used by ":for".
1746 */
1747 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001748free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001749{
Bram Moolenaar33570922005-01-25 22:26:29 +00001750 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001751
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001752 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001753 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001754 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001755 list_unref(fi->fi_list);
1756 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001757 if (fi != NULL && fi->fi_blob != NULL)
1758 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001759 vim_free(fi);
1760}
1761
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001763set_context_for_expression(
1764 expand_T *xp,
1765 char_u *arg,
1766 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001768 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001770 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001772 if (cmdidx == CMD_let || cmdidx == CMD_var
1773 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001774 {
1775 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001776 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001777 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001778 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001779 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001780 {
1781 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001782 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001783 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001784 break;
1785 }
1786 return;
1787 }
1788 }
1789 else
1790 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1791 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 while ((xp->xp_pattern = vim_strpbrk(arg,
1793 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1794 {
1795 c = *xp->xp_pattern;
1796 if (c == '&')
1797 {
1798 c = xp->xp_pattern[1];
1799 if (c == '&')
1800 {
1801 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001802 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 }
1804 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001805 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001807 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1808 xp->xp_pattern += 2;
1809
1810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 }
1812 else if (c == '$')
1813 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001814 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 xp->xp_context = EXPAND_ENV_VARS;
1816 }
1817 else if (c == '=')
1818 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001819 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 xp->xp_context = EXPAND_EXPRESSION;
1821 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001822 else if (c == '#'
1823 && xp->xp_context == EXPAND_EXPRESSION)
1824 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001825 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001826 break;
1827 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001828 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 && xp->xp_context == EXPAND_FUNCTIONS
1830 && vim_strchr(xp->xp_pattern, '(') == NULL)
1831 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001832 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 break;
1834 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001835 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001837 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 {
1839 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1840 if (c == '\\' && xp->xp_pattern[1] != NUL)
1841 ++xp->xp_pattern;
1842 xp->xp_context = EXPAND_NOTHING;
1843 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001844 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001846 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1848 /* skip */ ;
1849 xp->xp_context = EXPAND_NOTHING;
1850 }
1851 else if (c == '|')
1852 {
1853 if (xp->xp_pattern[1] == '|')
1854 {
1855 ++xp->xp_pattern;
1856 xp->xp_context = EXPAND_EXPRESSION;
1857 }
1858 else
1859 xp->xp_context = EXPAND_COMMANDS;
1860 }
1861 else
1862 xp->xp_context = EXPAND_EXPRESSION;
1863 }
1864 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001865 // Doesn't look like something valid, expand as an expression
1866 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001867 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 arg = xp->xp_pattern;
1869 if (*arg != NUL)
1870 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1871 /* skip */ ;
1872 }
1873 xp->xp_pattern = arg;
1874}
1875
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001877 * Return TRUE if "pat" matches "text".
1878 * Does not use 'cpo' and always uses 'magic'.
1879 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001880 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001881pattern_match(char_u *pat, char_u *text, int ic)
1882{
1883 int matches = FALSE;
1884 char_u *save_cpo;
1885 regmatch_T regmatch;
1886
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001887 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001888 save_cpo = p_cpo;
1889 p_cpo = (char_u *)"";
1890 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1891 if (regmatch.regprog != NULL)
1892 {
1893 regmatch.rm_ic = ic;
1894 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1895 vim_regfree(regmatch.regprog);
1896 }
1897 p_cpo = save_cpo;
1898 return matches;
1899}
1900
1901/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001902 * Handle a name followed by "(". Both for just "name(arg)" and for
1903 * "expr->name(arg)".
1904 * Returns OK or FAIL.
1905 */
1906 static int
1907eval_func(
1908 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02001909 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001910 char_u *name,
1911 int name_len,
1912 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001913 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001914 typval_T *basetv) // "expr" for "expr->name(arg)"
1915{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001916 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001917 char_u *s = name;
1918 int len = name_len;
1919 partial_T *partial;
1920 int ret = OK;
1921
1922 if (!evaluate)
1923 check_vars(s, len);
1924
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001925 // If "s" is the name of a variable of type VAR_FUNC
1926 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001927 s = deref_func_name(s, &len, &partial, !evaluate);
1928
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001929 // Need to make a copy, in case evaluating the arguments makes
1930 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001931 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001932 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001933 ret = FAIL;
1934 else
1935 {
1936 funcexe_T funcexe;
1937
1938 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001939 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001940 funcexe.firstline = curwin->w_cursor.lnum;
1941 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001942 funcexe.evaluate = evaluate;
1943 funcexe.partial = partial;
1944 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02001945 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001946 }
1947 vim_free(s);
1948
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001949 // If evaluate is FALSE rettv->v_type was not set in
1950 // get_func_tv, but it's needed in handle_subscript() to parse
1951 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001952 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
1953 {
1954 rettv->vval.v_string = NULL;
1955 rettv->v_type = VAR_FUNC;
1956 }
1957
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001958 // Stop the expression evaluation when immediately
1959 // aborting on error, or when an interrupt occurred or
1960 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001961 if (evaluate && aborting())
1962 {
1963 if (ret == OK)
1964 clear_tv(rettv);
1965 ret = FAIL;
1966 }
1967 return ret;
1968}
1969
1970/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02001971 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
1972 * comment) and there is a next line, return the next line (skipping blanks)
1973 * and set "getnext".
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001974 * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
1975 * "arg" must point somewhere inside a line, not at the start.
1976 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001977 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001978eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
1979{
Bram Moolenaar9d489562020-07-30 20:08:50 +02001980 char_u *p = skipwhite(arg);
1981
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001982 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001983 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001984 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001985 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaar9d489562020-07-30 20:08:50 +02001986 && (*p == NUL || (VIM_ISWHITE(p[-1]) && vim9_comment_start(p))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001987 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02001988 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001989
1990 if (evalarg->eval_cookie != NULL)
Bram Moolenaar9d489562020-07-30 20:08:50 +02001991 next = getline_peek(evalarg->eval_getline, evalarg->eval_cookie);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001992 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02001993 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001994
Bram Moolenaar9d489562020-07-30 20:08:50 +02001995 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001996 {
1997 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02001998 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001999 }
2000 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002001 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002002}
2003
2004/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002005 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002006 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002007 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002008eval_next_line(evalarg_T *evalarg)
2009{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002010 garray_T *gap = &evalarg->eval_ga;
2011 char_u *line;
2012
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002013 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002014 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2015 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002016 else
2017 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002018 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002019 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2020 {
2021 // Going to concatenate the lines after parsing.
2022 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2023 ++gap->ga_len;
2024 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002025 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002026 {
2027 vim_free(evalarg->eval_tofree);
2028 evalarg->eval_tofree = line;
2029 }
2030 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002031}
2032
Bram Moolenaare6b53242020-07-01 17:28:33 +02002033/*
2034 * Call eval_next_non_blank() and get the next line if needed.
2035 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002036 char_u *
2037skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2038{
2039 int getnext;
2040 char_u *p = skipwhite(arg);
2041
Bram Moolenaar962d7212020-07-04 14:15:00 +02002042 if (evalarg == NULL)
2043 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002044 eval_next_non_blank(p, evalarg, &getnext);
2045 if (getnext)
2046 return eval_next_line(evalarg);
2047 return p;
2048}
2049
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002050/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002051 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002052 */
2053 void
2054clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2055{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002056 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002057 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002058 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002059 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002060 if (eap != NULL)
2061 {
2062 // We may need to keep the original command line, e.g. for
2063 // ":let" it has the variable names. But we may also need the
2064 // new one, "nextcmd" points into it. Keep both.
2065 vim_free(eap->cmdline_tofree);
2066 eap->cmdline_tofree = *eap->cmdlinep;
2067 *eap->cmdlinep = evalarg->eval_tofree;
2068 }
2069 else
2070 vim_free(evalarg->eval_tofree);
2071 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002072 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002073
2074 vim_free(evalarg->eval_tofree_lambda);
2075 evalarg->eval_tofree_lambda = NULL;
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002076 }
2077}
2078
2079/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002081 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2083 */
2084
2085/*
2086 * Handle zero level expression.
2087 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002088 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002089 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002090 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 * Return OK or FAIL.
2092 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002093 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002094eval0(
2095 char_u *arg,
2096 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002097 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002098 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099{
2100 int ret;
2101 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002102 int did_emsg_before = did_emsg;
2103 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002104 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105
2106 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002107 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002108 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002109
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002110 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 {
2112 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002113 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 /*
2115 * Report the invalid expression unless the expression evaluation has
2116 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002117 * exception, or we already gave a more specific error.
2118 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002120 if (!aborting()
2121 && did_emsg == did_emsg_before
2122 && called_emsg == called_emsg_before
2123 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002124 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 ret = FAIL;
2126 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002127
2128 if (eap != NULL)
2129 eap->nextcmd = check_nextcmd(p);
2130
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 return ret;
2132}
2133
2134/*
2135 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002136 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002137 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 *
2139 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002140 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002142 * Note: "rettv.v_lock" is not set.
2143 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 * Return OK or FAIL.
2145 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002146 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002147eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002149 char_u *p;
2150 int getnext;
2151
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002152 CLEAR_POINTER(rettv);
2153
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 /*
2155 * Get the first variable.
2156 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002157 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 return FAIL;
2159
Bram Moolenaar793648f2020-06-26 21:28:25 +02002160 p = eval_next_non_blank(*arg, evalarg, &getnext);
2161 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002163 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002164 int result;
2165 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002166 evalarg_T *evalarg_used = evalarg;
2167 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002168 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002169 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002170 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002171
2172 if (evalarg == NULL)
2173 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002174 CLEAR_FIELD(local_evalarg);
2175 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002176 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002177 orig_flags = evalarg_used->eval_flags;
2178 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002179
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002180 if (getnext)
2181 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002182 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002183 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002184 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002185 {
2186 error_white_both(p, 1);
2187 clear_tv(rettv);
2188 return FAIL;
2189 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002190 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002191 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002192
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002194 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002196 int error = FALSE;
2197
Bram Moolenaar13106602020-10-04 16:06:05 +02002198 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002199 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002200 else if (vim9script)
2201 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002202 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002204 if (error || !op_falsy || !result)
2205 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002206 if (error)
2207 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 }
2209
2210 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002211 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002213 if (op_falsy)
2214 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002215 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002216 {
2217 error_white_both(p, 1);
2218 clear_tv(rettv);
2219 return FAIL;
2220 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002221 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002222 evalarg_used->eval_flags = (op_falsy ? !result : result)
2223 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2224 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002225 {
2226 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002228 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002229 if (!op_falsy || !result)
2230 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002232 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002234 /*
2235 * Check for the ":".
2236 */
2237 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2238 if (*p != ':')
2239 {
2240 emsg(_(e_missing_colon));
2241 if (evaluate && result)
2242 clear_tv(rettv);
2243 evalarg_used->eval_flags = orig_flags;
2244 return FAIL;
2245 }
2246 if (getnext)
2247 *arg = eval_next_line(evalarg_used);
2248 else
2249 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002250 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002251 {
2252 error_white_both(p, 1);
2253 clear_tv(rettv);
2254 evalarg_used->eval_flags = orig_flags;
2255 return FAIL;
2256 }
2257 *arg = p;
2258 }
2259
2260 /*
2261 * Get the third variable. Recursive!
2262 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002263 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002264 {
2265 error_white_both(p, 1);
2266 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002267 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002268 return FAIL;
2269 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002270 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2271 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002272 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002273 if (eval1(arg, &var2, evalarg_used) == FAIL)
2274 {
2275 if (evaluate && result)
2276 clear_tv(rettv);
2277 evalarg_used->eval_flags = orig_flags;
2278 return FAIL;
2279 }
2280 if (evaluate && !result)
2281 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002283
2284 if (evalarg == NULL)
2285 clear_evalarg(&local_evalarg, NULL);
2286 else
2287 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 }
2289
2290 return OK;
2291}
2292
2293/*
2294 * Handle first level expression:
2295 * expr2 || expr2 || expr2 logical OR
2296 *
2297 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002298 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 *
2300 * Return OK or FAIL.
2301 */
2302 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002303eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002305 char_u *p;
2306 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307
2308 /*
2309 * Get the first variable.
2310 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002311 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 return FAIL;
2313
2314 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002315 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002317 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002318 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002320 evalarg_T *evalarg_used = evalarg;
2321 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002322 int evaluate;
2323 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002324 long result = FALSE;
2325 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002326 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002327 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002328
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002329 if (evalarg == NULL)
2330 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002331 CLEAR_FIELD(local_evalarg);
2332 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002333 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002334 orig_flags = evalarg_used->eval_flags;
2335 evaluate = orig_flags & EVAL_EVALUATE;
2336 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002337 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002338 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002339 result = tv_get_bool_chk(rettv, &error);
2340 else if (tv_get_number_chk(rettv, &error) != 0)
2341 result = TRUE;
2342 clear_tv(rettv);
2343 if (error)
2344 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 }
2346
2347 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002348 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002350 while (p[0] == '|' && p[1] == '|')
2351 {
2352 if (getnext)
2353 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002354 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002355 {
2356 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2357 {
2358 error_white_both(p, 2);
2359 clear_tv(rettv);
2360 return FAIL;
2361 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002362 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002363 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002364
2365 /*
2366 * Get the second variable.
2367 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002368 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2369 {
2370 error_white_both(p, 2);
2371 clear_tv(rettv);
2372 return FAIL;
2373 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002374 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2375 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002376 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002377 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002378 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002379
2380 /*
2381 * Compute the result.
2382 */
2383 if (evaluate && !result)
2384 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002385 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002386 result = tv_get_bool_chk(&var2, &error);
2387 else if (tv_get_number_chk(&var2, &error) != 0)
2388 result = TRUE;
2389 clear_tv(&var2);
2390 if (error)
2391 return FAIL;
2392 }
2393 if (evaluate)
2394 {
2395 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002396 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002397 rettv->v_type = VAR_BOOL;
2398 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002399 }
2400 else
2401 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002402 rettv->v_type = VAR_NUMBER;
2403 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002404 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002405 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002406
2407 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002409
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002410 if (evalarg == NULL)
2411 clear_evalarg(&local_evalarg, NULL);
2412 else
2413 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 }
2415
2416 return OK;
2417}
2418
2419/*
2420 * Handle second level expression:
2421 * expr3 && expr3 && expr3 logical AND
2422 *
2423 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002424 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 *
2426 * Return OK or FAIL.
2427 */
2428 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002429eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002431 char_u *p;
2432 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433
2434 /*
2435 * Get the first variable.
2436 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002437 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 return FAIL;
2439
2440 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002441 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002443 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002444 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002446 evalarg_T *evalarg_used = evalarg;
2447 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002448 int orig_flags;
2449 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002450 long result = TRUE;
2451 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002452 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002453 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002454
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002455 if (evalarg == NULL)
2456 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002457 CLEAR_FIELD(local_evalarg);
2458 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002459 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002460 orig_flags = evalarg_used->eval_flags;
2461 evaluate = orig_flags & EVAL_EVALUATE;
2462 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002463 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002464 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002465 result = tv_get_bool_chk(rettv, &error);
2466 else if (tv_get_number_chk(rettv, &error) == 0)
2467 result = FALSE;
2468 clear_tv(rettv);
2469 if (error)
2470 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 }
2472
2473 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002474 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002476 while (p[0] == '&' && p[1] == '&')
2477 {
2478 if (getnext)
2479 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002480 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002481 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002482 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002483 {
2484 error_white_both(p, 2);
2485 clear_tv(rettv);
2486 return FAIL;
2487 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002488 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002489 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002490
2491 /*
2492 * Get the second variable.
2493 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002494 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2495 {
2496 error_white_both(p, 2);
2497 clear_tv(rettv);
2498 return FAIL;
2499 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002500 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2501 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002502 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002503 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002504 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002505 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002506
2507 /*
2508 * Compute the result.
2509 */
2510 if (evaluate && result)
2511 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002512 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002513 result = tv_get_bool_chk(&var2, &error);
2514 else if (tv_get_number_chk(&var2, &error) == 0)
2515 result = FALSE;
2516 clear_tv(&var2);
2517 if (error)
2518 return FAIL;
2519 }
2520 if (evaluate)
2521 {
2522 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002523 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002524 rettv->v_type = VAR_BOOL;
2525 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002526 }
2527 else
2528 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002529 rettv->v_type = VAR_NUMBER;
2530 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002531 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002532 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002533
2534 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002536
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002537 if (evalarg == NULL)
2538 clear_evalarg(&local_evalarg, NULL);
2539 else
2540 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 }
2542
2543 return OK;
2544}
2545
2546/*
2547 * Handle third level expression:
2548 * var1 == var2
2549 * var1 =~ var2
2550 * var1 != var2
2551 * var1 !~ var2
2552 * var1 > var2
2553 * var1 >= var2
2554 * var1 < var2
2555 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002556 * var1 is var2
2557 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 *
2559 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002560 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 *
2562 * Return OK or FAIL.
2563 */
2564 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002565eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002568 int getnext;
Bram Moolenaar87396072019-12-31 22:36:18 +01002569 exptype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002571 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572
2573 /*
2574 * Get the first variable.
2575 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002576 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 return FAIL;
2578
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002579 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002580 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581
2582 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002583 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002585 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002587 typval_T var2;
2588 int ic;
2589 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002590 int evaluate = evalarg == NULL
2591 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002592
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002593 if (getnext)
2594 *arg = eval_next_line(evalarg);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002595 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2596 {
2597 error_white_both(p, len);
2598 clear_tv(rettv);
2599 return FAIL;
2600 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002601
Bram Moolenaar696ba232020-07-29 21:20:41 +02002602 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2603 {
2604 semsg(_(e_invexpr2), p);
2605 clear_tv(rettv);
2606 return FAIL;
2607 }
2608
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002609 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 if (p[len] == '?')
2611 {
2612 ic = TRUE;
2613 ++len;
2614 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002615 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 else if (p[len] == '#')
2617 {
2618 ic = FALSE;
2619 ++len;
2620 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002621 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002623 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624
2625 /*
2626 * Get the second variable.
2627 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002628 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2629 {
2630 error_white_both(p, 1);
2631 clear_tv(rettv);
2632 return FAIL;
2633 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002634 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002635 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002637 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 return FAIL;
2639 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002640 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002641 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002642 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002643
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002644 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002645 {
2646 ret = FAIL;
2647 clear_tv(rettv);
2648 }
2649 else
2650 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002651 clear_tv(&var2);
2652 return ret;
2653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 }
2655
2656 return OK;
2657}
2658
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002659 void
2660eval_addblob(typval_T *tv1, typval_T *tv2)
2661{
2662 blob_T *b1 = tv1->vval.v_blob;
2663 blob_T *b2 = tv2->vval.v_blob;
2664 blob_T *b = blob_alloc();
2665 int i;
2666
2667 if (b != NULL)
2668 {
2669 for (i = 0; i < blob_len(b1); i++)
2670 ga_append(&b->bv_ga, blob_get(b1, i));
2671 for (i = 0; i < blob_len(b2); i++)
2672 ga_append(&b->bv_ga, blob_get(b2, i));
2673
2674 clear_tv(tv1);
2675 rettv_blob_set(tv1, b);
2676 }
2677}
2678
2679 int
2680eval_addlist(typval_T *tv1, typval_T *tv2)
2681{
2682 typval_T var3;
2683
2684 // concatenate Lists
2685 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2686 {
2687 clear_tv(tv1);
2688 clear_tv(tv2);
2689 return FAIL;
2690 }
2691 clear_tv(tv1);
2692 *tv1 = var3;
2693 return OK;
2694}
2695
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696/*
2697 * Handle fourth level expression:
2698 * + number addition
2699 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002700 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002701 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 *
2703 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002704 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 *
2706 * Return OK or FAIL.
2707 */
2708 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002709eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 /*
2712 * Get the first variable.
2713 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002714 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 return FAIL;
2716
2717 /*
2718 * Repeat computing, until no '+', '-' or '.' is following.
2719 */
2720 for (;;)
2721 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002722 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002723 int getnext;
2724 char_u *p;
2725 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002726 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002727 int concat;
2728 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002729 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002730
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002731 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002732 p = eval_next_non_blank(*arg, evalarg, &getnext);
2733 op = *p;
2734 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002735 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002737
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002738 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002739 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002740 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002741 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002742 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002743 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002744 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002745 {
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002746 error_white_both(p, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002747 clear_tv(rettv);
2748 return FAIL;
2749 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002750 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002751 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002752 if ((op != '+' || (rettv->v_type != VAR_LIST
2753 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002754#ifdef FEAT_FLOAT
2755 && (op == '.' || rettv->v_type != VAR_FLOAT)
2756#endif
2757 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002758 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002759 // For "list + ...", an illegal use of the first operand as
2760 // a number cannot be determined before evaluating the 2nd
2761 // operand: if this is also a list, all is ok.
2762 // For "something . ...", "something - ..." or "non-list + ...",
2763 // we know that the first operand needs to be a string or number
2764 // without evaluating the 2nd operand. So check before to avoid
2765 // side effects after an error.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002766 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002767 {
2768 clear_tv(rettv);
2769 return FAIL;
2770 }
2771 }
2772
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 /*
2774 * Get the second variable.
2775 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002776 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002777 {
2778 error_white_both(p, oplen);
2779 clear_tv(rettv);
2780 return FAIL;
2781 }
2782 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002783 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002785 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 return FAIL;
2787 }
2788
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002789 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 {
2791 /*
2792 * Compute the result.
2793 */
2794 if (op == '.')
2795 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002796 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2797 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002798 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002799
Bram Moolenaar2e086612020-08-25 22:37:48 +02002800 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002801 || var2.v_type == VAR_CHANNEL
2802 || var2.v_type == VAR_JOB))
2803 emsg(_(e_inval_string));
2804#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02002805 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002806 {
2807 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2808 var2.vval.v_float);
2809 s2 = buf2;
2810 }
2811#endif
2812 else
2813 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002814 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002815 {
2816 clear_tv(rettv);
2817 clear_tv(&var2);
2818 return FAIL;
2819 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002820 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002821 clear_tv(rettv);
2822 rettv->v_type = VAR_STRING;
2823 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002825 else if (op == '+' && rettv->v_type == VAR_BLOB
2826 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002827 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002828 else if (op == '+' && rettv->v_type == VAR_LIST
2829 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002830 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002831 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002832 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 else
2835 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002836 int error = FALSE;
2837 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002838#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002839 float_T f1 = 0, f2 = 0;
2840
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002841 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002842 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002843 f1 = rettv->vval.v_float;
2844 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002845 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002846 else
2847#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002848 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002849 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002850 if (error)
2851 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002852 // This can only happen for "list + non-list". For
2853 // "non-list + ..." or "something - ...", we returned
2854 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002855 clear_tv(rettv);
2856 return FAIL;
2857 }
2858#ifdef FEAT_FLOAT
2859 if (var2.v_type == VAR_FLOAT)
2860 f1 = n1;
2861#endif
2862 }
2863#ifdef FEAT_FLOAT
2864 if (var2.v_type == VAR_FLOAT)
2865 {
2866 f2 = var2.vval.v_float;
2867 n2 = 0;
2868 }
2869 else
2870#endif
2871 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002872 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002873 if (error)
2874 {
2875 clear_tv(rettv);
2876 clear_tv(&var2);
2877 return FAIL;
2878 }
2879#ifdef FEAT_FLOAT
2880 if (rettv->v_type == VAR_FLOAT)
2881 f2 = n2;
2882#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002883 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002884 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002885
2886#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002887 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002888 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2889 {
2890 if (op == '+')
2891 f1 = f1 + f2;
2892 else
2893 f1 = f1 - f2;
2894 rettv->v_type = VAR_FLOAT;
2895 rettv->vval.v_float = f1;
2896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002898#endif
2899 {
2900 if (op == '+')
2901 n1 = n1 + n2;
2902 else
2903 n1 = n1 - n2;
2904 rettv->v_type = VAR_NUMBER;
2905 rettv->vval.v_number = n1;
2906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002908 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 }
2910 }
2911 return OK;
2912}
2913
2914/*
2915 * Handle fifth level expression:
2916 * * number multiplication
2917 * / number division
2918 * % number modulo
2919 *
2920 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002921 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 *
2923 * Return OK or FAIL.
2924 */
2925 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002926eval6(
2927 char_u **arg,
2928 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002929 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002930 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002932#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002933 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002934#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935
2936 /*
2937 * Get the first variable.
2938 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002939 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 return FAIL;
2941
2942 /*
2943 * Repeat computing, until no '*', '/' or '%' is following.
2944 */
2945 for (;;)
2946 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002947 int evaluate;
2948 int getnext;
2949 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002950 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002951 int op;
2952 varnumber_T n1, n2;
2953#ifdef FEAT_FLOAT
2954 float_T f1, f2;
2955#endif
2956 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002957
Bram Moolenaar9d489562020-07-30 20:08:50 +02002958 p = eval_next_non_blank(*arg, evalarg, &getnext);
2959 op = *p;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002960 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002962
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002963 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002964 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002965 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002966 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002967 {
2968 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
2969 {
2970 error_white_both(p, 1);
2971 clear_tv(rettv);
2972 return FAIL;
2973 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002974 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002977#ifdef FEAT_FLOAT
2978 f1 = 0;
2979 f2 = 0;
2980#endif
2981 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002982 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002984#ifdef FEAT_FLOAT
2985 if (rettv->v_type == VAR_FLOAT)
2986 {
2987 f1 = rettv->vval.v_float;
2988 use_float = TRUE;
2989 n1 = 0;
2990 }
2991 else
2992#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002993 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002994 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002995 if (error)
2996 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 }
2998 else
2999 n1 = 0;
3000
3001 /*
3002 * Get the second variable.
3003 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003004 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3005 {
3006 error_white_both(p, 1);
3007 clear_tv(rettv);
3008 return FAIL;
3009 }
3010 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003011 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 return FAIL;
3013
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003014 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003016#ifdef FEAT_FLOAT
3017 if (var2.v_type == VAR_FLOAT)
3018 {
3019 if (!use_float)
3020 {
3021 f1 = n1;
3022 use_float = TRUE;
3023 }
3024 f2 = var2.vval.v_float;
3025 n2 = 0;
3026 }
3027 else
3028#endif
3029 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003030 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003031 clear_tv(&var2);
3032 if (error)
3033 return FAIL;
3034#ifdef FEAT_FLOAT
3035 if (use_float)
3036 f2 = n2;
3037#endif
3038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039
3040 /*
3041 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003042 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003044#ifdef FEAT_FLOAT
3045 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003047 if (op == '*')
3048 f1 = f1 * f2;
3049 else if (op == '/')
3050 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003051# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003052 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003053 if (f2 == 0.0)
3054 {
3055 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003056 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003057 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003058 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003059 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003060 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003061 }
3062 else
3063 f1 = f1 / f2;
3064# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003065 // We rely on the floating point library to handle divide
3066 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003067 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003068# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003071 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003072 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003073 return FAIL;
3074 }
3075 rettv->v_type = VAR_FLOAT;
3076 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 }
3078 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003079#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003081 if (op == '*')
3082 n1 = n1 * n2;
3083 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01003084 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01003086 n1 = num_modulus(n1, n2);
3087
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003088 rettv->v_type = VAR_NUMBER;
3089 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 }
3092 }
3093
3094 return OK;
3095}
3096
3097/*
3098 * Handle sixth level expression:
3099 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003100 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003101 * "string" string constant
3102 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 * &option-name option value
3104 * @r register contents
3105 * identifier variable value
3106 * function() function call
3107 * $VAR environment variable
3108 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003109 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003110 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003111 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003112 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 *
3114 * Also handle:
3115 * ! in front logical NOT
3116 * - in front unary minus
3117 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003118 * trailing [] subscript in String or List
3119 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003120 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 *
3122 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003123 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 *
3125 * Return OK or FAIL.
3126 */
3127 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003128eval7(
3129 char_u **arg,
3130 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003131 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003132 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003134 int evaluate = evalarg != NULL
3135 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 int len;
3137 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 char_u *start_leader, *end_leader;
3139 int ret = OK;
3140 char_u *alias;
3141
3142 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003143 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003144 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003146 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147
3148 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003149 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 */
3151 start_leader = *arg;
3152 while (**arg == '!' || **arg == '-' || **arg == '+')
3153 *arg = skipwhite(*arg + 1);
3154 end_leader = *arg;
3155
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003156 if (**arg == '.' && (!isdigit(*(*arg + 1))
3157#ifdef FEAT_FLOAT
3158 || current_sctx.sc_version < 2
3159#endif
3160 ))
3161 {
3162 semsg(_(e_invexpr2), *arg);
3163 ++*arg;
3164 return FAIL;
3165 }
3166
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 switch (**arg)
3168 {
3169 /*
3170 * Number constant.
3171 */
3172 case '0':
3173 case '1':
3174 case '2':
3175 case '3':
3176 case '4':
3177 case '5':
3178 case '6':
3179 case '7':
3180 case '8':
3181 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003182 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003183
3184 // Apply prefixed "-" and "+" now. Matters especially when
3185 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003186 if (ret == OK && evaluate && end_leader > start_leader
3187 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003188 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 break;
3190
3191 /*
3192 * String constant: "string".
3193 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003194 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 break;
3196
3197 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003198 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003200 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003201 break;
3202
3203 /*
3204 * List: [expr, expr]
3205 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003206 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 break;
3208
3209 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003210 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003211 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003212 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003213 {
3214 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003215 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003216 }
3217 else
3218 ret = NOTDONE;
3219 break;
3220
3221 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003222 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003223 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003224 */
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003225 case '{': ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003226 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003227 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003228 break;
3229
3230 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003231 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003233 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 break;
3235
3236 /*
3237 * Environment variable: $VAR.
3238 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003239 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 break;
3241
3242 /*
3243 * Register contents: @r.
3244 */
3245 case '@': ++*arg;
3246 if (evaluate)
3247 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003248 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02003249 rettv->vval.v_string = get_reg_contents(**arg,
3250 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 }
3252 if (**arg != NUL)
3253 ++*arg;
3254 break;
3255
3256 /*
3257 * nested expression: (expression).
3258 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003259 case '(': {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003260 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003261 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003262
Bram Moolenaar9215f012020-06-27 21:18:00 +02003263 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003264 if (**arg == ')')
3265 ++*arg;
3266 else if (ret == OK)
3267 {
3268 emsg(_(e_missing_close));
3269 clear_tv(rettv);
3270 ret = FAIL;
3271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 }
3273 break;
3274
Bram Moolenaar8c711452005-01-14 21:53:12 +00003275 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 break;
3277 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003278
3279 if (ret == NOTDONE)
3280 {
3281 /*
3282 * Must be a variable or function name.
3283 * Can also be a curly-braces kind of name: {expr}.
3284 */
3285 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003286 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003287 if (alias != NULL)
3288 s = alias;
3289
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003290 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003291 ret = FAIL;
3292 else
3293 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003294 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3295
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003296 if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
3297 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003298 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003299 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003300 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003301 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003302 else if (flags & EVAL_CONSTANT)
3303 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003304 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003305 {
3306 // get the value of "true", "false" or a variable
3307 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3308 {
3309 rettv->v_type = VAR_BOOL;
3310 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003311 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003312 }
3313 else if (len == 5 && in_vim9script()
3314 && STRNCMP(s, "false", 4) == 0)
3315 {
3316 rettv->v_type = VAR_BOOL;
3317 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003318 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003319 }
3320 else
3321 ret = eval_variable(s, len, rettv, NULL, TRUE, FALSE);
3322 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003323 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003324 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003325 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003326 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003327 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003328 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003329 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003330 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003331 }
3332
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003333 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3334 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003335 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003336 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337
3338 /*
3339 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3340 */
3341 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003342 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003343 return ret;
3344}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003345
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003346/*
3347 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003348 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003349 * Adjusts "end_leaderp" until it is at "start_leader".
3350 */
3351 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003352eval7_leader(
3353 typval_T *rettv,
3354 int numeric_only,
3355 char_u *start_leader,
3356 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003357{
3358 char_u *end_leader = *end_leaderp;
3359 int ret = OK;
3360 int error = FALSE;
3361 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003362 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003363#ifdef FEAT_FLOAT
3364 float_T f = 0.0;
3365
3366 if (rettv->v_type == VAR_FLOAT)
3367 f = rettv->vval.v_float;
3368 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003369#endif
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003370 if (in_vim9script() && end_leader[-1] == '!')
3371 val = tv2bool(rettv);
3372 else
3373 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003374 if (error)
3375 {
3376 clear_tv(rettv);
3377 ret = FAIL;
3378 }
3379 else
3380 {
3381 while (end_leader > start_leader)
3382 {
3383 --end_leader;
3384 if (*end_leader == '!')
3385 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003386 if (numeric_only)
3387 {
3388 ++end_leader;
3389 break;
3390 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003391#ifdef FEAT_FLOAT
3392 if (rettv->v_type == VAR_FLOAT)
3393 f = !f;
3394 else
3395#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003396 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003397 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003398 type = VAR_BOOL;
3399 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003400 }
3401 else if (*end_leader == '-')
3402 {
3403#ifdef FEAT_FLOAT
3404 if (rettv->v_type == VAR_FLOAT)
3405 f = -f;
3406 else
3407#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003408 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003409 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003410 type = VAR_NUMBER;
3411 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003412 }
3413 }
3414#ifdef FEAT_FLOAT
3415 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003417 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003418 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003420 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003421#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003422 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003423 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003424 if (in_vim9script())
3425 rettv->v_type = type;
3426 else
3427 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003428 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003431 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 return ret;
3433}
3434
3435/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003436 * Call the function referred to in "rettv".
3437 */
3438 static int
3439call_func_rettv(
3440 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003441 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003442 typval_T *rettv,
3443 int evaluate,
3444 dict_T *selfdict,
3445 typval_T *basetv)
3446{
3447 partial_T *pt = NULL;
3448 funcexe_T funcexe;
3449 typval_T functv;
3450 char_u *s;
3451 int ret;
3452
3453 // need to copy the funcref so that we can clear rettv
3454 if (evaluate)
3455 {
3456 functv = *rettv;
3457 rettv->v_type = VAR_UNKNOWN;
3458
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003459 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003460 if (functv.v_type == VAR_PARTIAL)
3461 {
3462 pt = functv.vval.v_partial;
3463 s = partial_name(pt);
3464 }
3465 else
3466 s = functv.vval.v_string;
3467 }
3468 else
3469 s = (char_u *)"";
3470
Bram Moolenaara80faa82020-04-12 19:37:17 +02003471 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003472 funcexe.firstline = curwin->w_cursor.lnum;
3473 funcexe.lastline = curwin->w_cursor.lnum;
3474 funcexe.evaluate = evaluate;
3475 funcexe.partial = pt;
3476 funcexe.selfdict = selfdict;
3477 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003478 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003479
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003480 // Clear the funcref afterwards, so that deleting it while
3481 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003482 if (evaluate)
3483 clear_tv(&functv);
3484
3485 return ret;
3486}
3487
3488/*
3489 * Evaluate "->method()".
3490 * "*arg" points to the '-'.
3491 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3492 */
3493 static int
3494eval_lambda(
3495 char_u **arg,
3496 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003497 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003498 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003499{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003500 int evaluate = evalarg != NULL
3501 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003502 typval_T base = *rettv;
3503 int ret;
3504
3505 // Skip over the ->.
3506 *arg += 2;
3507 rettv->v_type = VAR_UNKNOWN;
3508
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003509 ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003510 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003511 return FAIL;
3512 else if (**arg != '(')
3513 {
3514 if (verbose)
3515 {
3516 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003517 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003518 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003519 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003520 }
3521 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003522 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003523 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003524 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003525 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003526
3527 // Clear the funcref afterwards, so that deleting it while
3528 // evaluating the arguments is possible (see test55).
3529 if (evaluate)
3530 clear_tv(&base);
3531
3532 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003533}
3534
3535/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003536 * Evaluate "->method()".
3537 * "*arg" points to the '-'.
3538 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3539 */
3540 static int
3541eval_method(
3542 char_u **arg,
3543 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003544 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003545 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003546{
3547 char_u *name;
3548 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003549 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003550 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003551 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003552 int evaluate = evalarg != NULL
3553 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003554
3555 // Skip over the ->.
3556 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003557 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003558
Bram Moolenaarac92e252019-08-03 21:58:38 +02003559 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003560 len = get_name_len(arg, &alias, evaluate, TRUE);
3561 if (alias != NULL)
3562 name = alias;
3563
3564 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003565 {
3566 if (verbose)
3567 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003568 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003569 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003570 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003571 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003572 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003573 if (**arg != '(')
3574 {
3575 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003576 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003577 ret = FAIL;
3578 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003579 else if (VIM_ISWHITE((*arg)[-1]))
3580 {
3581 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003582 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003583 ret = FAIL;
3584 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003585 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003586 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003587 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003588 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003589
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003590 // Clear the funcref afterwards, so that deleting it while
3591 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003592 if (evaluate)
3593 clear_tv(&base);
3594
Bram Moolenaarac92e252019-08-03 21:58:38 +02003595 return ret;
3596}
3597
3598/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003599 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3600 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003601 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3602 */
3603 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003604eval_index(
3605 char_u **arg,
3606 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003607 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003608 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003609{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003610 int evaluate = evalarg != NULL
3611 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003612 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003613 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003614 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003615 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003616 int keylen = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003617
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003618 if (check_can_index(rettv, evaluate, verbose) == FAIL)
3619 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003620
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003621 init_tv(&var1);
3622 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003623 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003624 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003625 /*
3626 * dict.name
3627 */
3628 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003629 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003630 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003631 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003632 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02003633 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003634 }
3635 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003636 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003637 /*
3638 * something[idx]
3639 *
3640 * Get the (first) variable from inside the [].
3641 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003642 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003643 if (**arg == ':')
3644 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003645 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003646 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003647 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003648 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003649 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003650 clear_tv(&var1);
3651 return FAIL;
3652 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003653
3654 /*
3655 * Get the second variable from inside the [:].
3656 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003657 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003658 if (**arg == ':')
3659 {
3660 range = TRUE;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003661 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003662 if (**arg == ']')
3663 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003664 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003665 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003666 if (!empty1)
3667 clear_tv(&var1);
3668 return FAIL;
3669 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003670 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003671 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003672 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003673 if (!empty1)
3674 clear_tv(&var1);
3675 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003676 return FAIL;
3677 }
3678 }
3679
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003680 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003681 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003682 if (**arg != ']')
3683 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003684 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003685 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003686 clear_tv(&var1);
3687 if (range)
3688 clear_tv(&var2);
3689 return FAIL;
3690 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02003691 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003692 }
3693
3694 if (evaluate)
3695 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003696 int res = eval_index_inner(rettv, range,
3697 empty1 ? NULL : &var1, empty2 ? NULL : &var2,
3698 key, keylen, verbose);
3699 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003700 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003701 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003702 clear_tv(&var2);
3703 return res;
3704 }
3705 return OK;
3706}
3707
3708/*
3709 * Check if "rettv" can have an [index] or [sli:ce]
3710 */
3711 int
3712check_can_index(typval_T *rettv, int evaluate, int verbose)
3713{
3714 switch (rettv->v_type)
3715 {
3716 case VAR_FUNC:
3717 case VAR_PARTIAL:
3718 if (verbose)
3719 emsg(_("E695: Cannot index a Funcref"));
3720 return FAIL;
3721 case VAR_FLOAT:
3722#ifdef FEAT_FLOAT
3723 if (verbose)
3724 emsg(_(e_float_as_string));
3725 return FAIL;
3726#endif
3727 case VAR_BOOL:
3728 case VAR_SPECIAL:
3729 case VAR_JOB:
3730 case VAR_CHANNEL:
3731 if (verbose)
3732 emsg(_(e_cannot_index_special_variable));
3733 return FAIL;
3734 case VAR_UNKNOWN:
3735 case VAR_ANY:
3736 case VAR_VOID:
3737 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003738 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003739 emsg(_(e_cannot_index_special_variable));
3740 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003741 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003742 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003743
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003744 case VAR_STRING:
3745 case VAR_LIST:
3746 case VAR_DICT:
3747 case VAR_BLOB:
3748 break;
3749 case VAR_NUMBER:
3750 if (in_vim9script())
3751 emsg(_(e_cannot_index_number));
3752 break;
3753 }
3754 return OK;
3755}
3756
3757/*
3758 * Apply index or range to "rettv".
3759 * "var1" is the first index, NULL for [:expr].
3760 * "var2" is the second index, NULL for [expr] and [expr: ]
3761 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
3762 */
3763 int
3764eval_index_inner(
3765 typval_T *rettv,
3766 int is_range,
3767 typval_T *var1,
3768 typval_T *var2,
3769 char_u *key,
3770 int keylen,
3771 int verbose)
3772{
3773 long n1, n2 = 0;
3774 long len;
3775
3776 n1 = 0;
3777 if (var1 != NULL && rettv->v_type != VAR_DICT)
3778 n1 = tv_get_number(var1);
3779
3780 if (is_range)
3781 {
3782 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003783 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003784 if (verbose)
3785 emsg(_(e_cannot_slice_dictionary));
3786 return FAIL;
3787 }
3788 if (var2 == NULL)
3789 n2 = -1;
3790 else
3791 n2 = tv_get_number(var2);
3792 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01003793
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003794 switch (rettv->v_type)
3795 {
3796 case VAR_UNKNOWN:
3797 case VAR_ANY:
3798 case VAR_VOID:
3799 case VAR_FUNC:
3800 case VAR_PARTIAL:
3801 case VAR_FLOAT:
3802 case VAR_BOOL:
3803 case VAR_SPECIAL:
3804 case VAR_JOB:
3805 case VAR_CHANNEL:
3806 break; // not evaluating, skipping over subscript
3807
3808 case VAR_NUMBER:
3809 case VAR_STRING:
3810 {
3811 char_u *s = tv_get_string(rettv);
3812
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003813 len = (long)STRLEN(s);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003814 if (in_vim9script())
3815 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003816 if (is_range)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003817 s = string_slice(s, n1, n2);
3818 else
3819 s = char_from_string(s, n1);
3820 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003821 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003822 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003823 // The resulting variable is a substring. If the indexes
3824 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003825 if (n1 < 0)
3826 {
3827 n1 = len + n1;
3828 if (n1 < 0)
3829 n1 = 0;
3830 }
3831 if (n2 < 0)
3832 n2 = len + n2;
3833 else if (n2 >= len)
3834 n2 = len;
3835 if (n1 >= len || n2 < 0 || n1 > n2)
3836 s = NULL;
3837 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02003838 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003839 }
3840 else
3841 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003842 // The resulting variable is a string of a single
3843 // character. If the index is too big or negative the
3844 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003845 if (n1 >= len || n1 < 0)
3846 s = NULL;
3847 else
3848 s = vim_strnsave(s + n1, 1);
3849 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003850 clear_tv(rettv);
3851 rettv->v_type = VAR_STRING;
3852 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003853 }
3854 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003855
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003856 case VAR_BLOB:
3857 len = blob_len(rettv->vval.v_blob);
3858 if (is_range)
3859 {
3860 // The resulting variable is a sub-blob. If the indexes
3861 // are out of range the result is empty.
3862 if (n1 < 0)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003863 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003864 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003865 if (n1 < 0)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003866 n1 = 0;
3867 }
3868 if (n2 < 0)
3869 n2 = len + n2;
3870 else if (n2 >= len)
3871 n2 = len - 1;
3872 if (n1 >= len || n2 < 0 || n1 > n2)
3873 {
3874 clear_tv(rettv);
3875 rettv->v_type = VAR_BLOB;
3876 rettv->vval.v_blob = NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003877 }
3878 else
3879 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003880 blob_T *blob = blob_alloc();
3881 long i;
3882
3883 if (blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003884 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003885 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003886 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003887 blob_free(blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003888 return FAIL;
3889 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003890 blob->bv_ga.ga_len = n2 - n1 + 1;
3891 for (i = n1; i <= n2; i++)
3892 blob_set(blob, i - n1,
3893 blob_get(rettv->vval.v_blob, i));
3894
3895 clear_tv(rettv);
3896 rettv_blob_set(rettv, blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003897 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003898 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003899 }
3900 else
3901 {
3902 // The resulting variable is a byte value.
3903 // If the index is too big or negative that is an error.
3904 if (n1 < 0)
3905 n1 = len + n1;
3906 if (n1 < len && n1 >= 0)
3907 {
3908 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003909
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003910 clear_tv(rettv);
3911 rettv->v_type = VAR_NUMBER;
3912 rettv->vval.v_number = v;
3913 }
3914 else
3915 semsg(_(e_blobidx), n1);
3916 }
3917 break;
3918
3919 case VAR_LIST:
3920 if (var1 == NULL)
3921 n1 = 0;
3922 if (var2 == NULL)
3923 n2 = -1;
3924 if (list_slice_or_index(rettv->vval.v_list,
3925 is_range, n1, n2, rettv, verbose) == FAIL)
3926 return FAIL;
3927 break;
3928
3929 case VAR_DICT:
3930 {
3931 dictitem_T *item;
3932 typval_T tmp;
3933
3934 if (key == NULL)
3935 {
3936 key = tv_get_string_chk(var1);
3937 if (key == NULL)
3938 return FAIL;
3939 }
3940
3941 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
3942
3943 if (item == NULL && verbose)
3944 semsg(_(e_dictkey), key);
3945 if (item == NULL)
3946 return FAIL;
3947
3948 copy_tv(&item->di_tv, &tmp);
3949 clear_tv(rettv);
3950 *rettv = tmp;
3951 }
3952 break;
3953 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003954 return OK;
3955}
3956
3957/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02003958 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003959 */
3960 char_u *
3961partial_name(partial_T *pt)
3962{
3963 if (pt->pt_name != NULL)
3964 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02003965 if (pt->pt_func != NULL)
3966 return pt->pt_func->uf_name;
3967 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003968}
3969
Bram Moolenaarddecc252016-04-06 22:59:37 +02003970 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003971partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003972{
3973 int i;
3974
3975 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003976 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003977 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003978 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003979 if (pt->pt_name != NULL)
3980 {
3981 func_unref(pt->pt_name);
3982 vim_free(pt->pt_name);
3983 }
3984 else
3985 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003986
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02003987 // Decrease the reference count for the context of a closure. If down
3988 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003989 if (pt->pt_funcstack != NULL)
3990 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02003991 --pt->pt_funcstack->fs_refcount;
3992 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003993 }
3994
Bram Moolenaarddecc252016-04-06 22:59:37 +02003995 vim_free(pt);
3996}
3997
3998/*
3999 * Unreference a closure: decrement the reference count and free it when it
4000 * becomes zero.
4001 */
4002 void
4003partial_unref(partial_T *pt)
4004{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004005 if (pt != NULL)
4006 {
4007 if (--pt->pt_refcount <= 0)
4008 partial_free(pt);
4009
4010 // If the reference count goes down to one, the funcstack may be the
4011 // only reference and can be freed if no other partials reference it.
4012 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4013 funcstack_check_refcount(pt->pt_funcstack);
4014 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004015}
4016
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004017/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004018 * Return the next (unique) copy ID.
4019 * Used for serializing nested structures.
4020 */
4021 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004022get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004023{
4024 current_copyID += COPYID_INC;
4025 return current_copyID;
4026}
4027
4028/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004029 * Garbage collection for lists and dictionaries.
4030 *
4031 * We use reference counts to be able to free most items right away when they
4032 * are no longer used. But for composite items it's possible that it becomes
4033 * unused while the reference count is > 0: When there is a recursive
4034 * reference. Example:
4035 * :let l = [1, 2, 3]
4036 * :let d = {9: l}
4037 * :let l[1] = d
4038 *
4039 * Since this is quite unusual we handle this with garbage collection: every
4040 * once in a while find out which lists and dicts are not referenced from any
4041 * variable.
4042 *
4043 * Here is a good reference text about garbage collection (refers to Python
4044 * but it applies to all reference-counting mechanisms):
4045 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004046 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004047
4048/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004049 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004050 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004051 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004052 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004053 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004054garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004055{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004056 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004057 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004058 buf_T *buf;
4059 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004060 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004061 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004062
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004063 if (!testing)
4064 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004065 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004066 want_garbage_collect = FALSE;
4067 may_garbage_collect = FALSE;
4068 garbage_collect_at_exit = FALSE;
4069 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004070
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004071 // The execution stack can grow big, limit the size.
4072 if (exestack.ga_maxlen - exestack.ga_len > 500)
4073 {
4074 size_t new_len;
4075 char_u *pp;
4076 int n;
4077
4078 // Keep 150% of the current size, with a minimum of the growth size.
4079 n = exestack.ga_len / 2;
4080 if (n < exestack.ga_growsize)
4081 n = exestack.ga_growsize;
4082
4083 // Don't make it bigger though.
4084 if (exestack.ga_len + n < exestack.ga_maxlen)
4085 {
4086 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4087 pp = vim_realloc(exestack.ga_data, new_len);
4088 if (pp == NULL)
4089 return FAIL;
4090 exestack.ga_maxlen = exestack.ga_len + n;
4091 exestack.ga_data = pp;
4092 }
4093 }
4094
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004095 // We advance by two because we add one for items referenced through
4096 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004097 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004098
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004099 /*
4100 * 1. Go through all accessible variables and mark all lists and dicts
4101 * with copyID.
4102 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004103
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004104 // Don't free variables in the previous_funccal list unless they are only
4105 // referenced through previous_funccal. This must be first, because if
4106 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004107 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004108
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004109 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004110 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004111
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004112 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004113 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004114 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4115 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004116
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004117 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004118 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004119 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4120 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004121 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004122 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4123 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004124#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004125 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004126 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4127 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004128 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004129 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004130 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4131 NULL, NULL);
4132#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004133
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004134 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004135 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004136 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4137 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004138 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004139 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004140
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004141 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004142 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004143
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004144 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004145 abort = abort || set_ref_in_functions(copyID);
4146
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004147 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004148 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004149
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004150 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004151 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004152
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004153 // callbacks in buffers
4154 abort = abort || set_ref_in_buffers(copyID);
4155
Bram Moolenaar1dced572012-04-05 16:54:08 +02004156#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004157 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004158#endif
4159
Bram Moolenaardb913952012-06-29 12:54:53 +02004160#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004161 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004162#endif
4163
4164#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004165 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004166#endif
4167
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004168#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004169 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004170 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004171#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004172#ifdef FEAT_NETBEANS_INTG
4173 abort = abort || set_ref_in_nb_channel(copyID);
4174#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004175
Bram Moolenaare3188e22016-05-31 21:13:04 +02004176#ifdef FEAT_TIMERS
4177 abort = abort || set_ref_in_timer(copyID);
4178#endif
4179
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004180#ifdef FEAT_QUICKFIX
4181 abort = abort || set_ref_in_quickfix(copyID);
4182#endif
4183
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004184#ifdef FEAT_TERMINAL
4185 abort = abort || set_ref_in_term(copyID);
4186#endif
4187
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004188#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004189 abort = abort || set_ref_in_popups(copyID);
4190#endif
4191
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004192 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004193 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004194 /*
4195 * 2. Free lists and dictionaries that are not referenced.
4196 */
4197 did_free = free_unref_items(copyID);
4198
4199 /*
4200 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004201 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004202 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004203 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004204 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004205 else if (p_verbose > 0)
4206 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004207 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004208 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004209
4210 return did_free;
4211}
4212
4213/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004214 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004215 */
4216 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004217free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004218{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004219 int did_free = FALSE;
4220
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004221 // Let all "free" functions know that we are here. This means no
4222 // dictionaries, lists, channels or jobs are to be freed, because we will
4223 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004224 in_free_unref_items = TRUE;
4225
4226 /*
4227 * PASS 1: free the contents of the items. We don't free the items
4228 * themselves yet, so that it is possible to decrement refcount counters
4229 */
4230
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004231 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004232 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004233
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004234 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004235 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004236
4237#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004238 // Go through the list of jobs and free items without the copyID. This
4239 // must happen before doing channels, because jobs refer to channels, but
4240 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004241 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4242
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004243 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004244 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4245#endif
4246
4247 /*
4248 * PASS 2: free the items themselves.
4249 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004250 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004251 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004252
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004253#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004254 // Go through the list of jobs and free items without the copyID. This
4255 // must happen before doing channels, because jobs refer to channels, but
4256 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004257 free_unused_jobs(copyID, COPYID_MASK);
4258
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004259 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004260 free_unused_channels(copyID, COPYID_MASK);
4261#endif
4262
4263 in_free_unref_items = FALSE;
4264
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004265 return did_free;
4266}
4267
4268/*
4269 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004270 * "list_stack" is used to add lists to be marked. Can be NULL.
4271 *
4272 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004273 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004274 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004275set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004276{
4277 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004278 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004279 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004280 hashtab_T *cur_ht;
4281 ht_stack_T *ht_stack = NULL;
4282 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004283
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004284 cur_ht = ht;
4285 for (;;)
4286 {
4287 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004288 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004289 // Mark each item in the hashtab. If the item contains a hashtab
4290 // it is added to ht_stack, if it contains a list it is added to
4291 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004292 todo = (int)cur_ht->ht_used;
4293 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4294 if (!HASHITEM_EMPTY(hi))
4295 {
4296 --todo;
4297 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4298 &ht_stack, list_stack);
4299 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004300 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004301
4302 if (ht_stack == NULL)
4303 break;
4304
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004305 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004306 cur_ht = ht_stack->ht;
4307 tempitem = ht_stack;
4308 ht_stack = ht_stack->prev;
4309 free(tempitem);
4310 }
4311
4312 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004313}
4314
4315/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004316 * Mark a dict and its items with "copyID".
4317 * Returns TRUE if setting references failed somehow.
4318 */
4319 int
4320set_ref_in_dict(dict_T *d, int copyID)
4321{
4322 if (d != NULL && d->dv_copyID != copyID)
4323 {
4324 d->dv_copyID = copyID;
4325 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4326 }
4327 return FALSE;
4328}
4329
4330/*
4331 * Mark a list and its items with "copyID".
4332 * Returns TRUE if setting references failed somehow.
4333 */
4334 int
4335set_ref_in_list(list_T *ll, int copyID)
4336{
4337 if (ll != NULL && ll->lv_copyID != copyID)
4338 {
4339 ll->lv_copyID = copyID;
4340 return set_ref_in_list_items(ll, copyID, NULL);
4341 }
4342 return FALSE;
4343}
4344
4345/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004346 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004347 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4348 *
4349 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004350 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004351 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004352set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004353{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004354 listitem_T *li;
4355 int abort = FALSE;
4356 list_T *cur_l;
4357 list_stack_T *list_stack = NULL;
4358 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004359
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004360 cur_l = l;
4361 for (;;)
4362 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004363 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004364 // Mark each item in the list. If the item contains a hashtab
4365 // it is added to ht_stack, if it contains a list it is added to
4366 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004367 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4368 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4369 ht_stack, &list_stack);
4370 if (list_stack == NULL)
4371 break;
4372
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004373 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004374 cur_l = list_stack->list;
4375 tempitem = list_stack;
4376 list_stack = list_stack->prev;
4377 free(tempitem);
4378 }
4379
4380 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004381}
4382
4383/*
4384 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004385 * "list_stack" is used to add lists to be marked. Can be NULL.
4386 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4387 *
4388 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004389 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004390 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004391set_ref_in_item(
4392 typval_T *tv,
4393 int copyID,
4394 ht_stack_T **ht_stack,
4395 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004396{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004397 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004398
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004399 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004400 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004401 dict_T *dd = tv->vval.v_dict;
4402
Bram Moolenaara03f2332016-02-06 18:09:59 +01004403 if (dd != NULL && dd->dv_copyID != copyID)
4404 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004405 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004406 dd->dv_copyID = copyID;
4407 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004408 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004409 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4410 }
4411 else
4412 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004413 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4414
Bram Moolenaara03f2332016-02-06 18:09:59 +01004415 if (newitem == NULL)
4416 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004417 else
4418 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004419 newitem->ht = &dd->dv_hashtab;
4420 newitem->prev = *ht_stack;
4421 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004422 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004423 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004424 }
4425 }
4426 else if (tv->v_type == VAR_LIST)
4427 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004428 list_T *ll = tv->vval.v_list;
4429
Bram Moolenaara03f2332016-02-06 18:09:59 +01004430 if (ll != NULL && ll->lv_copyID != copyID)
4431 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004432 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004433 ll->lv_copyID = copyID;
4434 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004435 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004436 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004437 }
4438 else
4439 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004440 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4441
Bram Moolenaara03f2332016-02-06 18:09:59 +01004442 if (newitem == NULL)
4443 abort = TRUE;
4444 else
4445 {
4446 newitem->list = ll;
4447 newitem->prev = *list_stack;
4448 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004449 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004450 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004451 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004452 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004453 else if (tv->v_type == VAR_FUNC)
4454 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004455 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004456 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004457 else if (tv->v_type == VAR_PARTIAL)
4458 {
4459 partial_T *pt = tv->vval.v_partial;
4460 int i;
4461
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004462 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004463 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004464 // Didn't see this partial yet.
4465 pt->pt_copyID = copyID;
4466
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004467 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004468
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004469 if (pt->pt_dict != NULL)
4470 {
4471 typval_T dtv;
4472
4473 dtv.v_type = VAR_DICT;
4474 dtv.vval.v_dict = pt->pt_dict;
4475 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4476 }
4477
4478 for (i = 0; i < pt->pt_argc; ++i)
4479 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4480 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004481 if (pt->pt_funcstack != NULL)
4482 {
4483 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4484
4485 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4486 abort = abort || set_ref_in_item(stack + i, copyID,
4487 ht_stack, list_stack);
4488 }
4489
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004490 }
4491 }
4492#ifdef FEAT_JOB_CHANNEL
4493 else if (tv->v_type == VAR_JOB)
4494 {
4495 job_T *job = tv->vval.v_job;
4496 typval_T dtv;
4497
4498 if (job != NULL && job->jv_copyID != copyID)
4499 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004500 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004501 if (job->jv_channel != NULL)
4502 {
4503 dtv.v_type = VAR_CHANNEL;
4504 dtv.vval.v_channel = job->jv_channel;
4505 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4506 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004507 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004508 {
4509 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004510 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004511 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4512 }
4513 }
4514 }
4515 else if (tv->v_type == VAR_CHANNEL)
4516 {
4517 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004518 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004519 typval_T dtv;
4520 jsonq_T *jq;
4521 cbq_T *cq;
4522
4523 if (ch != NULL && ch->ch_copyID != copyID)
4524 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004525 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004526 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004527 {
4528 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4529 jq = jq->jq_next)
4530 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4531 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4532 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004533 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004534 {
4535 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004536 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004537 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4538 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004539 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004540 {
4541 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004542 dtv.vval.v_partial =
4543 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004544 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4545 }
4546 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004547 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004548 {
4549 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004550 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004551 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4552 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004553 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004554 {
4555 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004556 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004557 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4558 }
4559 }
4560 }
4561#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004562 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004563}
4564
Bram Moolenaar8c711452005-01-14 21:53:12 +00004565/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004566 * Return a string with the string representation of a variable.
4567 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004568 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004569 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004570 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4571 * stings as "string()", otherwise does not put quotes around strings, as
4572 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004573 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4574 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004575 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004576 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004577 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004578echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004579 typval_T *tv,
4580 char_u **tofree,
4581 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004582 int copyID,
4583 int echo_style,
4584 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004585 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004586{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004587 static int recurse = 0;
4588 char_u *r = NULL;
4589
Bram Moolenaar33570922005-01-25 22:26:29 +00004590 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004591 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004592 if (!did_echo_string_emsg)
4593 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004594 // Only give this message once for a recursive call to avoid
4595 // flooding the user with errors. And stop iterating over lists
4596 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004597 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004598 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004599 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004600 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004601 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004602 }
4603 ++recurse;
4604
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004605 switch (tv->v_type)
4606 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004607 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004608 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004609 {
4610 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004611 r = tv->vval.v_string;
4612 if (r == NULL)
4613 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004614 }
4615 else
4616 {
4617 *tofree = string_quote(tv->vval.v_string, FALSE);
4618 r = *tofree;
4619 }
4620 break;
4621
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004622 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004623 if (echo_style)
4624 {
4625 *tofree = NULL;
4626 r = tv->vval.v_string;
4627 }
4628 else
4629 {
4630 *tofree = string_quote(tv->vval.v_string, TRUE);
4631 r = *tofree;
4632 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004633 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004634
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004635 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004636 {
4637 partial_T *pt = tv->vval.v_partial;
4638 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004639 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004640 garray_T ga;
4641 int i;
4642 char_u *tf;
4643
4644 ga_init2(&ga, 1, 100);
4645 ga_concat(&ga, (char_u *)"function(");
4646 if (fname != NULL)
4647 {
4648 ga_concat(&ga, fname);
4649 vim_free(fname);
4650 }
4651 if (pt != NULL && pt->pt_argc > 0)
4652 {
4653 ga_concat(&ga, (char_u *)", [");
4654 for (i = 0; i < pt->pt_argc; ++i)
4655 {
4656 if (i > 0)
4657 ga_concat(&ga, (char_u *)", ");
4658 ga_concat(&ga,
4659 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4660 vim_free(tf);
4661 }
4662 ga_concat(&ga, (char_u *)"]");
4663 }
4664 if (pt != NULL && pt->pt_dict != NULL)
4665 {
4666 typval_T dtv;
4667
4668 ga_concat(&ga, (char_u *)", ");
4669 dtv.v_type = VAR_DICT;
4670 dtv.vval.v_dict = pt->pt_dict;
4671 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4672 vim_free(tf);
4673 }
4674 ga_concat(&ga, (char_u *)")");
4675
4676 *tofree = ga.ga_data;
4677 r = *tofree;
4678 break;
4679 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004680
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004681 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004682 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004683 break;
4684
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004685 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004686 if (tv->vval.v_list == NULL)
4687 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004688 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004689 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004690 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004691 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004692 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4693 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004694 {
4695 *tofree = NULL;
4696 r = (char_u *)"[...]";
4697 }
4698 else
4699 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004700 int old_copyID = tv->vval.v_list->lv_copyID;
4701
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004702 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004703 *tofree = list2string(tv, copyID, restore_copyID);
4704 if (restore_copyID)
4705 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004706 r = *tofree;
4707 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004708 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004709
Bram Moolenaar8c711452005-01-14 21:53:12 +00004710 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004711 if (tv->vval.v_dict == NULL)
4712 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004713 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004714 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004715 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004716 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004717 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4718 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004719 {
4720 *tofree = NULL;
4721 r = (char_u *)"{...}";
4722 }
4723 else
4724 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004725 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004726
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004727 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004728 *tofree = dict2string(tv, copyID, restore_copyID);
4729 if (restore_copyID)
4730 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004731 r = *tofree;
4732 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004733 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004734
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004735 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004736 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004737 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004738 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004739 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004740 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004741 break;
4742
Bram Moolenaar835dc632016-02-07 14:27:38 +01004743 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004744 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004745 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004746 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004747 if (composite_val)
4748 {
4749 *tofree = string_quote(r, FALSE);
4750 r = *tofree;
4751 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004752 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004753
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004754 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004755#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004756 *tofree = NULL;
4757 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4758 r = numbuf;
4759 break;
4760#endif
4761
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004762 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004763 case VAR_SPECIAL:
4764 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004765 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004766 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004767 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004768
Bram Moolenaar8502c702014-06-17 12:51:16 +02004769 if (--recurse == 0)
4770 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004771 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004772}
4773
4774/*
4775 * Return a string with the string representation of a variable.
4776 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4777 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004778 * Does not put quotes around strings, as ":echo" displays values.
4779 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4780 * May return NULL.
4781 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004782 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004783echo_string(
4784 typval_T *tv,
4785 char_u **tofree,
4786 char_u *numbuf,
4787 int copyID)
4788{
4789 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4790}
4791
4792/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004793 * Return string "str" in ' quotes, doubling ' characters.
4794 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004795 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004796 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004797 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004798string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004799{
Bram Moolenaar33570922005-01-25 22:26:29 +00004800 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004801 char_u *p, *r, *s;
4802
Bram Moolenaar33570922005-01-25 22:26:29 +00004803 len = (function ? 13 : 3);
4804 if (str != NULL)
4805 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004806 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004807 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004808 if (*p == '\'')
4809 ++len;
4810 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004811 s = r = alloc(len);
4812 if (r != NULL)
4813 {
4814 if (function)
4815 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004816 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004817 r += 10;
4818 }
4819 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004820 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004821 if (str != NULL)
4822 for (p = str; *p != NUL; )
4823 {
4824 if (*p == '\'')
4825 *r++ = '\'';
4826 MB_COPY_CHAR(p, r);
4827 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004828 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004829 if (function)
4830 *r++ = ')';
4831 *r++ = NUL;
4832 }
4833 return s;
4834}
4835
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004836#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004837/*
4838 * Convert the string "text" to a floating point number.
4839 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4840 * this always uses a decimal point.
4841 * Returns the length of the text that was consumed.
4842 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004843 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004844string2float(
4845 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004846 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004847{
4848 char *s = (char *)text;
4849 float_T f;
4850
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004851 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004852 if (STRNICMP(text, "inf", 3) == 0)
4853 {
4854 *value = INFINITY;
4855 return 3;
4856 }
4857 if (STRNICMP(text, "-inf", 3) == 0)
4858 {
4859 *value = -INFINITY;
4860 return 4;
4861 }
4862 if (STRNICMP(text, "nan", 3) == 0)
4863 {
4864 *value = NAN;
4865 return 3;
4866 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004867 f = strtod(s, &s);
4868 *value = f;
4869 return (int)((char_u *)s - text);
4870}
4871#endif
4872
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004873/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004875 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004877 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004878var2fpos(
4879 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004880 int dollar_lnum, // TRUE when $ is last line
4881 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004883 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004885 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004887 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004888 if (varp->v_type == VAR_LIST)
4889 {
4890 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004891 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004892 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004893 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004894
4895 l = varp->vval.v_list;
4896 if (l == NULL)
4897 return NULL;
4898
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004899 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004900 pos.lnum = list_find_nr(l, 0L, &error);
4901 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004902 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004903 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004904
Bram Moolenaarec65d772020-08-20 22:29:12 +02004905 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004906 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004907 li = list_find(l, 1L);
4908 if (li != NULL && li->li_tv.v_type == VAR_STRING
4909 && li->li_tv.vval.v_string != NULL
4910 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02004911 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004912 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02004913 }
4914 else
4915 {
4916 pos.col = list_find_nr(l, 1L, &error);
4917 if (error)
4918 return NULL;
4919 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00004920
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004921 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004922 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004923 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004924 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004925
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004926 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004927 pos.coladd = list_find_nr(l, 2L, &error);
4928 if (error)
4929 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004930
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004931 return &pos;
4932 }
4933
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004934 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004935 if (name == NULL)
4936 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004937 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004939 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004940 {
4941 if (VIsual_active)
4942 return &VIsual;
4943 return &curwin->w_cursor;
4944 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004945 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004947 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4949 return NULL;
4950 return pp;
4951 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004952
Bram Moolenaara5525202006-03-02 22:52:09 +00004953 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004954
Bram Moolenaar477933c2007-07-17 14:32:23 +00004955 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004956 {
4957 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004958 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004959 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004960 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004961 // In silent Ex mode topline is zero, but that's not a valid line
4962 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004963 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004964 return &pos;
4965 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004966 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004967 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004968 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004969 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004970 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004971 return &pos;
4972 }
4973 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004974 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004976 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 {
4978 pos.lnum = curbuf->b_ml.ml_line_count;
4979 pos.col = 0;
4980 }
4981 else
4982 {
4983 pos.lnum = curwin->w_cursor.lnum;
4984 pos.col = (colnr_T)STRLEN(ml_get_curline());
4985 }
4986 return &pos;
4987 }
4988 return NULL;
4989}
4990
4991/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004992 * Convert list in "arg" into a position and optional file number.
4993 * When "fnump" is NULL there is no file number, only 3 items.
4994 * Note that the column is passed on as-is, the caller may want to decrement
4995 * it to use 1 for the first column.
4996 * Return FAIL when conversion is not possible, doesn't check the position for
4997 * validity.
4998 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004999 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005000list2fpos(
5001 typval_T *arg,
5002 pos_T *posp,
5003 int *fnump,
5004 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005005{
5006 list_T *l = arg->vval.v_list;
5007 long i = 0;
5008 long n;
5009
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005010 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5011 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005012 if (arg->v_type != VAR_LIST
5013 || l == NULL
5014 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005015 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005016 return FAIL;
5017
5018 if (fnump != NULL)
5019 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005020 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005021 if (n < 0)
5022 return FAIL;
5023 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005024 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005025 *fnump = n;
5026 }
5027
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005028 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005029 if (n < 0)
5030 return FAIL;
5031 posp->lnum = n;
5032
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005033 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005034 if (n < 0)
5035 return FAIL;
5036 posp->col = n;
5037
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005038 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005039 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005040 posp->coladd = 0;
5041 else
5042 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005043
Bram Moolenaar493c1782014-05-28 14:34:46 +02005044 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005045 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005046
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005047 return OK;
5048}
5049
5050/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 * Get the length of an environment variable name.
5052 * Advance "arg" to the first character after the name.
5053 * Return 0 for error.
5054 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005055 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005056get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057{
5058 char_u *p;
5059 int len;
5060
5061 for (p = *arg; vim_isIDc(*p); ++p)
5062 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005063 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 return 0;
5065
5066 len = (int)(p - *arg);
5067 *arg = p;
5068 return len;
5069}
5070
5071/*
5072 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005073 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 * Return 0 if something is wrong.
5075 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005076 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005077get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078{
5079 char_u *p;
5080 int len;
5081
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005082 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005084 {
5085 if (*p == ':')
5086 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005087 // "s:" is start of "s:var", but "n:" is not and can be used in
5088 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005089 len = (int)(p - *arg);
5090 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5091 || len > 1)
5092 break;
5093 }
5094 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005095 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 return 0;
5097
5098 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005099 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100
5101 return len;
5102}
5103
5104/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005105 * Get the length of the name of a variable or function.
5106 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005108 * Return -1 if curly braces expansion failed.
5109 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 * If the name contains 'magic' {}'s, expand them and return the
5111 * expanded name in an allocated string via 'alias' - caller must free.
5112 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005113 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005114get_name_len(
5115 char_u **arg,
5116 char_u **alias,
5117 int evaluate,
5118 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119{
5120 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 char_u *p;
5122 char_u *expr_start;
5123 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005125 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126
5127 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5128 && (*arg)[2] == (int)KE_SNR)
5129 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005130 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 *arg += 3;
5132 return get_id_len(arg) + 3;
5133 }
5134 len = eval_fname_script(*arg);
5135 if (len > 0)
5136 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005137 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 *arg += len;
5139 }
5140
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005142 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005144 p = find_name_end(*arg, &expr_start, &expr_end,
5145 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 if (expr_start != NULL)
5147 {
5148 char_u *temp_string;
5149
5150 if (!evaluate)
5151 {
5152 len += (int)(p - *arg);
5153 *arg = skipwhite(p);
5154 return len;
5155 }
5156
5157 /*
5158 * Include any <SID> etc in the expanded string:
5159 * Thus the -len here.
5160 */
5161 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5162 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005163 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 *alias = temp_string;
5165 *arg = skipwhite(p);
5166 return (int)STRLEN(temp_string);
5167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168
5169 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005170 // Only give an error when there is something, otherwise it will be
5171 // reported at a higher level.
5172 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005173 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174
5175 return len;
5176}
5177
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005178/*
5179 * Find the end of a variable or function name, taking care of magic braces.
5180 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5181 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005182 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005183 * Return a pointer to just after the name. Equal to "arg" if there is no
5184 * valid name.
5185 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005186 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005187find_name_end(
5188 char_u *arg,
5189 char_u **expr_start,
5190 char_u **expr_end,
5191 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005193 int mb_nest = 0;
5194 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005196 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005197 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005199 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005201 *expr_start = NULL;
5202 *expr_end = NULL;
5203 }
5204
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005205 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005206 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5207 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005208 return arg;
5209
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005210 for (p = arg; *p != NUL
5211 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005212 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005213 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005214 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005215 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005216 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005217 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005218 if (*p == '\'')
5219 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005220 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005221 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005222 ;
5223 if (*p == NUL)
5224 break;
5225 }
5226 else if (*p == '"')
5227 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005228 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005229 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005230 if (*p == '\\' && p[1] != NUL)
5231 ++p;
5232 if (*p == NUL)
5233 break;
5234 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005235 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5236 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005237 // "s:" is start of "s:var", but "n:" is not and can be used in
5238 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005239 len = (int)(p - arg);
5240 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005241 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005242 break;
5243 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005244
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005245 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005247 if (*p == '[')
5248 ++br_nest;
5249 else if (*p == ']')
5250 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005252
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005253 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005255 if (*p == '{')
5256 {
5257 mb_nest++;
5258 if (expr_start != NULL && *expr_start == NULL)
5259 *expr_start = p;
5260 }
5261 else if (*p == '}')
5262 {
5263 mb_nest--;
5264 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5265 *expr_end = p;
5266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 }
5269
5270 return p;
5271}
5272
5273/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005274 * Expands out the 'magic' {}'s in a variable/function name.
5275 * Note that this can call itself recursively, to deal with
5276 * constructs like foo{bar}{baz}{bam}
5277 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5278 * "in_start" ^
5279 * "expr_start" ^
5280 * "expr_end" ^
5281 * "in_end" ^
5282 *
5283 * Returns a new allocated string, which the caller must free.
5284 * Returns NULL for failure.
5285 */
5286 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005287make_expanded_name(
5288 char_u *in_start,
5289 char_u *expr_start,
5290 char_u *expr_end,
5291 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005292{
5293 char_u c1;
5294 char_u *retval = NULL;
5295 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005296
5297 if (expr_end == NULL || in_end == NULL)
5298 return NULL;
5299 *expr_start = NUL;
5300 *expr_end = NUL;
5301 c1 = *in_end;
5302 *in_end = NUL;
5303
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005304 temp_result = eval_to_string(expr_start + 1, FALSE);
5305 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005306 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005307 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5308 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005309 if (retval != NULL)
5310 {
5311 STRCPY(retval, in_start);
5312 STRCAT(retval, temp_result);
5313 STRCAT(retval, expr_end + 1);
5314 }
5315 }
5316 vim_free(temp_result);
5317
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005318 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005319 *expr_start = '{';
5320 *expr_end = '}';
5321
5322 if (retval != NULL)
5323 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005324 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005325 if (expr_start != NULL)
5326 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005327 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005328 temp_result = make_expanded_name(retval, expr_start,
5329 expr_end, temp_result);
5330 vim_free(retval);
5331 retval = temp_result;
5332 }
5333 }
5334
5335 return retval;
5336}
5337
5338/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005340 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005342 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005343eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005345 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005346}
5347
5348/*
5349 * Return TRUE if character "c" can be used as the first character in a
5350 * variable or function name (excluding '{' and '}').
5351 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005352 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005353eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005354{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005355 return ASCII_ISALPHA(c) || c == '_';
5356}
5357
5358/*
5359 * Return TRUE if character "c" can be used as the first character of a
5360 * dictionary key.
5361 */
5362 int
5363eval_isdictc(int c)
5364{
5365 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366}
5367
5368/*
Bram Moolenaare3c37d82020-08-15 18:39:05 +02005369 * Return the character "str[index]" where "index" is the character index. If
5370 * "index" is out of range NULL is returned.
5371 */
5372 char_u *
5373char_from_string(char_u *str, varnumber_T index)
5374{
5375 size_t nbyte = 0;
5376 varnumber_T nchar = index;
5377 size_t slen;
5378
5379 if (str == NULL || index < 0)
5380 return NULL;
5381 slen = STRLEN(str);
5382 while (nchar > 0 && nbyte < slen)
5383 {
5384 nbyte += MB_CPTR2LEN(str + nbyte);
5385 --nchar;
5386 }
5387 if (nbyte >= slen)
5388 return NULL;
5389 return vim_strnsave(str + nbyte, MB_CPTR2LEN(str + nbyte));
5390}
5391
5392/*
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005393 * Get the byte index for character index "idx" in string "str" with length
5394 * "str_len".
5395 * If going over the end return "str_len".
5396 * If "idx" is negative count from the end, -1 is the last character.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005397 * When going over the start return -1.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005398 */
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005399 static long
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005400char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
5401{
5402 varnumber_T nchar = idx;
5403 size_t nbyte = 0;
5404
5405 if (nchar >= 0)
5406 {
5407 while (nchar > 0 && nbyte < str_len)
5408 {
5409 nbyte += MB_CPTR2LEN(str + nbyte);
5410 --nchar;
5411 }
5412 }
5413 else
5414 {
5415 nbyte = str_len;
5416 while (nchar < 0 && nbyte > 0)
5417 {
5418 --nbyte;
5419 nbyte -= mb_head_off(str, str + nbyte);
5420 ++nchar;
5421 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005422 if (nchar < 0)
5423 return -1;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005424 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005425 return (long)nbyte;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005426}
5427
5428/*
5429 * Return the slice "str[first:last]" using character indexes.
5430 * Return NULL when the result is empty.
5431 */
5432 char_u *
5433string_slice(char_u *str, varnumber_T first, varnumber_T last)
5434{
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005435 long start_byte, end_byte;
5436 size_t slen;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005437
5438 if (str == NULL)
5439 return NULL;
5440 slen = STRLEN(str);
5441 start_byte = char_idx2byte(str, slen, first);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005442 if (start_byte < 0)
5443 start_byte = 0; // first index very negative: use zero
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005444 if (last == -1)
Bram Moolenaar25660542020-08-28 16:38:11 +02005445 end_byte = (long)slen;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005446 else
5447 {
5448 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005449 if (end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005450 // end index is inclusive
5451 end_byte += MB_CPTR2LEN(str + end_byte);
5452 }
5453
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005454 if (start_byte >= (long)slen || end_byte <= start_byte)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005455 return NULL;
5456 return vim_strnsave(str + start_byte, end_byte - start_byte);
5457}
5458
5459/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005460 * Handle:
5461 * - expr[expr], expr[expr:expr] subscript
5462 * - ".name" lookup
5463 * - function call with Funcref variable: func(expr)
5464 * - method call: var->method()
5465 *
5466 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005467 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005468 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005469handle_subscript(
5470 char_u **arg,
5471 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005472 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005473 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005474{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005475 int evaluate = evalarg != NULL
5476 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005477 int ret = OK;
5478 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005479 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005480 int getnext;
5481 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005482
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005483 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005484 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005485 // When at the end of the line and ".name" or "->{" or "->X" follows in
5486 // the next line then consume the line break.
5487 p = eval_next_non_blank(*arg, evalarg, &getnext);
5488 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005489 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaar9d489562020-07-30 20:08:50 +02005490 || (p[0] == '-' && p[1] == '>'
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005491 && (p[2] == '{' || ASCII_ISALPHA(p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005492 {
5493 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005494 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005495 check_white = FALSE;
5496 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005497
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005498 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5499 || rettv->v_type == VAR_PARTIAL))
5500 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005501 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005502 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5503 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005504
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005505 // Stop the expression evaluation when immediately aborting on
5506 // error, or when an interrupt occurred or an exception was thrown
5507 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005508 if (aborting())
5509 {
5510 if (ret == OK)
5511 clear_tv(rettv);
5512 ret = FAIL;
5513 }
5514 dict_unref(selfdict);
5515 selfdict = NULL;
5516 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005517 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005518 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02005519 *arg = p;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005520 if (ret == OK)
5521 {
5522 if ((*arg)[2] == '{')
5523 // expr->{lambda}()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005524 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005525 else
5526 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005527 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005528 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005529 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005530 // "." is ".name" lookup when we found a dict or when evaluating and
5531 // scriptversion is at least 2, where string concatenation is "..".
5532 else if (**arg == '['
5533 || (**arg == '.' && (rettv->v_type == VAR_DICT
5534 || (!evaluate
5535 && (*arg)[1] != '.'
5536 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005537 {
5538 dict_unref(selfdict);
5539 if (rettv->v_type == VAR_DICT)
5540 {
5541 selfdict = rettv->vval.v_dict;
5542 if (selfdict != NULL)
5543 ++selfdict->dv_refcount;
5544 }
5545 else
5546 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005547 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005548 {
5549 clear_tv(rettv);
5550 ret = FAIL;
5551 }
5552 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005553 else
5554 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005555 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005556
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005557 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5558 // Don't do this when "Func" is already a partial that was bound
5559 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005560 if (selfdict != NULL
5561 && (rettv->v_type == VAR_FUNC
5562 || (rettv->v_type == VAR_PARTIAL
5563 && (rettv->vval.v_partial->pt_auto
5564 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005565 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005566
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005567 dict_unref(selfdict);
5568 return ret;
5569}
5570
5571/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005572 * Make a copy of an item.
5573 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005574 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5575 * reference to an already copied list/dict can be used.
5576 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005577 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005578 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005579item_copy(
5580 typval_T *from,
5581 typval_T *to,
5582 int deep,
5583 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005584{
5585 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005586 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005587
Bram Moolenaar33570922005-01-25 22:26:29 +00005588 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005589 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005590 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005591 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005592 }
5593 ++recurse;
5594
5595 switch (from->v_type)
5596 {
5597 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005598 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005599 case VAR_STRING:
5600 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005601 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005602 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005603 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005604 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005605 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005606 copy_tv(from, to);
5607 break;
5608 case VAR_LIST:
5609 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005610 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005611 if (from->vval.v_list == NULL)
5612 to->vval.v_list = NULL;
5613 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5614 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005615 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005616 to->vval.v_list = from->vval.v_list->lv_copylist;
5617 ++to->vval.v_list->lv_refcount;
5618 }
5619 else
5620 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5621 if (to->vval.v_list == NULL)
5622 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005623 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005624 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005625 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005626 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005627 case VAR_DICT:
5628 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005629 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005630 if (from->vval.v_dict == NULL)
5631 to->vval.v_dict = NULL;
5632 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5633 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005634 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005635 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5636 ++to->vval.v_dict->dv_refcount;
5637 }
5638 else
5639 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5640 if (to->vval.v_dict == NULL)
5641 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005642 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005643 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005644 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005645 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005646 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005647 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005648 }
5649 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005650 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005651}
5652
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005653 void
5654echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5655{
5656 char_u *tofree;
5657 char_u numbuf[NUMBUFLEN];
5658 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5659
5660 if (*atstart)
5661 {
5662 *atstart = FALSE;
5663 // Call msg_start() after eval1(), evaluating the expression
5664 // may cause a message to appear.
5665 if (with_space)
5666 {
5667 // Mark the saved text as finishing the line, so that what
5668 // follows is displayed on a new line when scrolling back
5669 // at the more prompt.
5670 msg_sb_eol();
5671 msg_start();
5672 }
5673 }
5674 else if (with_space)
5675 msg_puts_attr(" ", echo_attr);
5676
5677 if (p != NULL)
5678 for ( ; *p != NUL && !got_int; ++p)
5679 {
5680 if (*p == '\n' || *p == '\r' || *p == TAB)
5681 {
5682 if (*p != TAB && *needclr)
5683 {
5684 // remove any text still there from the command
5685 msg_clr_eos();
5686 *needclr = FALSE;
5687 }
5688 msg_putchar_attr(*p, echo_attr);
5689 }
5690 else
5691 {
5692 if (has_mbyte)
5693 {
5694 int i = (*mb_ptr2len)(p);
5695
5696 (void)msg_outtrans_len_attr(p, i, echo_attr);
5697 p += i - 1;
5698 }
5699 else
5700 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5701 }
5702 }
5703 vim_free(tofree);
5704}
5705
Bram Moolenaare9a41262005-01-15 22:18:47 +00005706/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 * ":echo expr1 ..." print each argument separated with a space, add a
5708 * newline at the end.
5709 * ":echon expr1 ..." print each argument plain.
5710 */
5711 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005712ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713{
5714 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005715 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 char_u *p;
5717 int needclr = TRUE;
5718 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01005719 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005720 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005721 evalarg_T evalarg;
5722
Bram Moolenaare707c882020-07-01 13:07:37 +02005723 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724
5725 if (eap->skip)
5726 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005727 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005729 // If eval1() causes an error message the text from the command may
5730 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005731 need_clr_eos = needclr;
5732
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005734 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 {
5736 /*
5737 * Report the invalid expression unless the expression evaluation
5738 * has been cancelled due to an aborting error, an interrupt, or an
5739 * exception.
5740 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005741 if (!aborting() && did_emsg == did_emsg_before
5742 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005743 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005744 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 break;
5746 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005747 need_clr_eos = FALSE;
5748
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005750 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005751
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005752 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 arg = skipwhite(arg);
5754 }
5755 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02005756 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757
5758 if (eap->skip)
5759 --emsg_skip;
5760 else
5761 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005762 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 if (needclr)
5764 msg_clr_eos();
5765 if (eap->cmdidx == CMD_echo)
5766 msg_end();
5767 }
5768}
5769
5770/*
5771 * ":echohl {name}".
5772 */
5773 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005774ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005776 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777}
5778
5779/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005780 * Returns the :echo attribute
5781 */
5782 int
5783get_echo_attr(void)
5784{
5785 return echo_attr;
5786}
5787
5788/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 * ":execute expr1 ..." execute the result of an expression.
5790 * ":echomsg expr1 ..." Print a message
5791 * ":echoerr expr1 ..." Print an error
5792 * Each gets spaces around each argument and a newline at the end for
5793 * echo commands
5794 */
5795 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005796ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797{
5798 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005799 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 int ret = OK;
5801 char_u *p;
5802 garray_T ga;
5803 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804
5805 ga_init2(&ga, 1, 80);
5806
5807 if (eap->skip)
5808 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02005809 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02005811 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01005812 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814
5815 if (!eap->skip)
5816 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005817 char_u buf[NUMBUFLEN];
5818
5819 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01005820 {
5821 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
5822 {
5823 emsg(_(e_inval_string));
5824 p = NULL;
5825 }
5826 else
5827 p = tv_get_string_buf(&rettv, buf);
5828 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005829 else
5830 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005831 if (p == NULL)
5832 {
5833 clear_tv(&rettv);
5834 ret = FAIL;
5835 break;
5836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 len = (int)STRLEN(p);
5838 if (ga_grow(&ga, len + 2) == FAIL)
5839 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005840 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 ret = FAIL;
5842 break;
5843 }
5844 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 ga.ga_len += len;
5848 }
5849
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005850 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 arg = skipwhite(arg);
5852 }
5853
5854 if (ret != FAIL && ga.ga_data != NULL)
5855 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005856 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
5857 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005858 // Mark the already saved text as finishing the line, so that what
5859 // follows is displayed on a new line when scrolling back at the
5860 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005861 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005862 }
5863
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005865 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005866 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005867 out_flush();
5868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 else if (eap->cmdidx == CMD_echoerr)
5870 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005871 int save_did_emsg = did_emsg;
5872
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005873 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005874 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 if (!force_abort)
5876 did_emsg = save_did_emsg;
5877 }
5878 else if (eap->cmdidx == CMD_execute)
5879 do_cmdline((char_u *)ga.ga_data,
5880 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
5881 }
5882
5883 ga_clear(&ga);
5884
5885 if (eap->skip)
5886 --emsg_skip;
5887
5888 eap->nextcmd = check_nextcmd(arg);
5889}
5890
5891/*
5892 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
5893 * "arg" points to the "&" or '+' when called, to "option" when returning.
5894 * Returns NULL when no option name found. Otherwise pointer to the char
5895 * after the option name.
5896 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005897 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005898find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899{
5900 char_u *p = *arg;
5901
5902 ++p;
5903 if (*p == 'g' && p[1] == ':')
5904 {
5905 *opt_flags = OPT_GLOBAL;
5906 p += 2;
5907 }
5908 else if (*p == 'l' && p[1] == ':')
5909 {
5910 *opt_flags = OPT_LOCAL;
5911 p += 2;
5912 }
5913 else
5914 *opt_flags = 0;
5915
5916 if (!ASCII_ISALPHA(*p))
5917 return NULL;
5918 *arg = p;
5919
5920 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005921 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 else
5923 while (ASCII_ISALPHA(*p))
5924 ++p;
5925 return p;
5926}
5927
5928/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00005929 * Display script name where an item was last set.
5930 * Should only be invoked when 'verbose' is non-zero.
5931 */
5932 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005933last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005934{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005935 char_u *p;
5936
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005937 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005938 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005939 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005940 if (p != NULL)
5941 {
5942 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01005943 msg_puts(_("\n\tLast set from "));
5944 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005945 if (script_ctx.sc_lnum > 0)
5946 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01005947 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005948 msg_outnum((long)script_ctx.sc_lnum);
5949 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005950 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005951 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005952 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00005953 }
5954}
5955
Bram Moolenaarb005cd82019-09-04 15:54:55 +02005956#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957
5958/*
5959 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005960 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 * "flags" can be "g" to do a global substitute.
5962 * Returns an allocated string, NULL for error.
5963 */
5964 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005965do_string_sub(
5966 char_u *str,
5967 char_u *pat,
5968 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005969 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01005970 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971{
5972 int sublen;
5973 regmatch_T regmatch;
5974 int i;
5975 int do_all;
5976 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005977 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 garray_T ga;
5979 char_u *ret;
5980 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005981 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005983 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005985 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986
5987 ga_init2(&ga, 1, 200);
5988
5989 do_all = (flags[0] == 'g');
5990
5991 regmatch.rm_ic = p_ic;
5992 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
5993 if (regmatch.regprog != NULL)
5994 {
5995 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005996 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
5998 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005999 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006000 if (regmatch.startp[0] == regmatch.endp[0])
6001 {
6002 if (zero_width == regmatch.startp[0])
6003 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006004 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006005 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006006 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6007 (size_t)i);
6008 ga.ga_len += i;
6009 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006010 continue;
6011 }
6012 zero_width = regmatch.startp[0];
6013 }
6014
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 /*
6016 * Get some space for a temporary buffer to do the substitution
6017 * into. It will contain:
6018 * - The text up to where the match is.
6019 * - The substituted text.
6020 * - The text after the match.
6021 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006022 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006023 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6025 {
6026 ga_clear(&ga);
6027 break;
6028 }
6029
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006030 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 i = (int)(regmatch.startp[0] - tail);
6032 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006033 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006034 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 + ga.ga_len + i, TRUE, TRUE, FALSE);
6036 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006037 tail = regmatch.endp[0];
6038 if (*tail == NUL)
6039 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 if (!do_all)
6041 break;
6042 }
6043
6044 if (ga.ga_data != NULL)
6045 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6046
Bram Moolenaar473de612013-06-08 18:19:48 +02006047 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 }
6049
6050 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6051 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006052 if (p_cpo == empty_option)
6053 p_cpo = save_cpo;
6054 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006055 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006056 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057
6058 return ret;
6059}