blob: 18f3a5bc05ae7b66406ffeb8f2cecbec2659fbee [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())
194 retval = tv2bool(&tv);
195 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)
875 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100876 && lp->ll_tv->vval.v_dict != NULL)
877 && !(lp->ll_tv->v_type == VAR_BLOB
878 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000879 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000880 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100881 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000882 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000883 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000884 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000885 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000886 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100887 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000888 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000889 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000890
Bram Moolenaar8c711452005-01-14 21:53:12 +0000891 len = -1;
892 if (*p == '.')
893 {
894 key = p + 1;
895 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
896 ;
897 if (len == 0)
898 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000899 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100900 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000901 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000902 }
903 p = key + len;
904 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000905 else
906 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100907 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000908 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000909 if (*p == ':')
910 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000911 else
912 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000913 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200914 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000915 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100916 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000917 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100918 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000919 clear_tv(&var1);
920 return NULL;
921 }
Bram Moolenaar6a250262020-08-04 15:53:01 +0200922 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000923 }
924
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100925 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000926 if (*p == ':')
927 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000928 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000929 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000930 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +0200931 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100932 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000933 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000934 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100935 if (rettv != NULL
936 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100937 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100938 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100939 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +0000940 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000941 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100942 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100943 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000944 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000945 }
946 p = skipwhite(p + 1);
947 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000948 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000949 else
950 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000951 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200952 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200953 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000954 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100955 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000956 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000957 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100958 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000959 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100960 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100961 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000962 clear_tv(&var2);
963 return NULL;
964 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000965 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000966 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000967 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000968 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000969 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000970
Bram Moolenaar8c711452005-01-14 21:53:12 +0000971 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000972 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000973 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100974 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100975 clear_tv(&var1);
976 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000977 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000978 }
979
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100980 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000981 ++p;
982 }
983
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000984 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000985 {
986 if (len == -1)
987 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100988 // "[key]": get key from "var1"
989 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +0200990 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000991 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000992 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000993 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000994 }
995 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000996 lp->ll_list = NULL;
997 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000998 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200999
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001000 // When assigning to a scope dictionary check that a function and
1001 // variable name is valid (only variable name unless it is l: or
1002 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001003 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001004 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001005 int prevval;
1006 int wrong;
1007
1008 if (len != -1)
1009 {
1010 prevval = key[len];
1011 key[len] = NUL;
1012 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001013 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001014 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001015 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1016 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001017 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001018 || !valid_varname(key);
1019 if (len != -1)
1020 key[len] = prevval;
1021 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001022 {
1023 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001024 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001025 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001026 }
1027
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001028 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001029 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001030 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001031 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001032 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001033 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001034 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001035 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001036 return NULL;
1037 }
1038
Bram Moolenaar31b81602019-02-10 22:14:27 +01001039 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001040 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001041 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001042 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001043 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001044 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001045 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001046 }
1047 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001048 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001049 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001050 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001051 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001052 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001053 p = NULL;
1054 break;
1055 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001056 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001057 else if ((flags & GLV_READ_ONLY) == 0
1058 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001059 {
1060 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001061 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001062 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001063
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001064 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001065 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001066 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001067 else if (lp->ll_tv->v_type == VAR_BLOB)
1068 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001069 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1070
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001071 /*
1072 * Get the number and item for the only or first index of the List.
1073 */
1074 if (empty1)
1075 lp->ll_n1 = 0;
1076 else
1077 // is number or string
1078 lp->ll_n1 = (long)tv_get_number(&var1);
1079 clear_tv(&var1);
1080
1081 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001082 || lp->ll_n1 > bloblen
1083 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001084 {
1085 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001086 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001087 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001088 return NULL;
1089 }
1090 if (lp->ll_range && !lp->ll_empty2)
1091 {
1092 lp->ll_n2 = (long)tv_get_number(&var2);
1093 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001094 if (lp->ll_n2 < 0
1095 || lp->ll_n2 >= bloblen
1096 || lp->ll_n2 < lp->ll_n1)
1097 {
1098 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001099 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001100 return NULL;
1101 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001102 }
1103 lp->ll_blob = lp->ll_tv->vval.v_blob;
1104 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001105 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001106 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001107 else
1108 {
1109 /*
1110 * Get the number and item for the only or first index of the List.
1111 */
1112 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001113 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001114 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001115 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001116 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001117 clear_tv(&var1);
1118
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001119 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001120 lp->ll_list = lp->ll_tv->vval.v_list;
1121 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1122 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001123 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001124 if (lp->ll_n1 < 0)
1125 {
1126 lp->ll_n1 = 0;
1127 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1128 }
1129 }
1130 if (lp->ll_li == NULL)
1131 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001132 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001133 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001134 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001135 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001136 }
1137
1138 /*
1139 * May need to find the item or absolute index for the second
1140 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001141 * When no index given: "lp->ll_empty2" is TRUE.
1142 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001143 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001144 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001145 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001146 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001147 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001148 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001149 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001150 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001151 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001152 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001153 {
1154 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001155 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001156 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001157 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001158 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001159 }
1160
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001161 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001162 if (lp->ll_n1 < 0)
1163 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1164 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001165 {
1166 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001167 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001168 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001169 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001170 }
1171
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001172 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001173 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001174 }
1175
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001176 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001177 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001178 return p;
1179}
1180
1181/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001182 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001183 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001184 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001185clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001186{
1187 vim_free(lp->ll_exp_name);
1188 vim_free(lp->ll_newkey);
1189}
1190
1191/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001192 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001193 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001194 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1195 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001196 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001197 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001198set_var_lval(
1199 lval_T *lp,
1200 char_u *endp,
1201 typval_T *rettv,
1202 int copy,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001203 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar7454a062016-01-30 15:14:10 +01001204 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001205{
1206 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001207 listitem_T *ri;
1208 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001209
1210 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001211 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001212 cc = *endp;
1213 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001214 if (lp->ll_blob != NULL)
1215 {
1216 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001217
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001218 if (op != NULL && *op != '=')
1219 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001220 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001221 return;
1222 }
Bram Moolenaar021bda52020-08-17 21:07:22 +02001223 if (var_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
1224 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001225
1226 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1227 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001228 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001229
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001230 if (lp->ll_empty2)
1231 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1232
1233 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001234 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001235 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001236 return;
1237 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001238 if (lp->ll_empty2)
1239 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001240
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001241 ir = 0;
1242 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1243 blob_set(lp->ll_blob, il,
1244 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001245 }
1246 else
1247 {
1248 val = (int)tv_get_number_chk(rettv, &error);
1249 if (!error)
1250 {
1251 garray_T *gap = &lp->ll_blob->bv_ga;
1252
1253 // Allow for appending a byte. Setting a byte beyond
1254 // the end is an error otherwise.
1255 if (lp->ll_n1 < gap->ga_len
1256 || (lp->ll_n1 == gap->ga_len
1257 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1258 {
1259 blob_set(lp->ll_blob, lp->ll_n1, val);
1260 if (lp->ll_n1 == gap->ga_len)
1261 ++gap->ga_len;
1262 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001263 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001264 }
1265 }
1266 }
1267 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001268 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001269 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001270
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001271 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001272 {
1273 emsg(_(e_cannot_mod));
1274 *endp = cc;
1275 return;
1276 }
1277
Bram Moolenaarff697e62019-02-12 22:28:33 +01001278 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001279 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001280 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar79518e22017-02-17 16:31:35 +01001281 &tv, &di, TRUE, FALSE) == OK)
1282 {
1283 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001284 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1285 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001286 && tv_op(&tv, rettv, op) == OK)
1287 set_var(lp->ll_name, &tv, FALSE);
1288 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001289 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001290 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001291 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001292 {
1293 if (lp->ll_type != NULL
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001294 && check_typval_type(lp->ll_type, rettv, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001295 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001296 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001297 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001298 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001299 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001300 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001301 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001302 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001303 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001304 else if (lp->ll_range)
1305 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001306 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001307 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001308
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001309 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001310 {
1311 emsg(_("E996: Cannot lock a range"));
1312 return;
1313 }
1314
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001315 /*
1316 * Check whether any of the list items is locked
1317 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001318 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001319 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001320 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001321 return;
1322 ri = ri->li_next;
1323 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1324 break;
1325 ll_li = ll_li->li_next;
1326 ++ll_n1;
1327 }
1328
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001329 /*
1330 * Assign the List values to the list items.
1331 */
1332 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001333 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001334 if (op != NULL && *op != '=')
1335 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1336 else
1337 {
1338 clear_tv(&lp->ll_li->li_tv);
1339 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1340 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001341 ri = ri->li_next;
1342 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1343 break;
1344 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001345 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001346 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001347 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001348 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001349 ri = NULL;
1350 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001351 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001352 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001353 lp->ll_li = lp->ll_li->li_next;
1354 ++lp->ll_n1;
1355 }
1356 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001357 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001358 else if (lp->ll_empty2
1359 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001360 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001361 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001362 }
1363 else
1364 {
1365 /*
1366 * Assign to a List or Dictionary item.
1367 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001368 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001369 {
1370 emsg(_("E996: Cannot lock a list or dict"));
1371 return;
1372 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001373 if (lp->ll_newkey != NULL)
1374 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001375 if (op != NULL && *op != '=')
1376 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001377 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001378 return;
1379 }
1380
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001381 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001382 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001383 if (di == NULL)
1384 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001385 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1386 {
1387 vim_free(di);
1388 return;
1389 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001390 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001391 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001392 else if (op != NULL && *op != '=')
1393 {
1394 tv_op(lp->ll_tv, rettv, op);
1395 return;
1396 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001397 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001398 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001399
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001400 /*
1401 * Assign the value to the variable or list item.
1402 */
1403 if (copy)
1404 copy_tv(rettv, lp->ll_tv);
1405 else
1406 {
1407 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001408 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001409 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001410 }
1411 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001412}
1413
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001414/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001415 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1416 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001417 * Returns OK or FAIL.
1418 */
1419 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001420tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001421{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001422 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001423 char_u numbuf[NUMBUFLEN];
1424 char_u *s;
1425
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001426 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001427 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001428 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001429 {
1430 switch (tv1->v_type)
1431 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001432 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001433 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001434 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001435 case VAR_DICT:
1436 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001437 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001438 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001439 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001440 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001441 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001442 break;
1443
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001444 case VAR_BLOB:
1445 if (*op != '+' || tv2->v_type != VAR_BLOB)
1446 break;
1447 // BLOB += BLOB
1448 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1449 {
1450 blob_T *b1 = tv1->vval.v_blob;
1451 blob_T *b2 = tv2->vval.v_blob;
1452 int i, len = blob_len(b2);
1453 for (i = 0; i < len; i++)
1454 ga_append(&b1->bv_ga, blob_get(b2, i));
1455 }
1456 return OK;
1457
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001458 case VAR_LIST:
1459 if (*op != '+' || tv2->v_type != VAR_LIST)
1460 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001461 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001462 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
1463 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1464 return OK;
1465
1466 case VAR_NUMBER:
1467 case VAR_STRING:
1468 if (tv2->v_type == VAR_LIST)
1469 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001470 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001471 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001472 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001473 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001474#ifdef FEAT_FLOAT
1475 if (tv2->v_type == VAR_FLOAT)
1476 {
1477 float_T f = n;
1478
Bram Moolenaarff697e62019-02-12 22:28:33 +01001479 if (*op == '%')
1480 break;
1481 switch (*op)
1482 {
1483 case '+': f += tv2->vval.v_float; break;
1484 case '-': f -= tv2->vval.v_float; break;
1485 case '*': f *= tv2->vval.v_float; break;
1486 case '/': f /= tv2->vval.v_float; break;
1487 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001488 clear_tv(tv1);
1489 tv1->v_type = VAR_FLOAT;
1490 tv1->vval.v_float = f;
1491 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001492 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001493#endif
1494 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001495 switch (*op)
1496 {
1497 case '+': n += tv_get_number(tv2); break;
1498 case '-': n -= tv_get_number(tv2); break;
1499 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001500 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1501 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001502 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001503 clear_tv(tv1);
1504 tv1->v_type = VAR_NUMBER;
1505 tv1->vval.v_number = n;
1506 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001507 }
1508 else
1509 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001510 if (tv2->v_type == VAR_FLOAT)
1511 break;
1512
Bram Moolenaarff697e62019-02-12 22:28:33 +01001513 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001514 s = tv_get_string(tv1);
1515 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001516 clear_tv(tv1);
1517 tv1->v_type = VAR_STRING;
1518 tv1->vval.v_string = s;
1519 }
1520 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001521
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001522 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001523#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001524 {
1525 float_T f;
1526
Bram Moolenaarff697e62019-02-12 22:28:33 +01001527 if (*op == '%' || *op == '.'
1528 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001529 && tv2->v_type != VAR_NUMBER
1530 && tv2->v_type != VAR_STRING))
1531 break;
1532 if (tv2->v_type == VAR_FLOAT)
1533 f = tv2->vval.v_float;
1534 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001535 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001536 switch (*op)
1537 {
1538 case '+': tv1->vval.v_float += f; break;
1539 case '-': tv1->vval.v_float -= f; break;
1540 case '*': tv1->vval.v_float *= f; break;
1541 case '/': tv1->vval.v_float /= f; break;
1542 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001543 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001544#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001545 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001546 }
1547 }
1548
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001549 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001550 return FAIL;
1551}
1552
1553/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001554 * Evaluate the expression used in a ":for var in expr" command.
1555 * "arg" points to "var".
1556 * Set "*errp" to TRUE for an error, FALSE otherwise;
1557 * Return a pointer that holds the info. Null when there is an error.
1558 */
1559 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001560eval_for_line(
1561 char_u *arg,
1562 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001563 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001564 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001565{
Bram Moolenaar33570922005-01-25 22:26:29 +00001566 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001567 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001568 typval_T tv;
1569 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001570 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001571
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001572 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001573
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001574 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001575 if (fi == NULL)
1576 return NULL;
1577
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001578 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001579 if (expr == NULL)
1580 return fi;
1581
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001582 expr = skipwhite_and_linebreak(expr, evalarg);
1583 if (expr[0] != 'i' || expr[1] != 'n'
1584 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001585 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001586 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001587 return fi;
1588 }
1589
1590 if (skip)
1591 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001592 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1593 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001594 {
1595 *errp = FALSE;
1596 if (!skip)
1597 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001598 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001599 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001600 l = tv.vval.v_list;
1601 if (l == NULL)
1602 {
1603 // a null list is like an empty list: do nothing
1604 clear_tv(&tv);
1605 }
1606 else
1607 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001608 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001609 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001610
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001611 // No need to increment the refcount, it's already set for
1612 // the list being used in "tv".
1613 fi->fi_list = l;
1614 list_add_watch(l, &fi->fi_lw);
1615 fi->fi_lw.lw_item = l->lv_first;
1616 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001617 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001618 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001619 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001620 fi->fi_bi = 0;
1621 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001622 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001623 typval_T btv;
1624
1625 // Make a copy, so that the iteration still works when the
1626 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001627 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001628 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001629 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001630 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001631 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001632 else
1633 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001634 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001635 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001636 }
1637 }
1638 }
1639 if (skip)
1640 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001641 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001642
1643 return fi;
1644}
1645
1646/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001647 * Used when looping over a :for line, skip the "in expr" part.
1648 */
1649 void
1650skip_for_lines(void *fi_void, evalarg_T *evalarg)
1651{
1652 forinfo_T *fi = (forinfo_T *)fi_void;
1653 int i;
1654
1655 for (i = 0; i < fi->fi_break_count; ++i)
1656 eval_next_line(evalarg);
1657}
1658
1659/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001660 * Use the first item in a ":for" list. Advance to the next.
1661 * Assign the values to the variable (list). "arg" points to the first one.
1662 * Return TRUE when a valid item was found, FALSE when at end of list or
1663 * something wrong.
1664 */
1665 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001666next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001667{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001668 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001669 int result;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001670 int flag = in_vim9script() ? LET_NO_COMMAND : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001671 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001672
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001673 if (fi->fi_blob != NULL)
1674 {
1675 typval_T tv;
1676
1677 if (fi->fi_bi >= blob_len(fi->fi_blob))
1678 return FALSE;
1679 tv.v_type = VAR_NUMBER;
1680 tv.v_lock = VAR_FIXED;
1681 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1682 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001683 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001684 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001685 }
1686
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001687 item = fi->fi_lw.lw_item;
1688 if (item == NULL)
1689 result = FALSE;
1690 else
1691 {
1692 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001693 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001694 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001695 }
1696 return result;
1697}
1698
1699/*
1700 * Free the structure used to store info used by ":for".
1701 */
1702 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001703free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001704{
Bram Moolenaar33570922005-01-25 22:26:29 +00001705 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001706
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001707 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001708 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001709 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001710 list_unref(fi->fi_list);
1711 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001712 if (fi != NULL && fi->fi_blob != NULL)
1713 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001714 vim_free(fi);
1715}
1716
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001718set_context_for_expression(
1719 expand_T *xp,
1720 char_u *arg,
1721 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722{
1723 int got_eq = FALSE;
1724 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001725 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001727 if (cmdidx == CMD_let || cmdidx == CMD_const)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001728 {
1729 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001730 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001731 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001732 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001733 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001734 {
1735 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001736 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001737 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001738 break;
1739 }
1740 return;
1741 }
1742 }
1743 else
1744 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1745 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 while ((xp->xp_pattern = vim_strpbrk(arg,
1747 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1748 {
1749 c = *xp->xp_pattern;
1750 if (c == '&')
1751 {
1752 c = xp->xp_pattern[1];
1753 if (c == '&')
1754 {
1755 ++xp->xp_pattern;
1756 xp->xp_context = cmdidx != CMD_let || got_eq
1757 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1758 }
1759 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001760 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001762 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1763 xp->xp_pattern += 2;
1764
1765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 }
1767 else if (c == '$')
1768 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001769 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 xp->xp_context = EXPAND_ENV_VARS;
1771 }
1772 else if (c == '=')
1773 {
1774 got_eq = TRUE;
1775 xp->xp_context = EXPAND_EXPRESSION;
1776 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001777 else if (c == '#'
1778 && xp->xp_context == EXPAND_EXPRESSION)
1779 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001780 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001781 break;
1782 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001783 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 && xp->xp_context == EXPAND_FUNCTIONS
1785 && vim_strchr(xp->xp_pattern, '(') == NULL)
1786 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001787 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 break;
1789 }
1790 else if (cmdidx != CMD_let || got_eq)
1791 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001792 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 {
1794 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1795 if (c == '\\' && xp->xp_pattern[1] != NUL)
1796 ++xp->xp_pattern;
1797 xp->xp_context = EXPAND_NOTHING;
1798 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001799 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001801 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1803 /* skip */ ;
1804 xp->xp_context = EXPAND_NOTHING;
1805 }
1806 else if (c == '|')
1807 {
1808 if (xp->xp_pattern[1] == '|')
1809 {
1810 ++xp->xp_pattern;
1811 xp->xp_context = EXPAND_EXPRESSION;
1812 }
1813 else
1814 xp->xp_context = EXPAND_COMMANDS;
1815 }
1816 else
1817 xp->xp_context = EXPAND_EXPRESSION;
1818 }
1819 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001820 // Doesn't look like something valid, expand as an expression
1821 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001822 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 arg = xp->xp_pattern;
1824 if (*arg != NUL)
1825 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1826 /* skip */ ;
1827 }
1828 xp->xp_pattern = arg;
1829}
1830
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001832 * Return TRUE if "pat" matches "text".
1833 * Does not use 'cpo' and always uses 'magic'.
1834 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001835 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001836pattern_match(char_u *pat, char_u *text, int ic)
1837{
1838 int matches = FALSE;
1839 char_u *save_cpo;
1840 regmatch_T regmatch;
1841
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001842 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001843 save_cpo = p_cpo;
1844 p_cpo = (char_u *)"";
1845 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1846 if (regmatch.regprog != NULL)
1847 {
1848 regmatch.rm_ic = ic;
1849 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1850 vim_regfree(regmatch.regprog);
1851 }
1852 p_cpo = save_cpo;
1853 return matches;
1854}
1855
1856/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001857 * Handle a name followed by "(". Both for just "name(arg)" and for
1858 * "expr->name(arg)".
1859 * Returns OK or FAIL.
1860 */
1861 static int
1862eval_func(
1863 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02001864 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001865 char_u *name,
1866 int name_len,
1867 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001868 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001869 typval_T *basetv) // "expr" for "expr->name(arg)"
1870{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001871 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001872 char_u *s = name;
1873 int len = name_len;
1874 partial_T *partial;
1875 int ret = OK;
1876
1877 if (!evaluate)
1878 check_vars(s, len);
1879
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001880 // If "s" is the name of a variable of type VAR_FUNC
1881 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001882 s = deref_func_name(s, &len, &partial, !evaluate);
1883
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001884 // Need to make a copy, in case evaluating the arguments makes
1885 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001886 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001887 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001888 ret = FAIL;
1889 else
1890 {
1891 funcexe_T funcexe;
1892
1893 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001894 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001895 funcexe.firstline = curwin->w_cursor.lnum;
1896 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001897 funcexe.evaluate = evaluate;
1898 funcexe.partial = partial;
1899 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02001900 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001901 }
1902 vim_free(s);
1903
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001904 // If evaluate is FALSE rettv->v_type was not set in
1905 // get_func_tv, but it's needed in handle_subscript() to parse
1906 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001907 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
1908 {
1909 rettv->vval.v_string = NULL;
1910 rettv->v_type = VAR_FUNC;
1911 }
1912
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001913 // Stop the expression evaluation when immediately
1914 // aborting on error, or when an interrupt occurred or
1915 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001916 if (evaluate && aborting())
1917 {
1918 if (ret == OK)
1919 clear_tv(rettv);
1920 ret = FAIL;
1921 }
1922 return ret;
1923}
1924
1925/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02001926 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
1927 * comment) and there is a next line, return the next line (skipping blanks)
1928 * and set "getnext".
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001929 * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
1930 * "arg" must point somewhere inside a line, not at the start.
1931 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001932 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001933eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
1934{
Bram Moolenaar9d489562020-07-30 20:08:50 +02001935 char_u *p = skipwhite(arg);
1936
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001937 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001938 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001939 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001940 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaar9d489562020-07-30 20:08:50 +02001941 && (*p == NUL || (VIM_ISWHITE(p[-1]) && vim9_comment_start(p))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001942 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02001943 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001944
1945 if (evalarg->eval_cookie != NULL)
Bram Moolenaar9d489562020-07-30 20:08:50 +02001946 next = getline_peek(evalarg->eval_getline, evalarg->eval_cookie);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001947 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02001948 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001949
Bram Moolenaar9d489562020-07-30 20:08:50 +02001950 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001951 {
1952 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02001953 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001954 }
1955 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02001956 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001957}
1958
1959/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001960 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001961 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001962 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001963eval_next_line(evalarg_T *evalarg)
1964{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001965 garray_T *gap = &evalarg->eval_ga;
1966 char_u *line;
1967
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001968 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02001969 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
1970 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001971 else
1972 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001973 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001974 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
1975 {
1976 // Going to concatenate the lines after parsing.
1977 ((char_u **)gap->ga_data)[gap->ga_len] = line;
1978 ++gap->ga_len;
1979 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001980 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001981 {
1982 vim_free(evalarg->eval_tofree);
1983 evalarg->eval_tofree = line;
1984 }
1985 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001986}
1987
Bram Moolenaare6b53242020-07-01 17:28:33 +02001988/*
1989 * Call eval_next_non_blank() and get the next line if needed.
1990 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02001991 char_u *
1992skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
1993{
1994 int getnext;
1995 char_u *p = skipwhite(arg);
1996
Bram Moolenaar962d7212020-07-04 14:15:00 +02001997 if (evalarg == NULL)
1998 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02001999 eval_next_non_blank(p, evalarg, &getnext);
2000 if (getnext)
2001 return eval_next_line(evalarg);
2002 return p;
2003}
2004
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002005/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002006 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002007 */
2008 void
2009clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2010{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002011 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002012 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002013 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002014 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002015 if (eap != NULL)
2016 {
2017 // We may need to keep the original command line, e.g. for
2018 // ":let" it has the variable names. But we may also need the
2019 // new one, "nextcmd" points into it. Keep both.
2020 vim_free(eap->cmdline_tofree);
2021 eap->cmdline_tofree = *eap->cmdlinep;
2022 *eap->cmdlinep = evalarg->eval_tofree;
2023 }
2024 else
2025 vim_free(evalarg->eval_tofree);
2026 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002027 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002028
2029 vim_free(evalarg->eval_tofree_lambda);
2030 evalarg->eval_tofree_lambda = NULL;
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002031 }
2032}
2033
2034/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002036 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2038 */
2039
2040/*
2041 * Handle zero level expression.
2042 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002043 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002044 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002045 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 * Return OK or FAIL.
2047 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002048 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002049eval0(
2050 char_u *arg,
2051 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002052 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002053 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054{
2055 int ret;
2056 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002057 int did_emsg_before = did_emsg;
2058 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002059 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060
2061 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002062 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002063 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002064
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002065 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 {
2067 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002068 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 /*
2070 * Report the invalid expression unless the expression evaluation has
2071 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002072 * exception, or we already gave a more specific error.
2073 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002075 if (!aborting()
2076 && did_emsg == did_emsg_before
2077 && called_emsg == called_emsg_before
2078 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002079 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 ret = FAIL;
2081 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002082
2083 if (eap != NULL)
2084 eap->nextcmd = check_nextcmd(p);
2085
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 return ret;
2087}
2088
2089/*
2090 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002091 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 *
2093 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002094 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002096 * Note: "rettv.v_lock" is not set.
2097 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 * Return OK or FAIL.
2099 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002100 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002101eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002103 char_u *p;
2104 int getnext;
2105
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002106 CLEAR_POINTER(rettv);
2107
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 /*
2109 * Get the first variable.
2110 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002111 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 return FAIL;
2113
Bram Moolenaar793648f2020-06-26 21:28:25 +02002114 p = eval_next_non_blank(*arg, evalarg, &getnext);
2115 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002117 int result;
2118 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002119 evalarg_T *evalarg_used = evalarg;
2120 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002121 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002122 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002123
2124 if (evalarg == NULL)
2125 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002126 CLEAR_FIELD(local_evalarg);
2127 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002128 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002129 orig_flags = evalarg_used->eval_flags;
2130 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002131
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002132 if (getnext)
2133 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002134 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002135 {
2136 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2137 {
2138 error_white_both(p, 1);
2139 clear_tv(rettv);
2140 return FAIL;
2141 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002142 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002143 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002144
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002146 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002148 int error = FALSE;
2149
Bram Moolenaar56acb092020-08-16 14:48:19 +02002150 if (in_vim9script())
2151 result = tv2bool(rettv);
2152 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002154 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002155 if (error)
2156 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 }
2158
2159 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002160 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 */
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002162 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
2163 {
2164 error_white_both(p, 1);
2165 clear_tv(rettv);
2166 return FAIL;
2167 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002168 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2169 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002170 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002171 if (eval1(arg, rettv, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002172 {
2173 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176
2177 /*
2178 * Check for the ":".
2179 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002180 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar793648f2020-06-26 21:28:25 +02002181 if (*p != ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002183 emsg(_(e_missing_colon));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002185 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002186 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 return FAIL;
2188 }
Bram Moolenaar793648f2020-06-26 21:28:25 +02002189 if (getnext)
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002190 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002191 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002192 {
2193 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2194 {
2195 error_white_both(p, 1);
2196 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002197 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002198 return FAIL;
2199 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002200 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202
2203 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002204 * Get the third variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 */
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002206 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
2207 {
2208 error_white_both(p, 1);
2209 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002210 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002211 return FAIL;
2212 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002213 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2214 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002215 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002216 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 {
2218 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002219 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002220 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 return FAIL;
2222 }
2223 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002224 *rettv = var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002225
2226 if (evalarg == NULL)
2227 clear_evalarg(&local_evalarg, NULL);
2228 else
2229 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 }
2231
2232 return OK;
2233}
2234
2235/*
2236 * Handle first level expression:
2237 * expr2 || expr2 || expr2 logical OR
2238 *
2239 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002240 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 *
2242 * Return OK or FAIL.
2243 */
2244 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002245eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002247 char_u *p;
2248 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249
2250 /*
2251 * Get the first variable.
2252 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002253 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 return FAIL;
2255
2256 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002257 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002259 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002260 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002262 evalarg_T *evalarg_used = evalarg;
2263 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002264 int evaluate;
2265 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002266 long result = FALSE;
2267 typval_T var2;
2268 int error;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002269 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002270
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002271 if (evalarg == NULL)
2272 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002273 CLEAR_FIELD(local_evalarg);
2274 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002275 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002276 orig_flags = evalarg_used->eval_flags;
2277 evaluate = orig_flags & EVAL_EVALUATE;
2278 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002279 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002280 if (vim9script)
2281 {
2282 result = tv2bool(rettv);
2283 }
2284 else
2285 {
2286 error = FALSE;
2287 if (tv_get_number_chk(rettv, &error) != 0)
2288 result = TRUE;
2289 clear_tv(rettv);
2290 if (error)
2291 return FAIL;
2292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 }
2294
2295 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002296 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002298 while (p[0] == '|' && p[1] == '|')
2299 {
2300 if (getnext)
2301 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002302 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002303 {
2304 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2305 {
2306 error_white_both(p, 2);
2307 clear_tv(rettv);
2308 return FAIL;
2309 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002310 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002311 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002312
2313 /*
2314 * Get the second variable.
2315 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002316 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2317 {
2318 error_white_both(p, 2);
2319 clear_tv(rettv);
2320 return FAIL;
2321 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002322 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2323 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002324 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002325 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002326 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002327
2328 /*
2329 * Compute the result.
2330 */
2331 if (evaluate && !result)
2332 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002333 if (vim9script)
2334 {
2335 clear_tv(rettv);
2336 *rettv = var2;
2337 result = tv2bool(rettv);
2338 }
2339 else
2340 {
2341 if (tv_get_number_chk(&var2, &error) != 0)
2342 result = TRUE;
2343 clear_tv(&var2);
2344 if (error)
2345 return FAIL;
2346 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002347 }
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002348 if (evaluate && !vim9script)
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002349 {
2350 rettv->v_type = VAR_NUMBER;
2351 rettv->vval.v_number = result;
2352 }
2353
2354 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002356
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002357 if (evalarg == NULL)
2358 clear_evalarg(&local_evalarg, NULL);
2359 else
2360 evalarg->eval_flags = orig_flags;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002361
2362 // Resulting value can be assigned to a bool.
2363 rettv->v_lock |= VAR_BOOL_OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 }
2365
2366 return OK;
2367}
2368
2369/*
2370 * Handle second level expression:
2371 * expr3 && expr3 && expr3 logical AND
2372 *
2373 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002374 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 *
2376 * Return OK or FAIL.
2377 */
2378 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002379eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002381 char_u *p;
2382 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383
2384 /*
2385 * Get the first variable.
2386 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002387 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 return FAIL;
2389
2390 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002391 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002393 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002394 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002396 evalarg_T *evalarg_used = evalarg;
2397 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002398 int orig_flags;
2399 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002400 long result = TRUE;
2401 typval_T var2;
2402 int error;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002403 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002404
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002405 if (evalarg == NULL)
2406 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002407 CLEAR_FIELD(local_evalarg);
2408 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002409 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002410 orig_flags = evalarg_used->eval_flags;
2411 evaluate = orig_flags & EVAL_EVALUATE;
2412 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002413 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002414 if (vim9script)
2415 {
2416 result = tv2bool(rettv);
2417 }
2418 else
2419 {
2420 error = FALSE;
2421 if (tv_get_number_chk(rettv, &error) == 0)
2422 result = FALSE;
2423 clear_tv(rettv);
2424 if (error)
2425 return FAIL;
2426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 }
2428
2429 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002430 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002432 while (p[0] == '&' && p[1] == '&')
2433 {
2434 if (getnext)
2435 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002436 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002437 {
2438 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2439 {
2440 error_white_both(p, 2);
2441 clear_tv(rettv);
2442 return FAIL;
2443 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002444 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002445 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002446
2447 /*
2448 * Get the second variable.
2449 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002450 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2451 {
2452 error_white_both(p, 2);
2453 clear_tv(rettv);
2454 return FAIL;
2455 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002456 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2457 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002458 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002459 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002460 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002461 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002462
2463 /*
2464 * Compute the result.
2465 */
2466 if (evaluate && result)
2467 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002468 if (vim9script)
2469 {
2470 clear_tv(rettv);
2471 *rettv = var2;
2472 result = tv2bool(rettv);
2473 }
2474 else
2475 {
2476 if (tv_get_number_chk(&var2, &error) == 0)
2477 result = FALSE;
2478 clear_tv(&var2);
2479 if (error)
2480 return FAIL;
2481 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002482 }
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002483 if (evaluate && !vim9script)
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002484 {
2485 rettv->v_type = VAR_NUMBER;
2486 rettv->vval.v_number = result;
2487 }
2488
2489 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002491
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002492 if (evalarg == NULL)
2493 clear_evalarg(&local_evalarg, NULL);
2494 else
2495 evalarg->eval_flags = orig_flags;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002496
2497 // Resulting value can be assigned to a bool.
2498 rettv->v_lock |= VAR_BOOL_OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 }
2500
2501 return OK;
2502}
2503
2504/*
2505 * Handle third level expression:
2506 * var1 == var2
2507 * var1 =~ var2
2508 * var1 != var2
2509 * var1 !~ var2
2510 * var1 > var2
2511 * var1 >= var2
2512 * var1 < var2
2513 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002514 * var1 is var2
2515 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 *
2517 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002518 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 *
2520 * Return OK or FAIL.
2521 */
2522 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002523eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002526 int getnext;
Bram Moolenaar87396072019-12-31 22:36:18 +01002527 exptype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002529 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530
2531 /*
2532 * Get the first variable.
2533 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002534 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 return FAIL;
2536
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002537 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002538 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539
2540 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002541 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002543 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002545 typval_T var2;
2546 int ic;
2547 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002548 int evaluate = evalarg == NULL
2549 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002550
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002551 if (getnext)
2552 *arg = eval_next_line(evalarg);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002553 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2554 {
2555 error_white_both(p, len);
2556 clear_tv(rettv);
2557 return FAIL;
2558 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002559
Bram Moolenaar696ba232020-07-29 21:20:41 +02002560 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2561 {
2562 semsg(_(e_invexpr2), p);
2563 clear_tv(rettv);
2564 return FAIL;
2565 }
2566
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002567 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 if (p[len] == '?')
2569 {
2570 ic = TRUE;
2571 ++len;
2572 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002573 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 else if (p[len] == '#')
2575 {
2576 ic = FALSE;
2577 ++len;
2578 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002579 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002581 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582
2583 /*
2584 * Get the second variable.
2585 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002586 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2587 {
2588 error_white_both(p, 1);
2589 clear_tv(rettv);
2590 return FAIL;
2591 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002592 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002593 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002595 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 return FAIL;
2597 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002598 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002599 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002600 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002601
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002602 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002603 {
2604 ret = FAIL;
2605 clear_tv(rettv);
2606 }
2607 else
2608 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002609 clear_tv(&var2);
2610 return ret;
2611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 }
2613
2614 return OK;
2615}
2616
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002617 void
2618eval_addblob(typval_T *tv1, typval_T *tv2)
2619{
2620 blob_T *b1 = tv1->vval.v_blob;
2621 blob_T *b2 = tv2->vval.v_blob;
2622 blob_T *b = blob_alloc();
2623 int i;
2624
2625 if (b != NULL)
2626 {
2627 for (i = 0; i < blob_len(b1); i++)
2628 ga_append(&b->bv_ga, blob_get(b1, i));
2629 for (i = 0; i < blob_len(b2); i++)
2630 ga_append(&b->bv_ga, blob_get(b2, i));
2631
2632 clear_tv(tv1);
2633 rettv_blob_set(tv1, b);
2634 }
2635}
2636
2637 int
2638eval_addlist(typval_T *tv1, typval_T *tv2)
2639{
2640 typval_T var3;
2641
2642 // concatenate Lists
2643 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2644 {
2645 clear_tv(tv1);
2646 clear_tv(tv2);
2647 return FAIL;
2648 }
2649 clear_tv(tv1);
2650 *tv1 = var3;
2651 return OK;
2652}
2653
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654/*
2655 * Handle fourth level expression:
2656 * + number addition
2657 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002658 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002659 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 *
2661 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002662 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 *
2664 * Return OK or FAIL.
2665 */
2666 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002667eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 /*
2670 * Get the first variable.
2671 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002672 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 return FAIL;
2674
2675 /*
2676 * Repeat computing, until no '+', '-' or '.' is following.
2677 */
2678 for (;;)
2679 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002680 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002681 int getnext;
2682 char_u *p;
2683 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002684 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002685 int concat;
2686 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002687 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002688
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002689 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002690 p = eval_next_non_blank(*arg, evalarg, &getnext);
2691 op = *p;
2692 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002693 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002695
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002696 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002697 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002698 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002699 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002700 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002701 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002702 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002703 {
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002704 error_white_both(p, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002705 clear_tv(rettv);
2706 return FAIL;
2707 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002708 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002709 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002710 if ((op != '+' || (rettv->v_type != VAR_LIST
2711 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002712#ifdef FEAT_FLOAT
2713 && (op == '.' || rettv->v_type != VAR_FLOAT)
2714#endif
2715 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002716 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002717 // For "list + ...", an illegal use of the first operand as
2718 // a number cannot be determined before evaluating the 2nd
2719 // operand: if this is also a list, all is ok.
2720 // For "something . ...", "something - ..." or "non-list + ...",
2721 // we know that the first operand needs to be a string or number
2722 // without evaluating the 2nd operand. So check before to avoid
2723 // side effects after an error.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002724 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002725 {
2726 clear_tv(rettv);
2727 return FAIL;
2728 }
2729 }
2730
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 /*
2732 * Get the second variable.
2733 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002734 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002735 {
2736 error_white_both(p, oplen);
2737 clear_tv(rettv);
2738 return FAIL;
2739 }
2740 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002741 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002743 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 return FAIL;
2745 }
2746
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002747 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 {
2749 /*
2750 * Compute the result.
2751 */
2752 if (op == '.')
2753 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002754 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2755 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002756 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002757
Bram Moolenaar2e086612020-08-25 22:37:48 +02002758 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002759 || var2.v_type == VAR_CHANNEL
2760 || var2.v_type == VAR_JOB))
2761 emsg(_(e_inval_string));
2762#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02002763 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002764 {
2765 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2766 var2.vval.v_float);
2767 s2 = buf2;
2768 }
2769#endif
2770 else
2771 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002772 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002773 {
2774 clear_tv(rettv);
2775 clear_tv(&var2);
2776 return FAIL;
2777 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002778 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002779 clear_tv(rettv);
2780 rettv->v_type = VAR_STRING;
2781 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002783 else if (op == '+' && rettv->v_type == VAR_BLOB
2784 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002785 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002786 else if (op == '+' && rettv->v_type == VAR_LIST
2787 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002788 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002789 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002790 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 else
2793 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002794 int error = FALSE;
2795 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002796#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002797 float_T f1 = 0, f2 = 0;
2798
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002799 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002800 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002801 f1 = rettv->vval.v_float;
2802 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002803 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002804 else
2805#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002806 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002807 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002808 if (error)
2809 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002810 // This can only happen for "list + non-list". For
2811 // "non-list + ..." or "something - ...", we returned
2812 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002813 clear_tv(rettv);
2814 return FAIL;
2815 }
2816#ifdef FEAT_FLOAT
2817 if (var2.v_type == VAR_FLOAT)
2818 f1 = n1;
2819#endif
2820 }
2821#ifdef FEAT_FLOAT
2822 if (var2.v_type == VAR_FLOAT)
2823 {
2824 f2 = var2.vval.v_float;
2825 n2 = 0;
2826 }
2827 else
2828#endif
2829 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002830 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002831 if (error)
2832 {
2833 clear_tv(rettv);
2834 clear_tv(&var2);
2835 return FAIL;
2836 }
2837#ifdef FEAT_FLOAT
2838 if (rettv->v_type == VAR_FLOAT)
2839 f2 = n2;
2840#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002841 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002842 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002843
2844#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002845 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002846 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2847 {
2848 if (op == '+')
2849 f1 = f1 + f2;
2850 else
2851 f1 = f1 - f2;
2852 rettv->v_type = VAR_FLOAT;
2853 rettv->vval.v_float = f1;
2854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002856#endif
2857 {
2858 if (op == '+')
2859 n1 = n1 + n2;
2860 else
2861 n1 = n1 - n2;
2862 rettv->v_type = VAR_NUMBER;
2863 rettv->vval.v_number = n1;
2864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002866 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 }
2868 }
2869 return OK;
2870}
2871
2872/*
2873 * Handle fifth level expression:
2874 * * number multiplication
2875 * / number division
2876 * % number modulo
2877 *
2878 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002879 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 *
2881 * Return OK or FAIL.
2882 */
2883 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002884eval6(
2885 char_u **arg,
2886 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002887 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002888 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002890#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002891 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002892#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893
2894 /*
2895 * Get the first variable.
2896 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002897 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 return FAIL;
2899
2900 /*
2901 * Repeat computing, until no '*', '/' or '%' is following.
2902 */
2903 for (;;)
2904 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002905 int evaluate;
2906 int getnext;
2907 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002908 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002909 int op;
2910 varnumber_T n1, n2;
2911#ifdef FEAT_FLOAT
2912 float_T f1, f2;
2913#endif
2914 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002915
Bram Moolenaar9d489562020-07-30 20:08:50 +02002916 p = eval_next_non_blank(*arg, evalarg, &getnext);
2917 op = *p;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002918 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002920
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002921 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002922 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002923 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002924 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002925 {
2926 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
2927 {
2928 error_white_both(p, 1);
2929 clear_tv(rettv);
2930 return FAIL;
2931 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002932 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002935#ifdef FEAT_FLOAT
2936 f1 = 0;
2937 f2 = 0;
2938#endif
2939 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002940 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002942#ifdef FEAT_FLOAT
2943 if (rettv->v_type == VAR_FLOAT)
2944 {
2945 f1 = rettv->vval.v_float;
2946 use_float = TRUE;
2947 n1 = 0;
2948 }
2949 else
2950#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002951 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002952 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002953 if (error)
2954 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 }
2956 else
2957 n1 = 0;
2958
2959 /*
2960 * Get the second variable.
2961 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002962 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
2963 {
2964 error_white_both(p, 1);
2965 clear_tv(rettv);
2966 return FAIL;
2967 }
2968 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002969 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 return FAIL;
2971
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002972 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002974#ifdef FEAT_FLOAT
2975 if (var2.v_type == VAR_FLOAT)
2976 {
2977 if (!use_float)
2978 {
2979 f1 = n1;
2980 use_float = TRUE;
2981 }
2982 f2 = var2.vval.v_float;
2983 n2 = 0;
2984 }
2985 else
2986#endif
2987 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002988 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002989 clear_tv(&var2);
2990 if (error)
2991 return FAIL;
2992#ifdef FEAT_FLOAT
2993 if (use_float)
2994 f2 = n2;
2995#endif
2996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997
2998 /*
2999 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003000 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003002#ifdef FEAT_FLOAT
3003 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003005 if (op == '*')
3006 f1 = f1 * f2;
3007 else if (op == '/')
3008 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003009# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003010 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003011 if (f2 == 0.0)
3012 {
3013 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003014 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003015 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003016 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003017 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003018 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003019 }
3020 else
3021 f1 = f1 / f2;
3022# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003023 // We rely on the floating point library to handle divide
3024 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003025 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003026# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003029 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003030 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003031 return FAIL;
3032 }
3033 rettv->v_type = VAR_FLOAT;
3034 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 }
3036 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003039 if (op == '*')
3040 n1 = n1 * n2;
3041 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01003042 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01003044 n1 = num_modulus(n1, n2);
3045
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003046 rettv->v_type = VAR_NUMBER;
3047 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 }
3050 }
3051
3052 return OK;
3053}
3054
3055/*
3056 * Handle sixth level expression:
3057 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003058 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003059 * "string" string constant
3060 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 * &option-name option value
3062 * @r register contents
3063 * identifier variable value
3064 * function() function call
3065 * $VAR environment variable
3066 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003067 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003068 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003069 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003070 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 *
3072 * Also handle:
3073 * ! in front logical NOT
3074 * - in front unary minus
3075 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003076 * trailing [] subscript in String or List
3077 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003078 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 *
3080 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003081 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 *
3083 * Return OK or FAIL.
3084 */
3085 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003086eval7(
3087 char_u **arg,
3088 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003089 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003090 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003092 int evaluate = evalarg != NULL
3093 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 int len;
3095 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 char_u *start_leader, *end_leader;
3097 int ret = OK;
3098 char_u *alias;
3099
3100 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003101 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003102 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003104 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105
3106 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003107 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 */
3109 start_leader = *arg;
3110 while (**arg == '!' || **arg == '-' || **arg == '+')
3111 *arg = skipwhite(*arg + 1);
3112 end_leader = *arg;
3113
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003114 if (**arg == '.' && (!isdigit(*(*arg + 1))
3115#ifdef FEAT_FLOAT
3116 || current_sctx.sc_version < 2
3117#endif
3118 ))
3119 {
3120 semsg(_(e_invexpr2), *arg);
3121 ++*arg;
3122 return FAIL;
3123 }
3124
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 switch (**arg)
3126 {
3127 /*
3128 * Number constant.
3129 */
3130 case '0':
3131 case '1':
3132 case '2':
3133 case '3':
3134 case '4':
3135 case '5':
3136 case '6':
3137 case '7':
3138 case '8':
3139 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003140 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003141
3142 // Apply prefixed "-" and "+" now. Matters especially when
3143 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003144 if (ret == OK && evaluate && end_leader > start_leader
3145 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003146 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 break;
3148
3149 /*
3150 * String constant: "string".
3151 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003152 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 break;
3154
3155 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003156 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003158 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003159 break;
3160
3161 /*
3162 * List: [expr, expr]
3163 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003164 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 break;
3166
3167 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003168 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003169 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003170 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003171 {
3172 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003173 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003174 }
3175 else
3176 ret = NOTDONE;
3177 break;
3178
3179 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003180 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003181 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003182 */
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003183 case '{': ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003184 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003185 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003186 break;
3187
3188 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003189 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003191 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 break;
3193
3194 /*
3195 * Environment variable: $VAR.
3196 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003197 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 break;
3199
3200 /*
3201 * Register contents: @r.
3202 */
3203 case '@': ++*arg;
3204 if (evaluate)
3205 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003206 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02003207 rettv->vval.v_string = get_reg_contents(**arg,
3208 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 }
3210 if (**arg != NUL)
3211 ++*arg;
3212 break;
3213
3214 /*
3215 * nested expression: (expression).
3216 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003217 case '(': {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003218 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003219 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003220
Bram Moolenaar9215f012020-06-27 21:18:00 +02003221 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003222 if (**arg == ')')
3223 ++*arg;
3224 else if (ret == OK)
3225 {
3226 emsg(_(e_missing_close));
3227 clear_tv(rettv);
3228 ret = FAIL;
3229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 }
3231 break;
3232
Bram Moolenaar8c711452005-01-14 21:53:12 +00003233 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 break;
3235 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003236
3237 if (ret == NOTDONE)
3238 {
3239 /*
3240 * Must be a variable or function name.
3241 * Can also be a curly-braces kind of name: {expr}.
3242 */
3243 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003244 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003245 if (alias != NULL)
3246 s = alias;
3247
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003248 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003249 ret = FAIL;
3250 else
3251 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003252 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3253
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003254 if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
3255 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003256 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003257 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003258 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003259 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003260 else if (flags & EVAL_CONSTANT)
3261 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003262 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003263 {
3264 // get the value of "true", "false" or a variable
3265 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3266 {
3267 rettv->v_type = VAR_BOOL;
3268 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003269 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003270 }
3271 else if (len == 5 && in_vim9script()
3272 && STRNCMP(s, "false", 4) == 0)
3273 {
3274 rettv->v_type = VAR_BOOL;
3275 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003276 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003277 }
3278 else
3279 ret = eval_variable(s, len, rettv, NULL, TRUE, FALSE);
3280 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003281 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003282 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003283 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003284 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003285 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003286 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003287 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003288 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003289 }
3290
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003291 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3292 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003293 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003294 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295
3296 /*
3297 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3298 */
3299 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003300 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003301 return ret;
3302}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003303
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003304/*
3305 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003306 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003307 * Adjusts "end_leaderp" until it is at "start_leader".
3308 */
3309 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003310eval7_leader(
3311 typval_T *rettv,
3312 int numeric_only,
3313 char_u *start_leader,
3314 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003315{
3316 char_u *end_leader = *end_leaderp;
3317 int ret = OK;
3318 int error = FALSE;
3319 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003320 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003321#ifdef FEAT_FLOAT
3322 float_T f = 0.0;
3323
3324 if (rettv->v_type == VAR_FLOAT)
3325 f = rettv->vval.v_float;
3326 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003327#endif
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003328 if (in_vim9script() && end_leader[-1] == '!')
3329 val = tv2bool(rettv);
3330 else
3331 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003332 if (error)
3333 {
3334 clear_tv(rettv);
3335 ret = FAIL;
3336 }
3337 else
3338 {
3339 while (end_leader > start_leader)
3340 {
3341 --end_leader;
3342 if (*end_leader == '!')
3343 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003344 if (numeric_only)
3345 {
3346 ++end_leader;
3347 break;
3348 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003349#ifdef FEAT_FLOAT
3350 if (rettv->v_type == VAR_FLOAT)
3351 f = !f;
3352 else
3353#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003354 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003355 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003356 type = VAR_BOOL;
3357 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003358 }
3359 else if (*end_leader == '-')
3360 {
3361#ifdef FEAT_FLOAT
3362 if (rettv->v_type == VAR_FLOAT)
3363 f = -f;
3364 else
3365#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003366 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003367 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003368 type = VAR_NUMBER;
3369 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003370 }
3371 }
3372#ifdef FEAT_FLOAT
3373 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003375 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003376 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003378 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003379#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003380 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003381 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003382 if (in_vim9script())
3383 rettv->v_type = type;
3384 else
3385 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003386 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003389 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 return ret;
3391}
3392
3393/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003394 * Call the function referred to in "rettv".
3395 */
3396 static int
3397call_func_rettv(
3398 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003399 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003400 typval_T *rettv,
3401 int evaluate,
3402 dict_T *selfdict,
3403 typval_T *basetv)
3404{
3405 partial_T *pt = NULL;
3406 funcexe_T funcexe;
3407 typval_T functv;
3408 char_u *s;
3409 int ret;
3410
3411 // need to copy the funcref so that we can clear rettv
3412 if (evaluate)
3413 {
3414 functv = *rettv;
3415 rettv->v_type = VAR_UNKNOWN;
3416
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003417 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003418 if (functv.v_type == VAR_PARTIAL)
3419 {
3420 pt = functv.vval.v_partial;
3421 s = partial_name(pt);
3422 }
3423 else
3424 s = functv.vval.v_string;
3425 }
3426 else
3427 s = (char_u *)"";
3428
Bram Moolenaara80faa82020-04-12 19:37:17 +02003429 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003430 funcexe.firstline = curwin->w_cursor.lnum;
3431 funcexe.lastline = curwin->w_cursor.lnum;
3432 funcexe.evaluate = evaluate;
3433 funcexe.partial = pt;
3434 funcexe.selfdict = selfdict;
3435 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003436 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003437
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003438 // Clear the funcref afterwards, so that deleting it while
3439 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003440 if (evaluate)
3441 clear_tv(&functv);
3442
3443 return ret;
3444}
3445
3446/*
3447 * Evaluate "->method()".
3448 * "*arg" points to the '-'.
3449 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3450 */
3451 static int
3452eval_lambda(
3453 char_u **arg,
3454 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003455 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003456 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003457{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003458 int evaluate = evalarg != NULL
3459 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003460 typval_T base = *rettv;
3461 int ret;
3462
3463 // Skip over the ->.
3464 *arg += 2;
3465 rettv->v_type = VAR_UNKNOWN;
3466
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003467 ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003468 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003469 return FAIL;
3470 else if (**arg != '(')
3471 {
3472 if (verbose)
3473 {
3474 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003475 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003476 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003477 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003478 }
3479 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003480 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003481 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003482 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003483 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003484
3485 // Clear the funcref afterwards, so that deleting it while
3486 // evaluating the arguments is possible (see test55).
3487 if (evaluate)
3488 clear_tv(&base);
3489
3490 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003491}
3492
3493/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003494 * Evaluate "->method()".
3495 * "*arg" points to the '-'.
3496 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3497 */
3498 static int
3499eval_method(
3500 char_u **arg,
3501 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003502 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003503 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003504{
3505 char_u *name;
3506 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003507 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003508 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003509 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003510 int evaluate = evalarg != NULL
3511 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003512
3513 // Skip over the ->.
3514 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003515 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003516
Bram Moolenaarac92e252019-08-03 21:58:38 +02003517 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003518 len = get_name_len(arg, &alias, evaluate, TRUE);
3519 if (alias != NULL)
3520 name = alias;
3521
3522 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003523 {
3524 if (verbose)
3525 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003526 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003527 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003528 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003529 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003530 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003531 if (**arg != '(')
3532 {
3533 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003534 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003535 ret = FAIL;
3536 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003537 else if (VIM_ISWHITE((*arg)[-1]))
3538 {
3539 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003540 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003541 ret = FAIL;
3542 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003543 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003544 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003545 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003546 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003547
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003548 // Clear the funcref afterwards, so that deleting it while
3549 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003550 if (evaluate)
3551 clear_tv(&base);
3552
Bram Moolenaarac92e252019-08-03 21:58:38 +02003553 return ret;
3554}
3555
3556/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003557 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3558 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003559 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3560 */
3561 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003562eval_index(
3563 char_u **arg,
3564 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003565 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003566 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003567{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003568 int evaluate = evalarg != NULL
3569 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003570 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003571 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003572 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003573 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003574 int keylen = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003575
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003576 if (check_can_index(rettv, evaluate, verbose) == FAIL)
3577 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003578
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003579 init_tv(&var1);
3580 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003581 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003582 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003583 /*
3584 * dict.name
3585 */
3586 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003587 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003588 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003589 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003590 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02003591 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003592 }
3593 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003594 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003595 /*
3596 * something[idx]
3597 *
3598 * Get the (first) variable from inside the [].
3599 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003600 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003601 if (**arg == ':')
3602 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003603 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003604 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003605 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003606 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003607 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003608 clear_tv(&var1);
3609 return FAIL;
3610 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003611
3612 /*
3613 * Get the second variable from inside the [:].
3614 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003615 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003616 if (**arg == ':')
3617 {
3618 range = TRUE;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003619 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003620 if (**arg == ']')
3621 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003622 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003623 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003624 if (!empty1)
3625 clear_tv(&var1);
3626 return FAIL;
3627 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003628 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003629 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003630 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003631 if (!empty1)
3632 clear_tv(&var1);
3633 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003634 return FAIL;
3635 }
3636 }
3637
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003638 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003639 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003640 if (**arg != ']')
3641 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003642 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003643 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003644 clear_tv(&var1);
3645 if (range)
3646 clear_tv(&var2);
3647 return FAIL;
3648 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02003649 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003650 }
3651
3652 if (evaluate)
3653 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003654 int res = eval_index_inner(rettv, range,
3655 empty1 ? NULL : &var1, empty2 ? NULL : &var2,
3656 key, keylen, verbose);
3657 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003658 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003659 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003660 clear_tv(&var2);
3661 return res;
3662 }
3663 return OK;
3664}
3665
3666/*
3667 * Check if "rettv" can have an [index] or [sli:ce]
3668 */
3669 int
3670check_can_index(typval_T *rettv, int evaluate, int verbose)
3671{
3672 switch (rettv->v_type)
3673 {
3674 case VAR_FUNC:
3675 case VAR_PARTIAL:
3676 if (verbose)
3677 emsg(_("E695: Cannot index a Funcref"));
3678 return FAIL;
3679 case VAR_FLOAT:
3680#ifdef FEAT_FLOAT
3681 if (verbose)
3682 emsg(_(e_float_as_string));
3683 return FAIL;
3684#endif
3685 case VAR_BOOL:
3686 case VAR_SPECIAL:
3687 case VAR_JOB:
3688 case VAR_CHANNEL:
3689 if (verbose)
3690 emsg(_(e_cannot_index_special_variable));
3691 return FAIL;
3692 case VAR_UNKNOWN:
3693 case VAR_ANY:
3694 case VAR_VOID:
3695 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003696 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003697 emsg(_(e_cannot_index_special_variable));
3698 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003699 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003700 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003701
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003702 case VAR_STRING:
3703 case VAR_LIST:
3704 case VAR_DICT:
3705 case VAR_BLOB:
3706 break;
3707 case VAR_NUMBER:
3708 if (in_vim9script())
3709 emsg(_(e_cannot_index_number));
3710 break;
3711 }
3712 return OK;
3713}
3714
3715/*
3716 * Apply index or range to "rettv".
3717 * "var1" is the first index, NULL for [:expr].
3718 * "var2" is the second index, NULL for [expr] and [expr: ]
3719 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
3720 */
3721 int
3722eval_index_inner(
3723 typval_T *rettv,
3724 int is_range,
3725 typval_T *var1,
3726 typval_T *var2,
3727 char_u *key,
3728 int keylen,
3729 int verbose)
3730{
3731 long n1, n2 = 0;
3732 long len;
3733
3734 n1 = 0;
3735 if (var1 != NULL && rettv->v_type != VAR_DICT)
3736 n1 = tv_get_number(var1);
3737
3738 if (is_range)
3739 {
3740 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003741 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003742 if (verbose)
3743 emsg(_(e_cannot_slice_dictionary));
3744 return FAIL;
3745 }
3746 if (var2 == NULL)
3747 n2 = -1;
3748 else
3749 n2 = tv_get_number(var2);
3750 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01003751
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003752 switch (rettv->v_type)
3753 {
3754 case VAR_UNKNOWN:
3755 case VAR_ANY:
3756 case VAR_VOID:
3757 case VAR_FUNC:
3758 case VAR_PARTIAL:
3759 case VAR_FLOAT:
3760 case VAR_BOOL:
3761 case VAR_SPECIAL:
3762 case VAR_JOB:
3763 case VAR_CHANNEL:
3764 break; // not evaluating, skipping over subscript
3765
3766 case VAR_NUMBER:
3767 case VAR_STRING:
3768 {
3769 char_u *s = tv_get_string(rettv);
3770
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003771 len = (long)STRLEN(s);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003772 if (in_vim9script())
3773 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003774 if (is_range)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003775 s = string_slice(s, n1, n2);
3776 else
3777 s = char_from_string(s, n1);
3778 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003779 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003780 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003781 // The resulting variable is a substring. If the indexes
3782 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003783 if (n1 < 0)
3784 {
3785 n1 = len + n1;
3786 if (n1 < 0)
3787 n1 = 0;
3788 }
3789 if (n2 < 0)
3790 n2 = len + n2;
3791 else if (n2 >= len)
3792 n2 = len;
3793 if (n1 >= len || n2 < 0 || n1 > n2)
3794 s = NULL;
3795 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02003796 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003797 }
3798 else
3799 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003800 // The resulting variable is a string of a single
3801 // character. If the index is too big or negative the
3802 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003803 if (n1 >= len || n1 < 0)
3804 s = NULL;
3805 else
3806 s = vim_strnsave(s + n1, 1);
3807 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003808 clear_tv(rettv);
3809 rettv->v_type = VAR_STRING;
3810 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003811 }
3812 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003813
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003814 case VAR_BLOB:
3815 len = blob_len(rettv->vval.v_blob);
3816 if (is_range)
3817 {
3818 // The resulting variable is a sub-blob. If the indexes
3819 // are out of range the result is empty.
3820 if (n1 < 0)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003821 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003822 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003823 if (n1 < 0)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003824 n1 = 0;
3825 }
3826 if (n2 < 0)
3827 n2 = len + n2;
3828 else if (n2 >= len)
3829 n2 = len - 1;
3830 if (n1 >= len || n2 < 0 || n1 > n2)
3831 {
3832 clear_tv(rettv);
3833 rettv->v_type = VAR_BLOB;
3834 rettv->vval.v_blob = NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003835 }
3836 else
3837 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003838 blob_T *blob = blob_alloc();
3839 long i;
3840
3841 if (blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003842 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003843 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003844 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003845 blob_free(blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003846 return FAIL;
3847 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003848 blob->bv_ga.ga_len = n2 - n1 + 1;
3849 for (i = n1; i <= n2; i++)
3850 blob_set(blob, i - n1,
3851 blob_get(rettv->vval.v_blob, i));
3852
3853 clear_tv(rettv);
3854 rettv_blob_set(rettv, blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003855 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003856 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003857 }
3858 else
3859 {
3860 // The resulting variable is a byte value.
3861 // If the index is too big or negative that is an error.
3862 if (n1 < 0)
3863 n1 = len + n1;
3864 if (n1 < len && n1 >= 0)
3865 {
3866 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003867
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003868 clear_tv(rettv);
3869 rettv->v_type = VAR_NUMBER;
3870 rettv->vval.v_number = v;
3871 }
3872 else
3873 semsg(_(e_blobidx), n1);
3874 }
3875 break;
3876
3877 case VAR_LIST:
3878 if (var1 == NULL)
3879 n1 = 0;
3880 if (var2 == NULL)
3881 n2 = -1;
3882 if (list_slice_or_index(rettv->vval.v_list,
3883 is_range, n1, n2, rettv, verbose) == FAIL)
3884 return FAIL;
3885 break;
3886
3887 case VAR_DICT:
3888 {
3889 dictitem_T *item;
3890 typval_T tmp;
3891
3892 if (key == NULL)
3893 {
3894 key = tv_get_string_chk(var1);
3895 if (key == NULL)
3896 return FAIL;
3897 }
3898
3899 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
3900
3901 if (item == NULL && verbose)
3902 semsg(_(e_dictkey), key);
3903 if (item == NULL)
3904 return FAIL;
3905
3906 copy_tv(&item->di_tv, &tmp);
3907 clear_tv(rettv);
3908 *rettv = tmp;
3909 }
3910 break;
3911 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003912 return OK;
3913}
3914
3915/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02003916 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003917 */
3918 char_u *
3919partial_name(partial_T *pt)
3920{
3921 if (pt->pt_name != NULL)
3922 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02003923 if (pt->pt_func != NULL)
3924 return pt->pt_func->uf_name;
3925 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003926}
3927
Bram Moolenaarddecc252016-04-06 22:59:37 +02003928 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003929partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003930{
3931 int i;
3932
3933 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003934 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003935 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003936 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003937 if (pt->pt_name != NULL)
3938 {
3939 func_unref(pt->pt_name);
3940 vim_free(pt->pt_name);
3941 }
3942 else
3943 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003944
3945 if (pt->pt_funcstack != NULL)
3946 {
3947 // Decrease the reference count for the context of a closure. If down
3948 // to zero free it and clear the variables on the stack.
3949 if (--pt->pt_funcstack->fs_refcount == 0)
3950 {
3951 garray_T *gap = &pt->pt_funcstack->fs_ga;
3952 typval_T *stack = gap->ga_data;
3953
3954 for (i = 0; i < gap->ga_len; ++i)
3955 clear_tv(stack + i);
3956 ga_clear(gap);
3957 vim_free(pt->pt_funcstack);
3958 }
3959 pt->pt_funcstack = NULL;
3960 }
3961
Bram Moolenaarddecc252016-04-06 22:59:37 +02003962 vim_free(pt);
3963}
3964
3965/*
3966 * Unreference a closure: decrement the reference count and free it when it
3967 * becomes zero.
3968 */
3969 void
3970partial_unref(partial_T *pt)
3971{
3972 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003973 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003974}
3975
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003976/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003977 * Return the next (unique) copy ID.
3978 * Used for serializing nested structures.
3979 */
3980 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003981get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003982{
3983 current_copyID += COPYID_INC;
3984 return current_copyID;
3985}
3986
3987/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003988 * Garbage collection for lists and dictionaries.
3989 *
3990 * We use reference counts to be able to free most items right away when they
3991 * are no longer used. But for composite items it's possible that it becomes
3992 * unused while the reference count is > 0: When there is a recursive
3993 * reference. Example:
3994 * :let l = [1, 2, 3]
3995 * :let d = {9: l}
3996 * :let l[1] = d
3997 *
3998 * Since this is quite unusual we handle this with garbage collection: every
3999 * once in a while find out which lists and dicts are not referenced from any
4000 * variable.
4001 *
4002 * Here is a good reference text about garbage collection (refers to Python
4003 * but it applies to all reference-counting mechanisms):
4004 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004005 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004006
4007/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004008 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004009 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004010 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004011 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004012 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004013garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004014{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004015 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004016 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004017 buf_T *buf;
4018 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004019 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004020 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004021
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004022 if (!testing)
4023 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004024 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004025 want_garbage_collect = FALSE;
4026 may_garbage_collect = FALSE;
4027 garbage_collect_at_exit = FALSE;
4028 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004029
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004030 // The execution stack can grow big, limit the size.
4031 if (exestack.ga_maxlen - exestack.ga_len > 500)
4032 {
4033 size_t new_len;
4034 char_u *pp;
4035 int n;
4036
4037 // Keep 150% of the current size, with a minimum of the growth size.
4038 n = exestack.ga_len / 2;
4039 if (n < exestack.ga_growsize)
4040 n = exestack.ga_growsize;
4041
4042 // Don't make it bigger though.
4043 if (exestack.ga_len + n < exestack.ga_maxlen)
4044 {
4045 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4046 pp = vim_realloc(exestack.ga_data, new_len);
4047 if (pp == NULL)
4048 return FAIL;
4049 exestack.ga_maxlen = exestack.ga_len + n;
4050 exestack.ga_data = pp;
4051 }
4052 }
4053
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004054 // We advance by two because we add one for items referenced through
4055 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004056 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004057
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004058 /*
4059 * 1. Go through all accessible variables and mark all lists and dicts
4060 * with copyID.
4061 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004062
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004063 // Don't free variables in the previous_funccal list unless they are only
4064 // referenced through previous_funccal. This must be first, because if
4065 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004066 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004067
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004068 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004069 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004070
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004071 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004072 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004073 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4074 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004075
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004076 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004077 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004078 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4079 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004080 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004081 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4082 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004083#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004084 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004085 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4086 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004087 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004088 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004089 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4090 NULL, NULL);
4091#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004092
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004093 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004094 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004095 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4096 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004097 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004098 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004099
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004100 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004101 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004102
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004103 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004104 abort = abort || set_ref_in_functions(copyID);
4105
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004106 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004107 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004108
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004109 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004110 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004111
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004112 // callbacks in buffers
4113 abort = abort || set_ref_in_buffers(copyID);
4114
Bram Moolenaar1dced572012-04-05 16:54:08 +02004115#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004116 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004117#endif
4118
Bram Moolenaardb913952012-06-29 12:54:53 +02004119#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004120 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004121#endif
4122
4123#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004124 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004125#endif
4126
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004127#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004128 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004129 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004130#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004131#ifdef FEAT_NETBEANS_INTG
4132 abort = abort || set_ref_in_nb_channel(copyID);
4133#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004134
Bram Moolenaare3188e22016-05-31 21:13:04 +02004135#ifdef FEAT_TIMERS
4136 abort = abort || set_ref_in_timer(copyID);
4137#endif
4138
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004139#ifdef FEAT_QUICKFIX
4140 abort = abort || set_ref_in_quickfix(copyID);
4141#endif
4142
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004143#ifdef FEAT_TERMINAL
4144 abort = abort || set_ref_in_term(copyID);
4145#endif
4146
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004147#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004148 abort = abort || set_ref_in_popups(copyID);
4149#endif
4150
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004151 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004152 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004153 /*
4154 * 2. Free lists and dictionaries that are not referenced.
4155 */
4156 did_free = free_unref_items(copyID);
4157
4158 /*
4159 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004160 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004161 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004162 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004163 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004164 else if (p_verbose > 0)
4165 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004166 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004167 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004168
4169 return did_free;
4170}
4171
4172/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004173 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004174 */
4175 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004176free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004177{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004178 int did_free = FALSE;
4179
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004180 // Let all "free" functions know that we are here. This means no
4181 // dictionaries, lists, channels or jobs are to be freed, because we will
4182 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004183 in_free_unref_items = TRUE;
4184
4185 /*
4186 * PASS 1: free the contents of the items. We don't free the items
4187 * themselves yet, so that it is possible to decrement refcount counters
4188 */
4189
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004190 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004191 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004192
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004193 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004194 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004195
4196#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004197 // Go through the list of jobs and free items without the copyID. This
4198 // must happen before doing channels, because jobs refer to channels, but
4199 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004200 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4201
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004202 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004203 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4204#endif
4205
4206 /*
4207 * PASS 2: free the items themselves.
4208 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004209 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004210 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004211
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004212#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004213 // Go through the list of jobs and free items without the copyID. This
4214 // must happen before doing channels, because jobs refer to channels, but
4215 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004216 free_unused_jobs(copyID, COPYID_MASK);
4217
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004218 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004219 free_unused_channels(copyID, COPYID_MASK);
4220#endif
4221
4222 in_free_unref_items = FALSE;
4223
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004224 return did_free;
4225}
4226
4227/*
4228 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004229 * "list_stack" is used to add lists to be marked. Can be NULL.
4230 *
4231 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004232 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004233 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004234set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004235{
4236 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004237 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004238 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004239 hashtab_T *cur_ht;
4240 ht_stack_T *ht_stack = NULL;
4241 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004242
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004243 cur_ht = ht;
4244 for (;;)
4245 {
4246 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004247 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004248 // Mark each item in the hashtab. If the item contains a hashtab
4249 // it is added to ht_stack, if it contains a list it is added to
4250 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004251 todo = (int)cur_ht->ht_used;
4252 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4253 if (!HASHITEM_EMPTY(hi))
4254 {
4255 --todo;
4256 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4257 &ht_stack, list_stack);
4258 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004259 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004260
4261 if (ht_stack == NULL)
4262 break;
4263
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004264 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004265 cur_ht = ht_stack->ht;
4266 tempitem = ht_stack;
4267 ht_stack = ht_stack->prev;
4268 free(tempitem);
4269 }
4270
4271 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004272}
4273
4274/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004275 * Mark a dict and its items with "copyID".
4276 * Returns TRUE if setting references failed somehow.
4277 */
4278 int
4279set_ref_in_dict(dict_T *d, int copyID)
4280{
4281 if (d != NULL && d->dv_copyID != copyID)
4282 {
4283 d->dv_copyID = copyID;
4284 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4285 }
4286 return FALSE;
4287}
4288
4289/*
4290 * Mark a list and its items with "copyID".
4291 * Returns TRUE if setting references failed somehow.
4292 */
4293 int
4294set_ref_in_list(list_T *ll, int copyID)
4295{
4296 if (ll != NULL && ll->lv_copyID != copyID)
4297 {
4298 ll->lv_copyID = copyID;
4299 return set_ref_in_list_items(ll, copyID, NULL);
4300 }
4301 return FALSE;
4302}
4303
4304/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004305 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004306 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4307 *
4308 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004309 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004310 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004311set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004312{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004313 listitem_T *li;
4314 int abort = FALSE;
4315 list_T *cur_l;
4316 list_stack_T *list_stack = NULL;
4317 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004318
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004319 cur_l = l;
4320 for (;;)
4321 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004322 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004323 // Mark each item in the list. If the item contains a hashtab
4324 // it is added to ht_stack, if it contains a list it is added to
4325 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004326 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4327 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4328 ht_stack, &list_stack);
4329 if (list_stack == NULL)
4330 break;
4331
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004332 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004333 cur_l = list_stack->list;
4334 tempitem = list_stack;
4335 list_stack = list_stack->prev;
4336 free(tempitem);
4337 }
4338
4339 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004340}
4341
4342/*
4343 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004344 * "list_stack" is used to add lists to be marked. Can be NULL.
4345 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4346 *
4347 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004348 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004349 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004350set_ref_in_item(
4351 typval_T *tv,
4352 int copyID,
4353 ht_stack_T **ht_stack,
4354 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004355{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004356 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004357
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004358 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004359 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004360 dict_T *dd = tv->vval.v_dict;
4361
Bram Moolenaara03f2332016-02-06 18:09:59 +01004362 if (dd != NULL && dd->dv_copyID != copyID)
4363 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004364 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004365 dd->dv_copyID = copyID;
4366 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004367 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004368 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4369 }
4370 else
4371 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004372 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4373
Bram Moolenaara03f2332016-02-06 18:09:59 +01004374 if (newitem == NULL)
4375 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004376 else
4377 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004378 newitem->ht = &dd->dv_hashtab;
4379 newitem->prev = *ht_stack;
4380 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004381 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004382 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004383 }
4384 }
4385 else if (tv->v_type == VAR_LIST)
4386 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004387 list_T *ll = tv->vval.v_list;
4388
Bram Moolenaara03f2332016-02-06 18:09:59 +01004389 if (ll != NULL && ll->lv_copyID != copyID)
4390 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004391 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004392 ll->lv_copyID = copyID;
4393 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004394 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004395 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004396 }
4397 else
4398 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004399 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4400
Bram Moolenaara03f2332016-02-06 18:09:59 +01004401 if (newitem == NULL)
4402 abort = TRUE;
4403 else
4404 {
4405 newitem->list = ll;
4406 newitem->prev = *list_stack;
4407 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004408 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004409 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004410 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004411 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004412 else if (tv->v_type == VAR_FUNC)
4413 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004414 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004415 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004416 else if (tv->v_type == VAR_PARTIAL)
4417 {
4418 partial_T *pt = tv->vval.v_partial;
4419 int i;
4420
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004421 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004422 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004423 // Didn't see this partial yet.
4424 pt->pt_copyID = copyID;
4425
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004426 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004427
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004428 if (pt->pt_dict != NULL)
4429 {
4430 typval_T dtv;
4431
4432 dtv.v_type = VAR_DICT;
4433 dtv.vval.v_dict = pt->pt_dict;
4434 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4435 }
4436
4437 for (i = 0; i < pt->pt_argc; ++i)
4438 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4439 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004440 if (pt->pt_funcstack != NULL)
4441 {
4442 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4443
4444 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4445 abort = abort || set_ref_in_item(stack + i, copyID,
4446 ht_stack, list_stack);
4447 }
4448
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004449 }
4450 }
4451#ifdef FEAT_JOB_CHANNEL
4452 else if (tv->v_type == VAR_JOB)
4453 {
4454 job_T *job = tv->vval.v_job;
4455 typval_T dtv;
4456
4457 if (job != NULL && job->jv_copyID != copyID)
4458 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004459 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004460 if (job->jv_channel != NULL)
4461 {
4462 dtv.v_type = VAR_CHANNEL;
4463 dtv.vval.v_channel = job->jv_channel;
4464 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4465 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004466 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004467 {
4468 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004469 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004470 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4471 }
4472 }
4473 }
4474 else if (tv->v_type == VAR_CHANNEL)
4475 {
4476 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004477 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004478 typval_T dtv;
4479 jsonq_T *jq;
4480 cbq_T *cq;
4481
4482 if (ch != NULL && ch->ch_copyID != copyID)
4483 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004484 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004485 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004486 {
4487 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4488 jq = jq->jq_next)
4489 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4490 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4491 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004492 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004493 {
4494 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004495 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004496 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4497 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004498 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004499 {
4500 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004501 dtv.vval.v_partial =
4502 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004503 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4504 }
4505 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004506 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004507 {
4508 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004509 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004510 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4511 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004512 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004513 {
4514 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004515 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004516 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4517 }
4518 }
4519 }
4520#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004521 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004522}
4523
Bram Moolenaar8c711452005-01-14 21:53:12 +00004524/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004525 * Return a string with the string representation of a variable.
4526 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004527 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004528 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004529 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4530 * stings as "string()", otherwise does not put quotes around strings, as
4531 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004532 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4533 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004534 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004535 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004536 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004537echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004538 typval_T *tv,
4539 char_u **tofree,
4540 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004541 int copyID,
4542 int echo_style,
4543 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004544 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004545{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004546 static int recurse = 0;
4547 char_u *r = NULL;
4548
Bram Moolenaar33570922005-01-25 22:26:29 +00004549 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004550 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004551 if (!did_echo_string_emsg)
4552 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004553 // Only give this message once for a recursive call to avoid
4554 // flooding the user with errors. And stop iterating over lists
4555 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004556 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004557 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004558 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004559 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004560 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004561 }
4562 ++recurse;
4563
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004564 switch (tv->v_type)
4565 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004566 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004567 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004568 {
4569 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004570 r = tv->vval.v_string;
4571 if (r == NULL)
4572 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004573 }
4574 else
4575 {
4576 *tofree = string_quote(tv->vval.v_string, FALSE);
4577 r = *tofree;
4578 }
4579 break;
4580
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004581 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004582 if (echo_style)
4583 {
4584 *tofree = NULL;
4585 r = tv->vval.v_string;
4586 }
4587 else
4588 {
4589 *tofree = string_quote(tv->vval.v_string, TRUE);
4590 r = *tofree;
4591 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004592 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004593
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004594 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004595 {
4596 partial_T *pt = tv->vval.v_partial;
4597 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004598 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004599 garray_T ga;
4600 int i;
4601 char_u *tf;
4602
4603 ga_init2(&ga, 1, 100);
4604 ga_concat(&ga, (char_u *)"function(");
4605 if (fname != NULL)
4606 {
4607 ga_concat(&ga, fname);
4608 vim_free(fname);
4609 }
4610 if (pt != NULL && pt->pt_argc > 0)
4611 {
4612 ga_concat(&ga, (char_u *)", [");
4613 for (i = 0; i < pt->pt_argc; ++i)
4614 {
4615 if (i > 0)
4616 ga_concat(&ga, (char_u *)", ");
4617 ga_concat(&ga,
4618 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4619 vim_free(tf);
4620 }
4621 ga_concat(&ga, (char_u *)"]");
4622 }
4623 if (pt != NULL && pt->pt_dict != NULL)
4624 {
4625 typval_T dtv;
4626
4627 ga_concat(&ga, (char_u *)", ");
4628 dtv.v_type = VAR_DICT;
4629 dtv.vval.v_dict = pt->pt_dict;
4630 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4631 vim_free(tf);
4632 }
4633 ga_concat(&ga, (char_u *)")");
4634
4635 *tofree = ga.ga_data;
4636 r = *tofree;
4637 break;
4638 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004639
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004640 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004641 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004642 break;
4643
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004644 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004645 if (tv->vval.v_list == NULL)
4646 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004647 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004648 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004649 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004650 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004651 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4652 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004653 {
4654 *tofree = NULL;
4655 r = (char_u *)"[...]";
4656 }
4657 else
4658 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004659 int old_copyID = tv->vval.v_list->lv_copyID;
4660
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004661 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004662 *tofree = list2string(tv, copyID, restore_copyID);
4663 if (restore_copyID)
4664 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004665 r = *tofree;
4666 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004667 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004668
Bram Moolenaar8c711452005-01-14 21:53:12 +00004669 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004670 if (tv->vval.v_dict == NULL)
4671 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004672 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004673 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004674 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004675 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004676 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4677 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004678 {
4679 *tofree = NULL;
4680 r = (char_u *)"{...}";
4681 }
4682 else
4683 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004684 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004685
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004686 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004687 *tofree = dict2string(tv, copyID, restore_copyID);
4688 if (restore_copyID)
4689 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004690 r = *tofree;
4691 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004692 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004693
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004694 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004695 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004696 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004697 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004698 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004699 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004700 break;
4701
Bram Moolenaar835dc632016-02-07 14:27:38 +01004702 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004703 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004704 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004705 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004706 if (composite_val)
4707 {
4708 *tofree = string_quote(r, FALSE);
4709 r = *tofree;
4710 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004711 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004712
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004713 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004714#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004715 *tofree = NULL;
4716 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4717 r = numbuf;
4718 break;
4719#endif
4720
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004721 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004722 case VAR_SPECIAL:
4723 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004724 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004725 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004726 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004727
Bram Moolenaar8502c702014-06-17 12:51:16 +02004728 if (--recurse == 0)
4729 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004730 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004731}
4732
4733/*
4734 * Return a string with the string representation of a variable.
4735 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4736 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004737 * Does not put quotes around strings, as ":echo" displays values.
4738 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4739 * May return NULL.
4740 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004741 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004742echo_string(
4743 typval_T *tv,
4744 char_u **tofree,
4745 char_u *numbuf,
4746 int copyID)
4747{
4748 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4749}
4750
4751/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004752 * Return string "str" in ' quotes, doubling ' characters.
4753 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004754 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004755 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004756 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004757string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004758{
Bram Moolenaar33570922005-01-25 22:26:29 +00004759 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004760 char_u *p, *r, *s;
4761
Bram Moolenaar33570922005-01-25 22:26:29 +00004762 len = (function ? 13 : 3);
4763 if (str != NULL)
4764 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004765 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004766 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004767 if (*p == '\'')
4768 ++len;
4769 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004770 s = r = alloc(len);
4771 if (r != NULL)
4772 {
4773 if (function)
4774 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004775 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004776 r += 10;
4777 }
4778 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004779 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004780 if (str != NULL)
4781 for (p = str; *p != NUL; )
4782 {
4783 if (*p == '\'')
4784 *r++ = '\'';
4785 MB_COPY_CHAR(p, r);
4786 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004787 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004788 if (function)
4789 *r++ = ')';
4790 *r++ = NUL;
4791 }
4792 return s;
4793}
4794
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004795#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004796/*
4797 * Convert the string "text" to a floating point number.
4798 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4799 * this always uses a decimal point.
4800 * Returns the length of the text that was consumed.
4801 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004802 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004803string2float(
4804 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004805 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004806{
4807 char *s = (char *)text;
4808 float_T f;
4809
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004810 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004811 if (STRNICMP(text, "inf", 3) == 0)
4812 {
4813 *value = INFINITY;
4814 return 3;
4815 }
4816 if (STRNICMP(text, "-inf", 3) == 0)
4817 {
4818 *value = -INFINITY;
4819 return 4;
4820 }
4821 if (STRNICMP(text, "nan", 3) == 0)
4822 {
4823 *value = NAN;
4824 return 3;
4825 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004826 f = strtod(s, &s);
4827 *value = f;
4828 return (int)((char_u *)s - text);
4829}
4830#endif
4831
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004832/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004834 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004836 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004837var2fpos(
4838 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004839 int dollar_lnum, // TRUE when $ is last line
4840 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004842 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004844 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004846 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004847 if (varp->v_type == VAR_LIST)
4848 {
4849 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004850 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004851 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004852 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004853
4854 l = varp->vval.v_list;
4855 if (l == NULL)
4856 return NULL;
4857
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004858 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004859 pos.lnum = list_find_nr(l, 0L, &error);
4860 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004861 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004862 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004863
Bram Moolenaarec65d772020-08-20 22:29:12 +02004864 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004865 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004866 li = list_find(l, 1L);
4867 if (li != NULL && li->li_tv.v_type == VAR_STRING
4868 && li->li_tv.vval.v_string != NULL
4869 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02004870 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004871 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02004872 }
4873 else
4874 {
4875 pos.col = list_find_nr(l, 1L, &error);
4876 if (error)
4877 return NULL;
4878 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00004879
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004880 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004881 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004882 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004883 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004884
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004885 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004886 pos.coladd = list_find_nr(l, 2L, &error);
4887 if (error)
4888 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004889
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004890 return &pos;
4891 }
4892
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004893 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004894 if (name == NULL)
4895 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004896 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004898 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004899 {
4900 if (VIsual_active)
4901 return &VIsual;
4902 return &curwin->w_cursor;
4903 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004904 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004906 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4908 return NULL;
4909 return pp;
4910 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004911
Bram Moolenaara5525202006-03-02 22:52:09 +00004912 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004913
Bram Moolenaar477933c2007-07-17 14:32:23 +00004914 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004915 {
4916 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004917 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004918 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004919 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004920 // In silent Ex mode topline is zero, but that's not a valid line
4921 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004922 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004923 return &pos;
4924 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004925 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004926 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004927 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004928 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004929 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004930 return &pos;
4931 }
4932 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004933 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004935 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 {
4937 pos.lnum = curbuf->b_ml.ml_line_count;
4938 pos.col = 0;
4939 }
4940 else
4941 {
4942 pos.lnum = curwin->w_cursor.lnum;
4943 pos.col = (colnr_T)STRLEN(ml_get_curline());
4944 }
4945 return &pos;
4946 }
4947 return NULL;
4948}
4949
4950/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004951 * Convert list in "arg" into a position and optional file number.
4952 * When "fnump" is NULL there is no file number, only 3 items.
4953 * Note that the column is passed on as-is, the caller may want to decrement
4954 * it to use 1 for the first column.
4955 * Return FAIL when conversion is not possible, doesn't check the position for
4956 * validity.
4957 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004958 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004959list2fpos(
4960 typval_T *arg,
4961 pos_T *posp,
4962 int *fnump,
4963 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004964{
4965 list_T *l = arg->vval.v_list;
4966 long i = 0;
4967 long n;
4968
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004969 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
4970 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00004971 if (arg->v_type != VAR_LIST
4972 || l == NULL
4973 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02004974 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004975 return FAIL;
4976
4977 if (fnump != NULL)
4978 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004979 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004980 if (n < 0)
4981 return FAIL;
4982 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004983 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004984 *fnump = n;
4985 }
4986
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004987 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004988 if (n < 0)
4989 return FAIL;
4990 posp->lnum = n;
4991
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004992 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004993 if (n < 0)
4994 return FAIL;
4995 posp->col = n;
4996
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004997 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004998 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00004999 posp->coladd = 0;
5000 else
5001 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005002
Bram Moolenaar493c1782014-05-28 14:34:46 +02005003 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005004 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005005
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005006 return OK;
5007}
5008
5009/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 * Get the length of an environment variable name.
5011 * Advance "arg" to the first character after the name.
5012 * Return 0 for error.
5013 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005014 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005015get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016{
5017 char_u *p;
5018 int len;
5019
5020 for (p = *arg; vim_isIDc(*p); ++p)
5021 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005022 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 return 0;
5024
5025 len = (int)(p - *arg);
5026 *arg = p;
5027 return len;
5028}
5029
5030/*
5031 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005032 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 * Return 0 if something is wrong.
5034 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005035 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005036get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037{
5038 char_u *p;
5039 int len;
5040
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005041 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005043 {
5044 if (*p == ':')
5045 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005046 // "s:" is start of "s:var", but "n:" is not and can be used in
5047 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005048 len = (int)(p - *arg);
5049 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5050 || len > 1)
5051 break;
5052 }
5053 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005054 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 return 0;
5056
5057 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005058 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059
5060 return len;
5061}
5062
5063/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005064 * Get the length of the name of a variable or function.
5065 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005067 * Return -1 if curly braces expansion failed.
5068 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 * If the name contains 'magic' {}'s, expand them and return the
5070 * expanded name in an allocated string via 'alias' - caller must free.
5071 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005072 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005073get_name_len(
5074 char_u **arg,
5075 char_u **alias,
5076 int evaluate,
5077 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078{
5079 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 char_u *p;
5081 char_u *expr_start;
5082 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005084 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085
5086 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5087 && (*arg)[2] == (int)KE_SNR)
5088 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005089 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 *arg += 3;
5091 return get_id_len(arg) + 3;
5092 }
5093 len = eval_fname_script(*arg);
5094 if (len > 0)
5095 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005096 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 *arg += len;
5098 }
5099
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005101 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005103 p = find_name_end(*arg, &expr_start, &expr_end,
5104 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 if (expr_start != NULL)
5106 {
5107 char_u *temp_string;
5108
5109 if (!evaluate)
5110 {
5111 len += (int)(p - *arg);
5112 *arg = skipwhite(p);
5113 return len;
5114 }
5115
5116 /*
5117 * Include any <SID> etc in the expanded string:
5118 * Thus the -len here.
5119 */
5120 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5121 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005122 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 *alias = temp_string;
5124 *arg = skipwhite(p);
5125 return (int)STRLEN(temp_string);
5126 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127
5128 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005129 // Only give an error when there is something, otherwise it will be
5130 // reported at a higher level.
5131 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005132 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133
5134 return len;
5135}
5136
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005137/*
5138 * Find the end of a variable or function name, taking care of magic braces.
5139 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5140 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005141 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005142 * Return a pointer to just after the name. Equal to "arg" if there is no
5143 * valid name.
5144 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005145 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005146find_name_end(
5147 char_u *arg,
5148 char_u **expr_start,
5149 char_u **expr_end,
5150 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005152 int mb_nest = 0;
5153 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005155 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005156 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005158 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005160 *expr_start = NULL;
5161 *expr_end = NULL;
5162 }
5163
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005164 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005165 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5166 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005167 return arg;
5168
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005169 for (p = arg; *p != NUL
5170 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005171 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005172 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005173 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005174 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005175 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005176 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005177 if (*p == '\'')
5178 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005179 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005180 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005181 ;
5182 if (*p == NUL)
5183 break;
5184 }
5185 else if (*p == '"')
5186 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005187 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005188 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005189 if (*p == '\\' && p[1] != NUL)
5190 ++p;
5191 if (*p == NUL)
5192 break;
5193 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005194 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5195 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005196 // "s:" is start of "s:var", but "n:" is not and can be used in
5197 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005198 len = (int)(p - arg);
5199 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005200 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005201 break;
5202 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005203
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005204 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005206 if (*p == '[')
5207 ++br_nest;
5208 else if (*p == ']')
5209 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005211
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005212 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005214 if (*p == '{')
5215 {
5216 mb_nest++;
5217 if (expr_start != NULL && *expr_start == NULL)
5218 *expr_start = p;
5219 }
5220 else if (*p == '}')
5221 {
5222 mb_nest--;
5223 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5224 *expr_end = p;
5225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 }
5228
5229 return p;
5230}
5231
5232/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005233 * Expands out the 'magic' {}'s in a variable/function name.
5234 * Note that this can call itself recursively, to deal with
5235 * constructs like foo{bar}{baz}{bam}
5236 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5237 * "in_start" ^
5238 * "expr_start" ^
5239 * "expr_end" ^
5240 * "in_end" ^
5241 *
5242 * Returns a new allocated string, which the caller must free.
5243 * Returns NULL for failure.
5244 */
5245 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005246make_expanded_name(
5247 char_u *in_start,
5248 char_u *expr_start,
5249 char_u *expr_end,
5250 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005251{
5252 char_u c1;
5253 char_u *retval = NULL;
5254 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005255
5256 if (expr_end == NULL || in_end == NULL)
5257 return NULL;
5258 *expr_start = NUL;
5259 *expr_end = NUL;
5260 c1 = *in_end;
5261 *in_end = NUL;
5262
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005263 temp_result = eval_to_string(expr_start + 1, FALSE);
5264 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005265 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005266 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5267 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005268 if (retval != NULL)
5269 {
5270 STRCPY(retval, in_start);
5271 STRCAT(retval, temp_result);
5272 STRCAT(retval, expr_end + 1);
5273 }
5274 }
5275 vim_free(temp_result);
5276
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005277 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005278 *expr_start = '{';
5279 *expr_end = '}';
5280
5281 if (retval != NULL)
5282 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005283 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005284 if (expr_start != NULL)
5285 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005286 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005287 temp_result = make_expanded_name(retval, expr_start,
5288 expr_end, temp_result);
5289 vim_free(retval);
5290 retval = temp_result;
5291 }
5292 }
5293
5294 return retval;
5295}
5296
5297/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005299 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005301 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005302eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005304 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005305}
5306
5307/*
5308 * Return TRUE if character "c" can be used as the first character in a
5309 * variable or function name (excluding '{' and '}').
5310 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005311 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005312eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005313{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005314 return ASCII_ISALPHA(c) || c == '_';
5315}
5316
5317/*
5318 * Return TRUE if character "c" can be used as the first character of a
5319 * dictionary key.
5320 */
5321 int
5322eval_isdictc(int c)
5323{
5324 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325}
5326
5327/*
Bram Moolenaare3c37d82020-08-15 18:39:05 +02005328 * Return the character "str[index]" where "index" is the character index. If
5329 * "index" is out of range NULL is returned.
5330 */
5331 char_u *
5332char_from_string(char_u *str, varnumber_T index)
5333{
5334 size_t nbyte = 0;
5335 varnumber_T nchar = index;
5336 size_t slen;
5337
5338 if (str == NULL || index < 0)
5339 return NULL;
5340 slen = STRLEN(str);
5341 while (nchar > 0 && nbyte < slen)
5342 {
5343 nbyte += MB_CPTR2LEN(str + nbyte);
5344 --nchar;
5345 }
5346 if (nbyte >= slen)
5347 return NULL;
5348 return vim_strnsave(str + nbyte, MB_CPTR2LEN(str + nbyte));
5349}
5350
5351/*
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005352 * Get the byte index for character index "idx" in string "str" with length
5353 * "str_len".
5354 * If going over the end return "str_len".
5355 * If "idx" is negative count from the end, -1 is the last character.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005356 * When going over the start return -1.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005357 */
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005358 static long
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005359char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
5360{
5361 varnumber_T nchar = idx;
5362 size_t nbyte = 0;
5363
5364 if (nchar >= 0)
5365 {
5366 while (nchar > 0 && nbyte < str_len)
5367 {
5368 nbyte += MB_CPTR2LEN(str + nbyte);
5369 --nchar;
5370 }
5371 }
5372 else
5373 {
5374 nbyte = str_len;
5375 while (nchar < 0 && nbyte > 0)
5376 {
5377 --nbyte;
5378 nbyte -= mb_head_off(str, str + nbyte);
5379 ++nchar;
5380 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005381 if (nchar < 0)
5382 return -1;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005383 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005384 return (long)nbyte;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005385}
5386
5387/*
5388 * Return the slice "str[first:last]" using character indexes.
5389 * Return NULL when the result is empty.
5390 */
5391 char_u *
5392string_slice(char_u *str, varnumber_T first, varnumber_T last)
5393{
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005394 long start_byte, end_byte;
5395 size_t slen;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005396
5397 if (str == NULL)
5398 return NULL;
5399 slen = STRLEN(str);
5400 start_byte = char_idx2byte(str, slen, first);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005401 if (start_byte < 0)
5402 start_byte = 0; // first index very negative: use zero
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005403 if (last == -1)
Bram Moolenaar25660542020-08-28 16:38:11 +02005404 end_byte = (long)slen;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005405 else
5406 {
5407 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005408 if (end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005409 // end index is inclusive
5410 end_byte += MB_CPTR2LEN(str + end_byte);
5411 }
5412
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005413 if (start_byte >= (long)slen || end_byte <= start_byte)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005414 return NULL;
5415 return vim_strnsave(str + start_byte, end_byte - start_byte);
5416}
5417
5418/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005419 * Handle:
5420 * - expr[expr], expr[expr:expr] subscript
5421 * - ".name" lookup
5422 * - function call with Funcref variable: func(expr)
5423 * - method call: var->method()
5424 *
5425 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005426 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005427 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005428handle_subscript(
5429 char_u **arg,
5430 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005431 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005432 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005433{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005434 int evaluate = evalarg != NULL
5435 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005436 int ret = OK;
5437 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005438 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005439 int getnext;
5440 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005441
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005442 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005443 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005444 // When at the end of the line and ".name" or "->{" or "->X" follows in
5445 // the next line then consume the line break.
5446 p = eval_next_non_blank(*arg, evalarg, &getnext);
5447 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005448 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaar9d489562020-07-30 20:08:50 +02005449 || (p[0] == '-' && p[1] == '>'
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005450 && (p[2] == '{' || ASCII_ISALPHA(p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005451 {
5452 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005453 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005454 check_white = FALSE;
5455 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005456
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005457 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5458 || rettv->v_type == VAR_PARTIAL))
5459 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005460 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005461 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5462 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005463
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005464 // Stop the expression evaluation when immediately aborting on
5465 // error, or when an interrupt occurred or an exception was thrown
5466 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005467 if (aborting())
5468 {
5469 if (ret == OK)
5470 clear_tv(rettv);
5471 ret = FAIL;
5472 }
5473 dict_unref(selfdict);
5474 selfdict = NULL;
5475 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005476 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005477 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02005478 *arg = p;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005479 if (ret == OK)
5480 {
5481 if ((*arg)[2] == '{')
5482 // expr->{lambda}()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005483 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005484 else
5485 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005486 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005487 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005488 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005489 // "." is ".name" lookup when we found a dict or when evaluating and
5490 // scriptversion is at least 2, where string concatenation is "..".
5491 else if (**arg == '['
5492 || (**arg == '.' && (rettv->v_type == VAR_DICT
5493 || (!evaluate
5494 && (*arg)[1] != '.'
5495 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005496 {
5497 dict_unref(selfdict);
5498 if (rettv->v_type == VAR_DICT)
5499 {
5500 selfdict = rettv->vval.v_dict;
5501 if (selfdict != NULL)
5502 ++selfdict->dv_refcount;
5503 }
5504 else
5505 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005506 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005507 {
5508 clear_tv(rettv);
5509 ret = FAIL;
5510 }
5511 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005512 else
5513 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005514 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005515
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005516 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5517 // Don't do this when "Func" is already a partial that was bound
5518 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005519 if (selfdict != NULL
5520 && (rettv->v_type == VAR_FUNC
5521 || (rettv->v_type == VAR_PARTIAL
5522 && (rettv->vval.v_partial->pt_auto
5523 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005524 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005525
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005526 dict_unref(selfdict);
5527 return ret;
5528}
5529
5530/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005531 * Make a copy of an item.
5532 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005533 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5534 * reference to an already copied list/dict can be used.
5535 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005536 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005537 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005538item_copy(
5539 typval_T *from,
5540 typval_T *to,
5541 int deep,
5542 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005543{
5544 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005545 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005546
Bram Moolenaar33570922005-01-25 22:26:29 +00005547 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005548 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005549 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005550 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005551 }
5552 ++recurse;
5553
5554 switch (from->v_type)
5555 {
5556 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005557 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005558 case VAR_STRING:
5559 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005560 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005561 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005562 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005563 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005564 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005565 copy_tv(from, to);
5566 break;
5567 case VAR_LIST:
5568 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005569 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005570 if (from->vval.v_list == NULL)
5571 to->vval.v_list = NULL;
5572 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5573 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005574 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005575 to->vval.v_list = from->vval.v_list->lv_copylist;
5576 ++to->vval.v_list->lv_refcount;
5577 }
5578 else
5579 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5580 if (to->vval.v_list == NULL)
5581 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005582 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005583 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005584 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005585 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005586 case VAR_DICT:
5587 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005588 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005589 if (from->vval.v_dict == NULL)
5590 to->vval.v_dict = NULL;
5591 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5592 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005593 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005594 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5595 ++to->vval.v_dict->dv_refcount;
5596 }
5597 else
5598 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5599 if (to->vval.v_dict == NULL)
5600 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005601 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005602 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005603 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005604 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005605 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005606 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005607 }
5608 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005609 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005610}
5611
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005612 void
5613echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5614{
5615 char_u *tofree;
5616 char_u numbuf[NUMBUFLEN];
5617 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5618
5619 if (*atstart)
5620 {
5621 *atstart = FALSE;
5622 // Call msg_start() after eval1(), evaluating the expression
5623 // may cause a message to appear.
5624 if (with_space)
5625 {
5626 // Mark the saved text as finishing the line, so that what
5627 // follows is displayed on a new line when scrolling back
5628 // at the more prompt.
5629 msg_sb_eol();
5630 msg_start();
5631 }
5632 }
5633 else if (with_space)
5634 msg_puts_attr(" ", echo_attr);
5635
5636 if (p != NULL)
5637 for ( ; *p != NUL && !got_int; ++p)
5638 {
5639 if (*p == '\n' || *p == '\r' || *p == TAB)
5640 {
5641 if (*p != TAB && *needclr)
5642 {
5643 // remove any text still there from the command
5644 msg_clr_eos();
5645 *needclr = FALSE;
5646 }
5647 msg_putchar_attr(*p, echo_attr);
5648 }
5649 else
5650 {
5651 if (has_mbyte)
5652 {
5653 int i = (*mb_ptr2len)(p);
5654
5655 (void)msg_outtrans_len_attr(p, i, echo_attr);
5656 p += i - 1;
5657 }
5658 else
5659 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5660 }
5661 }
5662 vim_free(tofree);
5663}
5664
Bram Moolenaare9a41262005-01-15 22:18:47 +00005665/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 * ":echo expr1 ..." print each argument separated with a space, add a
5667 * newline at the end.
5668 * ":echon expr1 ..." print each argument plain.
5669 */
5670 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005671ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672{
5673 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005674 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 char_u *p;
5676 int needclr = TRUE;
5677 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01005678 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005679 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005680 evalarg_T evalarg;
5681
Bram Moolenaare707c882020-07-01 13:07:37 +02005682 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683
5684 if (eap->skip)
5685 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005686 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005688 // If eval1() causes an error message the text from the command may
5689 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005690 need_clr_eos = needclr;
5691
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005693 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 {
5695 /*
5696 * Report the invalid expression unless the expression evaluation
5697 * has been cancelled due to an aborting error, an interrupt, or an
5698 * exception.
5699 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005700 if (!aborting() && did_emsg == did_emsg_before
5701 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005702 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005703 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 break;
5705 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005706 need_clr_eos = FALSE;
5707
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005709 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005710
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005711 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 arg = skipwhite(arg);
5713 }
5714 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02005715 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716
5717 if (eap->skip)
5718 --emsg_skip;
5719 else
5720 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005721 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 if (needclr)
5723 msg_clr_eos();
5724 if (eap->cmdidx == CMD_echo)
5725 msg_end();
5726 }
5727}
5728
5729/*
5730 * ":echohl {name}".
5731 */
5732 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005733ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005735 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736}
5737
5738/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005739 * Returns the :echo attribute
5740 */
5741 int
5742get_echo_attr(void)
5743{
5744 return echo_attr;
5745}
5746
5747/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 * ":execute expr1 ..." execute the result of an expression.
5749 * ":echomsg expr1 ..." Print a message
5750 * ":echoerr expr1 ..." Print an error
5751 * Each gets spaces around each argument and a newline at the end for
5752 * echo commands
5753 */
5754 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005755ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756{
5757 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005758 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 int ret = OK;
5760 char_u *p;
5761 garray_T ga;
5762 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763
5764 ga_init2(&ga, 1, 80);
5765
5766 if (eap->skip)
5767 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02005768 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02005770 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01005771 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773
5774 if (!eap->skip)
5775 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005776 char_u buf[NUMBUFLEN];
5777
5778 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01005779 {
5780 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
5781 {
5782 emsg(_(e_inval_string));
5783 p = NULL;
5784 }
5785 else
5786 p = tv_get_string_buf(&rettv, buf);
5787 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005788 else
5789 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005790 if (p == NULL)
5791 {
5792 clear_tv(&rettv);
5793 ret = FAIL;
5794 break;
5795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 len = (int)STRLEN(p);
5797 if (ga_grow(&ga, len + 2) == FAIL)
5798 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005799 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 ret = FAIL;
5801 break;
5802 }
5803 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 ga.ga_len += len;
5807 }
5808
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005809 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 arg = skipwhite(arg);
5811 }
5812
5813 if (ret != FAIL && ga.ga_data != NULL)
5814 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005815 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
5816 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005817 // Mark the already saved text as finishing the line, so that what
5818 // follows is displayed on a new line when scrolling back at the
5819 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005820 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005821 }
5822
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005824 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005825 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005826 out_flush();
5827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 else if (eap->cmdidx == CMD_echoerr)
5829 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005830 int save_did_emsg = did_emsg;
5831
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005832 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005833 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 if (!force_abort)
5835 did_emsg = save_did_emsg;
5836 }
5837 else if (eap->cmdidx == CMD_execute)
5838 do_cmdline((char_u *)ga.ga_data,
5839 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
5840 }
5841
5842 ga_clear(&ga);
5843
5844 if (eap->skip)
5845 --emsg_skip;
5846
5847 eap->nextcmd = check_nextcmd(arg);
5848}
5849
5850/*
5851 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
5852 * "arg" points to the "&" or '+' when called, to "option" when returning.
5853 * Returns NULL when no option name found. Otherwise pointer to the char
5854 * after the option name.
5855 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005856 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005857find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858{
5859 char_u *p = *arg;
5860
5861 ++p;
5862 if (*p == 'g' && p[1] == ':')
5863 {
5864 *opt_flags = OPT_GLOBAL;
5865 p += 2;
5866 }
5867 else if (*p == 'l' && p[1] == ':')
5868 {
5869 *opt_flags = OPT_LOCAL;
5870 p += 2;
5871 }
5872 else
5873 *opt_flags = 0;
5874
5875 if (!ASCII_ISALPHA(*p))
5876 return NULL;
5877 *arg = p;
5878
5879 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005880 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 else
5882 while (ASCII_ISALPHA(*p))
5883 ++p;
5884 return p;
5885}
5886
5887/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00005888 * Display script name where an item was last set.
5889 * Should only be invoked when 'verbose' is non-zero.
5890 */
5891 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005892last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005893{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005894 char_u *p;
5895
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005896 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005897 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005898 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005899 if (p != NULL)
5900 {
5901 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01005902 msg_puts(_("\n\tLast set from "));
5903 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005904 if (script_ctx.sc_lnum > 0)
5905 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01005906 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005907 msg_outnum((long)script_ctx.sc_lnum);
5908 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005909 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005910 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005911 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00005912 }
5913}
5914
Bram Moolenaarb005cd82019-09-04 15:54:55 +02005915#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916
5917/*
5918 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005919 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 * "flags" can be "g" to do a global substitute.
5921 * Returns an allocated string, NULL for error.
5922 */
5923 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005924do_string_sub(
5925 char_u *str,
5926 char_u *pat,
5927 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005928 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01005929 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930{
5931 int sublen;
5932 regmatch_T regmatch;
5933 int i;
5934 int do_all;
5935 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005936 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 garray_T ga;
5938 char_u *ret;
5939 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005940 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005942 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005944 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945
5946 ga_init2(&ga, 1, 200);
5947
5948 do_all = (flags[0] == 'g');
5949
5950 regmatch.rm_ic = p_ic;
5951 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
5952 if (regmatch.regprog != NULL)
5953 {
5954 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005955 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
5957 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005958 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01005959 if (regmatch.startp[0] == regmatch.endp[0])
5960 {
5961 if (zero_width == regmatch.startp[0])
5962 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005963 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02005964 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02005965 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
5966 (size_t)i);
5967 ga.ga_len += i;
5968 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005969 continue;
5970 }
5971 zero_width = regmatch.startp[0];
5972 }
5973
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 /*
5975 * Get some space for a temporary buffer to do the substitution
5976 * into. It will contain:
5977 * - The text up to where the match is.
5978 * - The substituted text.
5979 * - The text after the match.
5980 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005981 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01005982 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
5984 {
5985 ga_clear(&ga);
5986 break;
5987 }
5988
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005989 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 i = (int)(regmatch.startp[0] - tail);
5991 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005992 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005993 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 + ga.ga_len + i, TRUE, TRUE, FALSE);
5995 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02005996 tail = regmatch.endp[0];
5997 if (*tail == NUL)
5998 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 if (!do_all)
6000 break;
6001 }
6002
6003 if (ga.ga_data != NULL)
6004 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6005
Bram Moolenaar473de612013-06-08 18:19:48 +02006006 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 }
6008
6009 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6010 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006011 if (p_cpo == empty_option)
6012 p_cpo = save_cpo;
6013 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006014 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006015 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016
6017 return ret;
6018}