blob: c80013960a30ff7819996d53576d43dd2ed3cda6 [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 Moolenaard155d7a2018-12-21 16:04:21 +0100329 res = (tv_get_number_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 Moolenaare40fbc22020-06-27 18:06:45 +0200398 garray_T *gap = &evalarg->eval_ga;
399 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
400
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200401 if (vim9script
402 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200403 {
404 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200405 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200406 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200407 ++gap->ga_len;
408 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200409 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200410
411 // Don't evaluate the expression.
412 if (evalarg != NULL)
413 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200414 *arg = skipwhite(*arg);
415 res = eval1(arg, &rettv, evalarg);
416 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200417 if (evalarg != NULL)
418 evalarg->eval_flags = save_flags;
419
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200420 if (vim9script
421 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200422 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200423 if (evalarg->eval_ga.ga_len == 1)
424 {
425 // just one line, no need to concatenate
426 ga_clear(gap);
427 gap->ga_itemsize = 0;
428 }
429 else
430 {
431 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200432 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200433
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200434 // Line breaks encountered, concatenate all the lines.
435 *((char_u **)gap->ga_data) = *start;
436 p = ga_concat_strings(gap, "");
437
438 // free the lines only when using getsourceline()
439 if (evalarg->eval_cookie != NULL)
440 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200441 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200442 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200443 // Do not free the last line, "arg" points into it, free it
444 // later.
445 vim_free(evalarg->eval_tofree);
446 evalarg->eval_tofree =
447 ((char_u **)gap->ga_data)[gap->ga_len - 1];
448 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200449 ga_clear_strings(gap);
450 }
451 else
452 ga_clear(gap);
453 gap->ga_itemsize = 0;
454 if (p == NULL)
455 return FAIL;
456 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200457 vim_free(evalarg->eval_tofree_lambda);
458 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200459 // Compute "end" relative to the end.
460 *end = *start + STRLEN(*start) - endoff;
461 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200462 }
463
464 return res;
465}
466
467/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200468 * Top level evaluation function, returning a string. Does not handle line
469 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000470 * When "convert" is TRUE convert a List into a sequence of lines and convert
471 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472 * Return pointer to allocated memory, or NULL for failure.
473 */
474 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100475eval_to_string(
476 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100477 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478{
Bram Moolenaar33570922005-01-25 22:26:29 +0000479 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000481 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000482#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000483 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000484#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200486 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 retval = NULL;
488 else
489 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000490 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000491 {
492 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000493 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200494 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200495 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200496 if (tv.vval.v_list->lv_len > 0)
497 ga_append(&ga, NL);
498 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000499 ga_append(&ga, NUL);
500 retval = (char_u *)ga.ga_data;
501 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000502#ifdef FEAT_FLOAT
503 else if (convert && tv.v_type == VAR_FLOAT)
504 {
505 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
506 retval = vim_strsave(numbuf);
507 }
508#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000509 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100510 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000511 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 }
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200513 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514
515 return retval;
516}
517
518/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000519 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200520 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200521 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 */
523 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100524eval_to_string_safe(
525 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100526 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527{
528 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200529 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200530 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200532 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200533 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000534 if (use_sandbox)
535 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200536 ++textwinlock;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200537 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000538 if (use_sandbox)
539 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200540 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200541 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200542 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 return retval;
544}
545
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546/*
547 * Top level evaluation function, returning a number.
548 * Evaluates "expr" silently.
549 * Returns -1 for an error.
550 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200551 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100552eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553{
Bram Moolenaar33570922005-01-25 22:26:29 +0000554 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200555 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000556 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557
558 ++emsg_off;
559
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200560 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 retval = -1;
562 else
563 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100564 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000565 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 }
567 --emsg_off;
568
569 return retval;
570}
571
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000572/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000573 * Top level evaluation function.
574 * Returns an allocated typval_T with the result.
575 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000576 */
577 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200578eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000579{
580 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200581 evalarg_T evalarg;
582
583 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000584
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200585 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200586 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100587 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000588
Bram Moolenaar37c83712020-06-30 21:18:36 +0200589 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000590 return tv;
591}
592
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100594 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200595 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
596 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000597 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200599 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100600call_vim_function(
601 char_u *func,
602 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200603 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200604 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000606 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200607 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100609 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200610 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200611 funcexe.firstline = curwin->w_cursor.lnum;
612 funcexe.lastline = curwin->w_cursor.lnum;
613 funcexe.evaluate = TRUE;
614 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000615 if (ret == FAIL)
616 clear_tv(rettv);
617
618 return ret;
619}
620
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100621/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100622 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100623 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200624 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
625 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100626 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200627 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100628call_func_retnr(
629 char_u *func,
630 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200631 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100632{
633 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200634 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100635
Bram Moolenaarded27a12018-08-01 19:06:03 +0200636 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100637 return -1;
638
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100639 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100640 clear_tv(&rettv);
641 return retval;
642}
643
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000644/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100645 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000646 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200647 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
648 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000649 */
650 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100651call_func_retstr(
652 char_u *func,
653 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200654 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000655{
656 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000657 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000658
Bram Moolenaarded27a12018-08-01 19:06:03 +0200659 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000660 return NULL;
661
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100662 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000663 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 return retval;
665}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000666
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000667/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100668 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200669 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
670 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000671 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000672 */
673 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100674call_func_retlist(
675 char_u *func,
676 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200677 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000678{
679 typval_T rettv;
680
Bram Moolenaarded27a12018-08-01 19:06:03 +0200681 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000682 return NULL;
683
684 if (rettv.v_type != VAR_LIST)
685 {
686 clear_tv(&rettv);
687 return NULL;
688 }
689
690 return rettv.vval.v_list;
691}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693#ifdef FEAT_FOLDING
694/*
695 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
696 * it in "*cp". Doesn't give error messages.
697 */
698 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100699eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700{
Bram Moolenaar33570922005-01-25 22:26:29 +0000701 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200702 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000704 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
705 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706
707 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000708 if (use_sandbox)
709 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200710 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200712 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 retval = 0;
714 else
715 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100716 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000717 if (tv.v_type == VAR_NUMBER)
718 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000719 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 retval = 0;
721 else
722 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100723 // If the result is a string, check if there is a non-digit before
724 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000725 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 if (!VIM_ISDIGIT(*s) && *s != '-')
727 *cp = *s++;
728 retval = atol((char *)s);
729 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000730 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 }
732 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000733 if (use_sandbox)
734 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200735 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200736 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200738 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739}
740#endif
741
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000743 * Get an lval: variable, Dict item or List item that can be assigned a value
744 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
745 * "name.key", "name.key[expr]" etc.
746 * Indexing only works if "name" is an existing List or Dictionary.
747 * "name" points to the start of the name.
748 * If "rettv" is not NULL it points to the value to be assigned.
749 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
750 * wrong; must end in space or cmd separator.
751 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100752 * flags:
753 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100754 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100755 * GLV_NO_AUTOLOAD: do not use script autoloading
756 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000757 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000758 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000759 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000760 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200761 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100762get_lval(
763 char_u *name,
764 typval_T *rettv,
765 lval_T *lp,
766 int unlet,
767 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100768 int flags, // GLV_ values
769 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000770{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000771 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000772 char_u *expr_start, *expr_end;
773 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000774 dictitem_T *v;
775 typval_T var1;
776 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000777 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000778 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000779 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000780 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +0000781 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100782 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000783
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100784 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200785 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000786
787 if (skip)
788 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100789 // When skipping just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000790 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200791 lp->ll_name_end = find_name_end(name, NULL, NULL,
792 FNE_INCL_BR | fne_flags);
793 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000794 }
795
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100796 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000797 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100798 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000799 if (expr_start != NULL)
800 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100801 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100802 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000803 && *p != '[' && *p != '.')
804 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200805 semsg(_(e_trailing_arg), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000806 return NULL;
807 }
808
809 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
810 if (lp->ll_exp_name == NULL)
811 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100812 // Report an invalid expression in braces, unless the
813 // expression evaluation has been cancelled due to an
814 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000815 if (!aborting() && !quiet)
816 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000817 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100818 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000819 return NULL;
820 }
821 }
822 lp->ll_name = lp->ll_exp_name;
823 }
824 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100825 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000826 lp->ll_name = name;
827
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200828 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100829 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200830 // "a: type" is declaring variable "a" with a type, not "a:".
831 if (p == name + 2 && p[-1] == ':')
832 {
833 --p;
834 lp->ll_name_end = p;
835 }
836 if (*p == ':')
837 {
838 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
839 char_u *tp = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100840
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200841 // parse the type after the name
842 lp->ll_type = parse_type(&tp, &si->sn_type_list);
843 lp->ll_name_end = tp;
844 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100845 }
846 }
847
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100848 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000849 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
850 return p;
851
852 cc = *p;
853 *p = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100854 // Only pass &ht when we would write to the variable, it prevents autoload
855 // as well.
Bram Moolenaar6e65d592017-12-07 22:11:27 +0100856 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
857 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000858 if (v == NULL && !quiet)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200859 semsg(_(e_undefined_variable_str), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000860 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000861 if (v == NULL)
862 return NULL;
863
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000864 /*
865 * Loop until no more [idx] or .key is following.
866 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000867 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100868 var1.v_type = VAR_UNKNOWN;
869 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000870 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000871 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000872 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
873 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100874 && lp->ll_tv->vval.v_dict != NULL)
875 && !(lp->ll_tv->v_type == VAR_BLOB
876 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000877 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000878 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100879 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000880 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000881 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000882 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000883 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000884 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100885 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000886 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000887 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000888
Bram Moolenaar8c711452005-01-14 21:53:12 +0000889 len = -1;
890 if (*p == '.')
891 {
892 key = p + 1;
893 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
894 ;
895 if (len == 0)
896 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000897 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100898 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000899 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000900 }
901 p = key + len;
902 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000903 else
904 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100905 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000906 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000907 if (*p == ':')
908 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000909 else
910 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000911 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200912 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000913 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100914 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000915 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100916 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000917 clear_tv(&var1);
918 return NULL;
919 }
Bram Moolenaar6a250262020-08-04 15:53:01 +0200920 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000921 }
922
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100923 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000924 if (*p == ':')
925 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000926 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000927 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000928 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +0200929 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100930 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000931 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000932 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100933 if (rettv != NULL
934 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100935 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100936 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100937 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +0000938 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000939 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100940 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100941 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000942 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000943 }
944 p = skipwhite(p + 1);
945 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000946 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000947 else
948 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000949 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200950 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200951 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000952 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100953 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000954 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000955 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100956 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000957 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100958 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100959 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000960 clear_tv(&var2);
961 return NULL;
962 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000963 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000964 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000965 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000966 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000967 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000968
Bram Moolenaar8c711452005-01-14 21:53:12 +0000969 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000970 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000971 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100972 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100973 clear_tv(&var1);
974 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000975 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000976 }
977
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100978 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000979 ++p;
980 }
981
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000982 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000983 {
984 if (len == -1)
985 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100986 // "[key]": get key from "var1"
987 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +0200988 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000989 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000990 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000991 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000992 }
993 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000994 lp->ll_list = NULL;
995 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000996 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200997
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100998 // When assigning to a scope dictionary check that a function and
999 // variable name is valid (only variable name unless it is l: or
1000 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001001 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001002 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001003 int prevval;
1004 int wrong;
1005
1006 if (len != -1)
1007 {
1008 prevval = key[len];
1009 key[len] = NUL;
1010 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001011 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001012 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001013 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1014 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001015 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001016 || !valid_varname(key);
1017 if (len != -1)
1018 key[len] = prevval;
1019 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001020 {
1021 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001022 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001023 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001024 }
1025
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001026 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001027 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001028 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001029 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001030 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001031 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001032 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001033 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001034 return NULL;
1035 }
1036
Bram Moolenaar31b81602019-02-10 22:14:27 +01001037 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001038 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001039 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001040 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001041 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001042 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001043 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001044 }
1045 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001046 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001047 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001048 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001049 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001050 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001051 p = NULL;
1052 break;
1053 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001054 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001055 else if ((flags & GLV_READ_ONLY) == 0
1056 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001057 {
1058 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001059 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001060 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001061
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001062 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001063 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001064 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001065 else if (lp->ll_tv->v_type == VAR_BLOB)
1066 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001067 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1068
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001069 /*
1070 * Get the number and item for the only or first index of the List.
1071 */
1072 if (empty1)
1073 lp->ll_n1 = 0;
1074 else
1075 // is number or string
1076 lp->ll_n1 = (long)tv_get_number(&var1);
1077 clear_tv(&var1);
1078
1079 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001080 || lp->ll_n1 > bloblen
1081 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001082 {
1083 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001084 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001085 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001086 return NULL;
1087 }
1088 if (lp->ll_range && !lp->ll_empty2)
1089 {
1090 lp->ll_n2 = (long)tv_get_number(&var2);
1091 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001092 if (lp->ll_n2 < 0
1093 || lp->ll_n2 >= bloblen
1094 || lp->ll_n2 < lp->ll_n1)
1095 {
1096 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001097 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001098 return NULL;
1099 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001100 }
1101 lp->ll_blob = lp->ll_tv->vval.v_blob;
1102 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001103 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001104 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001105 else
1106 {
1107 /*
1108 * Get the number and item for the only or first index of the List.
1109 */
1110 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001111 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001112 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001113 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001114 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001115 clear_tv(&var1);
1116
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001117 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001118 lp->ll_list = lp->ll_tv->vval.v_list;
1119 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1120 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001121 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001122 if (lp->ll_n1 < 0)
1123 {
1124 lp->ll_n1 = 0;
1125 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1126 }
1127 }
1128 if (lp->ll_li == NULL)
1129 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001130 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001131 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001132 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001133 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001134 }
1135
1136 /*
1137 * May need to find the item or absolute index for the second
1138 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001139 * When no index given: "lp->ll_empty2" is TRUE.
1140 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001141 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001142 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001143 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001144 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001145 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001146 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001147 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001148 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001149 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001150 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001151 {
1152 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001153 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001154 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001155 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001156 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001157 }
1158
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001159 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001160 if (lp->ll_n1 < 0)
1161 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1162 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001163 {
1164 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001165 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001166 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001167 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001168 }
1169
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001170 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001171 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001172 }
1173
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001174 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001175 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001176 return p;
1177}
1178
1179/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001180 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001181 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001182 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001183clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001184{
1185 vim_free(lp->ll_exp_name);
1186 vim_free(lp->ll_newkey);
1187}
1188
1189/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001190 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001191 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001192 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1193 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001194 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001195 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001196set_var_lval(
1197 lval_T *lp,
1198 char_u *endp,
1199 typval_T *rettv,
1200 int copy,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001201 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar7454a062016-01-30 15:14:10 +01001202 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001203{
1204 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001205 listitem_T *ri;
1206 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001207
1208 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001209 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001210 cc = *endp;
1211 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001212 if (lp->ll_blob != NULL)
1213 {
1214 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001215
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001216 if (op != NULL && *op != '=')
1217 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001218 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001219 return;
1220 }
Bram Moolenaar021bda52020-08-17 21:07:22 +02001221 if (var_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
1222 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001223
1224 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1225 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001226 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001227
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001228 if (lp->ll_empty2)
1229 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1230
1231 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001232 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001233 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001234 return;
1235 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001236 if (lp->ll_empty2)
1237 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001238
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001239 ir = 0;
1240 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1241 blob_set(lp->ll_blob, il,
1242 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001243 }
1244 else
1245 {
1246 val = (int)tv_get_number_chk(rettv, &error);
1247 if (!error)
1248 {
1249 garray_T *gap = &lp->ll_blob->bv_ga;
1250
1251 // Allow for appending a byte. Setting a byte beyond
1252 // the end is an error otherwise.
1253 if (lp->ll_n1 < gap->ga_len
1254 || (lp->ll_n1 == gap->ga_len
1255 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1256 {
1257 blob_set(lp->ll_blob, lp->ll_n1, val);
1258 if (lp->ll_n1 == gap->ga_len)
1259 ++gap->ga_len;
1260 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001261 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001262 }
1263 }
1264 }
1265 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001266 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001267 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001268
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001269 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001270 {
1271 emsg(_(e_cannot_mod));
1272 *endp = cc;
1273 return;
1274 }
1275
Bram Moolenaarff697e62019-02-12 22:28:33 +01001276 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001277 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001278 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar79518e22017-02-17 16:31:35 +01001279 &tv, &di, TRUE, FALSE) == OK)
1280 {
1281 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001282 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1283 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001284 && tv_op(&tv, rettv, op) == OK)
1285 set_var(lp->ll_name, &tv, FALSE);
1286 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001287 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001288 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001289 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001290 {
1291 if (lp->ll_type != NULL
1292 && check_typval_type(lp->ll_type, rettv) == FAIL)
1293 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001294 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001295 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001296 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001297 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001298 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001299 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001300 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001301 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001302 else if (lp->ll_range)
1303 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001304 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001305 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001306
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001307 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001308 {
1309 emsg(_("E996: Cannot lock a range"));
1310 return;
1311 }
1312
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001313 /*
1314 * Check whether any of the list items is locked
1315 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001316 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001317 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001318 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001319 return;
1320 ri = ri->li_next;
1321 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1322 break;
1323 ll_li = ll_li->li_next;
1324 ++ll_n1;
1325 }
1326
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001327 /*
1328 * Assign the List values to the list items.
1329 */
1330 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001331 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001332 if (op != NULL && *op != '=')
1333 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1334 else
1335 {
1336 clear_tv(&lp->ll_li->li_tv);
1337 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1338 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001339 ri = ri->li_next;
1340 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1341 break;
1342 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001343 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001344 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001345 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001346 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001347 ri = NULL;
1348 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001349 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001350 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001351 lp->ll_li = lp->ll_li->li_next;
1352 ++lp->ll_n1;
1353 }
1354 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001355 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001356 else if (lp->ll_empty2
1357 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001358 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001359 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001360 }
1361 else
1362 {
1363 /*
1364 * Assign to a List or Dictionary item.
1365 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001366 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001367 {
1368 emsg(_("E996: Cannot lock a list or dict"));
1369 return;
1370 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001371 if (lp->ll_newkey != NULL)
1372 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001373 if (op != NULL && *op != '=')
1374 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001375 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001376 return;
1377 }
1378
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001379 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001380 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001381 if (di == NULL)
1382 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001383 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1384 {
1385 vim_free(di);
1386 return;
1387 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001388 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001389 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001390 else if (op != NULL && *op != '=')
1391 {
1392 tv_op(lp->ll_tv, rettv, op);
1393 return;
1394 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001395 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001396 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001397
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001398 /*
1399 * Assign the value to the variable or list item.
1400 */
1401 if (copy)
1402 copy_tv(rettv, lp->ll_tv);
1403 else
1404 {
1405 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001406 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001407 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001408 }
1409 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001410}
1411
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001412/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001413 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1414 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001415 * Returns OK or FAIL.
1416 */
1417 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001418tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001419{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001420 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001421 char_u numbuf[NUMBUFLEN];
1422 char_u *s;
1423
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001424 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001425 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001426 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001427 {
1428 switch (tv1->v_type)
1429 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001430 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001431 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001432 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001433 case VAR_DICT:
1434 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001435 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001436 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001437 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001438 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001439 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001440 break;
1441
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001442 case VAR_BLOB:
1443 if (*op != '+' || tv2->v_type != VAR_BLOB)
1444 break;
1445 // BLOB += BLOB
1446 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1447 {
1448 blob_T *b1 = tv1->vval.v_blob;
1449 blob_T *b2 = tv2->vval.v_blob;
1450 int i, len = blob_len(b2);
1451 for (i = 0; i < len; i++)
1452 ga_append(&b1->bv_ga, blob_get(b2, i));
1453 }
1454 return OK;
1455
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001456 case VAR_LIST:
1457 if (*op != '+' || tv2->v_type != VAR_LIST)
1458 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001459 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001460 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
1461 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1462 return OK;
1463
1464 case VAR_NUMBER:
1465 case VAR_STRING:
1466 if (tv2->v_type == VAR_LIST)
1467 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001468 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001469 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001470 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001471 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001472#ifdef FEAT_FLOAT
1473 if (tv2->v_type == VAR_FLOAT)
1474 {
1475 float_T f = n;
1476
Bram Moolenaarff697e62019-02-12 22:28:33 +01001477 if (*op == '%')
1478 break;
1479 switch (*op)
1480 {
1481 case '+': f += tv2->vval.v_float; break;
1482 case '-': f -= tv2->vval.v_float; break;
1483 case '*': f *= tv2->vval.v_float; break;
1484 case '/': f /= tv2->vval.v_float; break;
1485 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001486 clear_tv(tv1);
1487 tv1->v_type = VAR_FLOAT;
1488 tv1->vval.v_float = f;
1489 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001490 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001491#endif
1492 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001493 switch (*op)
1494 {
1495 case '+': n += tv_get_number(tv2); break;
1496 case '-': n -= tv_get_number(tv2); break;
1497 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001498 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1499 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001500 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001501 clear_tv(tv1);
1502 tv1->v_type = VAR_NUMBER;
1503 tv1->vval.v_number = n;
1504 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001505 }
1506 else
1507 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001508 if (tv2->v_type == VAR_FLOAT)
1509 break;
1510
Bram Moolenaarff697e62019-02-12 22:28:33 +01001511 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001512 s = tv_get_string(tv1);
1513 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001514 clear_tv(tv1);
1515 tv1->v_type = VAR_STRING;
1516 tv1->vval.v_string = s;
1517 }
1518 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001519
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001520 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001521#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001522 {
1523 float_T f;
1524
Bram Moolenaarff697e62019-02-12 22:28:33 +01001525 if (*op == '%' || *op == '.'
1526 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001527 && tv2->v_type != VAR_NUMBER
1528 && tv2->v_type != VAR_STRING))
1529 break;
1530 if (tv2->v_type == VAR_FLOAT)
1531 f = tv2->vval.v_float;
1532 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001533 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001534 switch (*op)
1535 {
1536 case '+': tv1->vval.v_float += f; break;
1537 case '-': tv1->vval.v_float -= f; break;
1538 case '*': tv1->vval.v_float *= f; break;
1539 case '/': tv1->vval.v_float /= f; break;
1540 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001541 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001542#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001543 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001544 }
1545 }
1546
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001547 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001548 return FAIL;
1549}
1550
1551/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001552 * Evaluate the expression used in a ":for var in expr" command.
1553 * "arg" points to "var".
1554 * Set "*errp" to TRUE for an error, FALSE otherwise;
1555 * Return a pointer that holds the info. Null when there is an error.
1556 */
1557 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001558eval_for_line(
1559 char_u *arg,
1560 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001561 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001562 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001563{
Bram Moolenaar33570922005-01-25 22:26:29 +00001564 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001565 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001566 typval_T tv;
1567 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001568 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001569
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001570 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001571
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001572 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001573 if (fi == NULL)
1574 return NULL;
1575
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001576 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001577 if (expr == NULL)
1578 return fi;
1579
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001580 expr = skipwhite_and_linebreak(expr, evalarg);
1581 if (expr[0] != 'i' || expr[1] != 'n'
1582 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001583 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001584 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001585 return fi;
1586 }
1587
1588 if (skip)
1589 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001590 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1591 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001592 {
1593 *errp = FALSE;
1594 if (!skip)
1595 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001596 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001597 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001598 l = tv.vval.v_list;
1599 if (l == NULL)
1600 {
1601 // a null list is like an empty list: do nothing
1602 clear_tv(&tv);
1603 }
1604 else
1605 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001606 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001607 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001608
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001609 // No need to increment the refcount, it's already set for
1610 // the list being used in "tv".
1611 fi->fi_list = l;
1612 list_add_watch(l, &fi->fi_lw);
1613 fi->fi_lw.lw_item = l->lv_first;
1614 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001615 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001616 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001617 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001618 fi->fi_bi = 0;
1619 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001620 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001621 typval_T btv;
1622
1623 // Make a copy, so that the iteration still works when the
1624 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001625 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001626 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001627 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001628 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001629 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001630 else
1631 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001632 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001633 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001634 }
1635 }
1636 }
1637 if (skip)
1638 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001639 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001640
1641 return fi;
1642}
1643
1644/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001645 * Used when looping over a :for line, skip the "in expr" part.
1646 */
1647 void
1648skip_for_lines(void *fi_void, evalarg_T *evalarg)
1649{
1650 forinfo_T *fi = (forinfo_T *)fi_void;
1651 int i;
1652
1653 for (i = 0; i < fi->fi_break_count; ++i)
1654 eval_next_line(evalarg);
1655}
1656
1657/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001658 * Use the first item in a ":for" list. Advance to the next.
1659 * Assign the values to the variable (list). "arg" points to the first one.
1660 * Return TRUE when a valid item was found, FALSE when at end of list or
1661 * something wrong.
1662 */
1663 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001664next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001665{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001666 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001667 int result;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001668 int flag = in_vim9script() ? LET_NO_COMMAND : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001669 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001670
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001671 if (fi->fi_blob != NULL)
1672 {
1673 typval_T tv;
1674
1675 if (fi->fi_bi >= blob_len(fi->fi_blob))
1676 return FALSE;
1677 tv.v_type = VAR_NUMBER;
1678 tv.v_lock = VAR_FIXED;
1679 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1680 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001681 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001682 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001683 }
1684
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001685 item = fi->fi_lw.lw_item;
1686 if (item == NULL)
1687 result = FALSE;
1688 else
1689 {
1690 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001691 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001692 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001693 }
1694 return result;
1695}
1696
1697/*
1698 * Free the structure used to store info used by ":for".
1699 */
1700 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001701free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001702{
Bram Moolenaar33570922005-01-25 22:26:29 +00001703 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001704
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001705 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001706 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001707 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001708 list_unref(fi->fi_list);
1709 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001710 if (fi != NULL && fi->fi_blob != NULL)
1711 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001712 vim_free(fi);
1713}
1714
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001716set_context_for_expression(
1717 expand_T *xp,
1718 char_u *arg,
1719 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720{
1721 int got_eq = FALSE;
1722 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001723 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001725 if (cmdidx == CMD_let || cmdidx == CMD_const)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001726 {
1727 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001728 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001729 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001730 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001731 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001732 {
1733 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001734 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001735 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001736 break;
1737 }
1738 return;
1739 }
1740 }
1741 else
1742 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1743 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 while ((xp->xp_pattern = vim_strpbrk(arg,
1745 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1746 {
1747 c = *xp->xp_pattern;
1748 if (c == '&')
1749 {
1750 c = xp->xp_pattern[1];
1751 if (c == '&')
1752 {
1753 ++xp->xp_pattern;
1754 xp->xp_context = cmdidx != CMD_let || got_eq
1755 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1756 }
1757 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001758 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001760 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1761 xp->xp_pattern += 2;
1762
1763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 }
1765 else if (c == '$')
1766 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001767 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 xp->xp_context = EXPAND_ENV_VARS;
1769 }
1770 else if (c == '=')
1771 {
1772 got_eq = TRUE;
1773 xp->xp_context = EXPAND_EXPRESSION;
1774 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001775 else if (c == '#'
1776 && xp->xp_context == EXPAND_EXPRESSION)
1777 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001778 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001779 break;
1780 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001781 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 && xp->xp_context == EXPAND_FUNCTIONS
1783 && vim_strchr(xp->xp_pattern, '(') == NULL)
1784 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001785 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 break;
1787 }
1788 else if (cmdidx != CMD_let || got_eq)
1789 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001790 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 {
1792 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1793 if (c == '\\' && xp->xp_pattern[1] != NUL)
1794 ++xp->xp_pattern;
1795 xp->xp_context = EXPAND_NOTHING;
1796 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001797 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001799 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1801 /* skip */ ;
1802 xp->xp_context = EXPAND_NOTHING;
1803 }
1804 else if (c == '|')
1805 {
1806 if (xp->xp_pattern[1] == '|')
1807 {
1808 ++xp->xp_pattern;
1809 xp->xp_context = EXPAND_EXPRESSION;
1810 }
1811 else
1812 xp->xp_context = EXPAND_COMMANDS;
1813 }
1814 else
1815 xp->xp_context = EXPAND_EXPRESSION;
1816 }
1817 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001818 // Doesn't look like something valid, expand as an expression
1819 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001820 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 arg = xp->xp_pattern;
1822 if (*arg != NUL)
1823 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1824 /* skip */ ;
1825 }
1826 xp->xp_pattern = arg;
1827}
1828
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001830 * Return TRUE if "pat" matches "text".
1831 * Does not use 'cpo' and always uses 'magic'.
1832 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001833 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001834pattern_match(char_u *pat, char_u *text, int ic)
1835{
1836 int matches = FALSE;
1837 char_u *save_cpo;
1838 regmatch_T regmatch;
1839
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001840 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001841 save_cpo = p_cpo;
1842 p_cpo = (char_u *)"";
1843 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1844 if (regmatch.regprog != NULL)
1845 {
1846 regmatch.rm_ic = ic;
1847 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1848 vim_regfree(regmatch.regprog);
1849 }
1850 p_cpo = save_cpo;
1851 return matches;
1852}
1853
1854/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001855 * Handle a name followed by "(". Both for just "name(arg)" and for
1856 * "expr->name(arg)".
1857 * Returns OK or FAIL.
1858 */
1859 static int
1860eval_func(
1861 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02001862 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001863 char_u *name,
1864 int name_len,
1865 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001866 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001867 typval_T *basetv) // "expr" for "expr->name(arg)"
1868{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001869 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001870 char_u *s = name;
1871 int len = name_len;
1872 partial_T *partial;
1873 int ret = OK;
1874
1875 if (!evaluate)
1876 check_vars(s, len);
1877
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001878 // If "s" is the name of a variable of type VAR_FUNC
1879 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001880 s = deref_func_name(s, &len, &partial, !evaluate);
1881
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001882 // Need to make a copy, in case evaluating the arguments makes
1883 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001884 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001885 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001886 ret = FAIL;
1887 else
1888 {
1889 funcexe_T funcexe;
1890
1891 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001892 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001893 funcexe.firstline = curwin->w_cursor.lnum;
1894 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001895 funcexe.evaluate = evaluate;
1896 funcexe.partial = partial;
1897 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02001898 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001899 }
1900 vim_free(s);
1901
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001902 // If evaluate is FALSE rettv->v_type was not set in
1903 // get_func_tv, but it's needed in handle_subscript() to parse
1904 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001905 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
1906 {
1907 rettv->vval.v_string = NULL;
1908 rettv->v_type = VAR_FUNC;
1909 }
1910
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001911 // Stop the expression evaluation when immediately
1912 // aborting on error, or when an interrupt occurred or
1913 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001914 if (evaluate && aborting())
1915 {
1916 if (ret == OK)
1917 clear_tv(rettv);
1918 ret = FAIL;
1919 }
1920 return ret;
1921}
1922
1923/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02001924 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
1925 * comment) and there is a next line, return the next line (skipping blanks)
1926 * and set "getnext".
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001927 * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
1928 * "arg" must point somewhere inside a line, not at the start.
1929 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001930 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001931eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
1932{
Bram Moolenaar9d489562020-07-30 20:08:50 +02001933 char_u *p = skipwhite(arg);
1934
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001935 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001936 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001937 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001938 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaar9d489562020-07-30 20:08:50 +02001939 && (*p == NUL || (VIM_ISWHITE(p[-1]) && vim9_comment_start(p))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001940 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02001941 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001942
1943 if (evalarg->eval_cookie != NULL)
Bram Moolenaar9d489562020-07-30 20:08:50 +02001944 next = getline_peek(evalarg->eval_getline, evalarg->eval_cookie);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001945 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02001946 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001947
Bram Moolenaar9d489562020-07-30 20:08:50 +02001948 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001949 {
1950 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02001951 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001952 }
1953 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02001954 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001955}
1956
1957/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001958 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001959 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001960 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001961eval_next_line(evalarg_T *evalarg)
1962{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001963 garray_T *gap = &evalarg->eval_ga;
1964 char_u *line;
1965
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02001966 if (evalarg->eval_cookie != NULL)
1967 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0, TRUE);
1968 else
1969 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001970 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001971 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
1972 {
1973 // Going to concatenate the lines after parsing.
1974 ((char_u **)gap->ga_data)[gap->ga_len] = line;
1975 ++gap->ga_len;
1976 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001977 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001978 {
1979 vim_free(evalarg->eval_tofree);
1980 evalarg->eval_tofree = line;
1981 }
1982 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001983}
1984
Bram Moolenaare6b53242020-07-01 17:28:33 +02001985/*
1986 * Call eval_next_non_blank() and get the next line if needed.
1987 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02001988 char_u *
1989skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
1990{
1991 int getnext;
1992 char_u *p = skipwhite(arg);
1993
Bram Moolenaar962d7212020-07-04 14:15:00 +02001994 if (evalarg == NULL)
1995 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02001996 eval_next_non_blank(p, evalarg, &getnext);
1997 if (getnext)
1998 return eval_next_line(evalarg);
1999 return p;
2000}
2001
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002002/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002003 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002004 */
2005 void
2006clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2007{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002008 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002009 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002010 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002011 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002012 if (eap != NULL)
2013 {
2014 // We may need to keep the original command line, e.g. for
2015 // ":let" it has the variable names. But we may also need the
2016 // new one, "nextcmd" points into it. Keep both.
2017 vim_free(eap->cmdline_tofree);
2018 eap->cmdline_tofree = *eap->cmdlinep;
2019 *eap->cmdlinep = evalarg->eval_tofree;
2020 }
2021 else
2022 vim_free(evalarg->eval_tofree);
2023 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002024 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002025
2026 vim_free(evalarg->eval_tofree_lambda);
2027 evalarg->eval_tofree_lambda = NULL;
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002028 }
2029}
2030
2031/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002033 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2035 */
2036
2037/*
2038 * Handle zero level expression.
2039 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002040 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002041 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002042 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 * Return OK or FAIL.
2044 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002045 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002046eval0(
2047 char_u *arg,
2048 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002049 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002050 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051{
2052 int ret;
2053 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002054 int did_emsg_before = did_emsg;
2055 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002056 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057
2058 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002059 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002060 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002061
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002062 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 {
2064 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002065 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 /*
2067 * Report the invalid expression unless the expression evaluation has
2068 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002069 * exception, or we already gave a more specific error.
2070 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002072 if (!aborting()
2073 && did_emsg == did_emsg_before
2074 && called_emsg == called_emsg_before
2075 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002076 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 ret = FAIL;
2078 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002079
2080 if (eap != NULL)
2081 eap->nextcmd = check_nextcmd(p);
2082
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 return ret;
2084}
2085
2086/*
2087 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002088 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 *
2090 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002091 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002093 * Note: "rettv.v_lock" is not set.
2094 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 * Return OK or FAIL.
2096 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002097 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002098eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002100 char_u *p;
2101 int getnext;
2102
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 /*
2104 * Get the first variable.
2105 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002106 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 return FAIL;
2108
Bram Moolenaar793648f2020-06-26 21:28:25 +02002109 p = eval_next_non_blank(*arg, evalarg, &getnext);
2110 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002112 int result;
2113 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002114 evalarg_T *evalarg_used = evalarg;
2115 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002116 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002117 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002118
2119 if (evalarg == NULL)
2120 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002121 CLEAR_FIELD(local_evalarg);
2122 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002123 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002124 orig_flags = evalarg_used->eval_flags;
2125 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002126
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002127 if (getnext)
2128 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002129 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002130 {
2131 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2132 {
2133 error_white_both(p, 1);
2134 clear_tv(rettv);
2135 return FAIL;
2136 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002137 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002138 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002139
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002141 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002143 int error = FALSE;
2144
Bram Moolenaar56acb092020-08-16 14:48:19 +02002145 if (in_vim9script())
2146 result = tv2bool(rettv);
2147 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002149 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002150 if (error)
2151 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 }
2153
2154 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002155 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 */
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002157 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
2158 {
2159 error_white_both(p, 1);
2160 clear_tv(rettv);
2161 return FAIL;
2162 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002163 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2164 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002165 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002166 if (eval1(arg, rettv, evalarg_used) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 return FAIL;
2168
2169 /*
2170 * Check for the ":".
2171 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002172 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar793648f2020-06-26 21:28:25 +02002173 if (*p != ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002175 emsg(_(e_missing_colon));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002177 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 return FAIL;
2179 }
Bram Moolenaar793648f2020-06-26 21:28:25 +02002180 if (getnext)
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002181 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002182 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002183 {
2184 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2185 {
2186 error_white_both(p, 1);
2187 clear_tv(rettv);
2188 return FAIL;
2189 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002190 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192
2193 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002194 * Get the third variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 */
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002196 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
2197 {
2198 error_white_both(p, 1);
2199 clear_tv(rettv);
2200 return FAIL;
2201 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002202 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2203 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002204 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002205 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 {
2207 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002208 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 return FAIL;
2210 }
2211 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002212 *rettv = var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002213
2214 if (evalarg == NULL)
2215 clear_evalarg(&local_evalarg, NULL);
2216 else
2217 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 }
2219
2220 return OK;
2221}
2222
2223/*
2224 * Handle first level expression:
2225 * expr2 || expr2 || expr2 logical OR
2226 *
2227 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002228 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 *
2230 * Return OK or FAIL.
2231 */
2232 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002233eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002235 char_u *p;
2236 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237
2238 /*
2239 * Get the first variable.
2240 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002241 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 return FAIL;
2243
2244 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002245 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002247 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002248 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002250 evalarg_T *evalarg_used = evalarg;
2251 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002252 int evaluate;
2253 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002254 long result = FALSE;
2255 typval_T var2;
2256 int error;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002257 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002258
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002259 if (evalarg == NULL)
2260 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002261 CLEAR_FIELD(local_evalarg);
2262 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002263 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002264 orig_flags = evalarg_used->eval_flags;
2265 evaluate = orig_flags & EVAL_EVALUATE;
2266 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002267 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002268 if (vim9script)
2269 {
2270 result = tv2bool(rettv);
2271 }
2272 else
2273 {
2274 error = FALSE;
2275 if (tv_get_number_chk(rettv, &error) != 0)
2276 result = TRUE;
2277 clear_tv(rettv);
2278 if (error)
2279 return FAIL;
2280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 }
2282
2283 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002284 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002286 while (p[0] == '|' && p[1] == '|')
2287 {
2288 if (getnext)
2289 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002290 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002291 {
2292 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2293 {
2294 error_white_both(p, 2);
2295 clear_tv(rettv);
2296 return FAIL;
2297 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002298 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002299 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002300
2301 /*
2302 * Get the second variable.
2303 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002304 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2305 {
2306 error_white_both(p, 2);
2307 clear_tv(rettv);
2308 return FAIL;
2309 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002310 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2311 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002312 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002313 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002314 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002315
2316 /*
2317 * Compute the result.
2318 */
2319 if (evaluate && !result)
2320 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002321 if (vim9script)
2322 {
2323 clear_tv(rettv);
2324 *rettv = var2;
2325 result = tv2bool(rettv);
2326 }
2327 else
2328 {
2329 if (tv_get_number_chk(&var2, &error) != 0)
2330 result = TRUE;
2331 clear_tv(&var2);
2332 if (error)
2333 return FAIL;
2334 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002335 }
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002336 if (evaluate && !vim9script)
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002337 {
2338 rettv->v_type = VAR_NUMBER;
2339 rettv->vval.v_number = result;
2340 }
2341
2342 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002344
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002345 if (evalarg == NULL)
2346 clear_evalarg(&local_evalarg, NULL);
2347 else
2348 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 }
2350
2351 return OK;
2352}
2353
2354/*
2355 * Handle second level expression:
2356 * expr3 && expr3 && expr3 logical AND
2357 *
2358 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002359 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 *
2361 * Return OK or FAIL.
2362 */
2363 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002364eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002366 char_u *p;
2367 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368
2369 /*
2370 * Get the first variable.
2371 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002372 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 return FAIL;
2374
2375 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002376 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002378 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002379 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002381 evalarg_T *evalarg_used = evalarg;
2382 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002383 int orig_flags;
2384 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002385 long result = TRUE;
2386 typval_T var2;
2387 int error;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002388 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002389
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002390 if (evalarg == NULL)
2391 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002392 CLEAR_FIELD(local_evalarg);
2393 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002394 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002395 orig_flags = evalarg_used->eval_flags;
2396 evaluate = orig_flags & EVAL_EVALUATE;
2397 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002398 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002399 if (vim9script)
2400 {
2401 result = tv2bool(rettv);
2402 }
2403 else
2404 {
2405 error = FALSE;
2406 if (tv_get_number_chk(rettv, &error) == 0)
2407 result = FALSE;
2408 clear_tv(rettv);
2409 if (error)
2410 return FAIL;
2411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 }
2413
2414 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002415 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002417 while (p[0] == '&' && p[1] == '&')
2418 {
2419 if (getnext)
2420 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002421 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002422 {
2423 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2424 {
2425 error_white_both(p, 2);
2426 clear_tv(rettv);
2427 return FAIL;
2428 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002429 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002430 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002431
2432 /*
2433 * Get the second variable.
2434 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002435 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2436 {
2437 error_white_both(p, 2);
2438 clear_tv(rettv);
2439 return FAIL;
2440 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002441 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2442 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002443 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002444 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002445 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002446
2447 /*
2448 * Compute the result.
2449 */
2450 if (evaluate && result)
2451 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002452 if (vim9script)
2453 {
2454 clear_tv(rettv);
2455 *rettv = var2;
2456 result = tv2bool(rettv);
2457 }
2458 else
2459 {
2460 if (tv_get_number_chk(&var2, &error) == 0)
2461 result = FALSE;
2462 clear_tv(&var2);
2463 if (error)
2464 return FAIL;
2465 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002466 }
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002467 if (evaluate && !vim9script)
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002468 {
2469 rettv->v_type = VAR_NUMBER;
2470 rettv->vval.v_number = result;
2471 }
2472
2473 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002475
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002476 if (evalarg == NULL)
2477 clear_evalarg(&local_evalarg, NULL);
2478 else
2479 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 }
2481
2482 return OK;
2483}
2484
2485/*
2486 * Handle third level expression:
2487 * var1 == var2
2488 * var1 =~ var2
2489 * var1 != var2
2490 * var1 !~ var2
2491 * var1 > var2
2492 * var1 >= var2
2493 * var1 < var2
2494 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002495 * var1 is var2
2496 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 *
2498 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002499 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 *
2501 * Return OK or FAIL.
2502 */
2503 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002504eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002507 int getnext;
Bram Moolenaar87396072019-12-31 22:36:18 +01002508 exptype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002510 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511
2512 /*
2513 * Get the first variable.
2514 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002515 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 return FAIL;
2517
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002518 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002519 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520
2521 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002522 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002524 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002526 typval_T var2;
2527 int ic;
2528 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002529 int evaluate = evalarg == NULL
2530 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002531
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002532 if (getnext)
2533 *arg = eval_next_line(evalarg);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002534 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2535 {
2536 error_white_both(p, len);
2537 clear_tv(rettv);
2538 return FAIL;
2539 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002540
Bram Moolenaar696ba232020-07-29 21:20:41 +02002541 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2542 {
2543 semsg(_(e_invexpr2), p);
2544 clear_tv(rettv);
2545 return FAIL;
2546 }
2547
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002548 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 if (p[len] == '?')
2550 {
2551 ic = TRUE;
2552 ++len;
2553 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002554 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 else if (p[len] == '#')
2556 {
2557 ic = FALSE;
2558 ++len;
2559 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002560 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002562 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563
2564 /*
2565 * Get the second variable.
2566 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002567 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2568 {
2569 error_white_both(p, 1);
2570 clear_tv(rettv);
2571 return FAIL;
2572 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002573 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002574 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002576 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 return FAIL;
2578 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002579 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002580 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002581 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002582
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002583 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002584 {
2585 ret = FAIL;
2586 clear_tv(rettv);
2587 }
2588 else
2589 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002590 clear_tv(&var2);
2591 return ret;
2592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 }
2594
2595 return OK;
2596}
2597
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002598 void
2599eval_addblob(typval_T *tv1, typval_T *tv2)
2600{
2601 blob_T *b1 = tv1->vval.v_blob;
2602 blob_T *b2 = tv2->vval.v_blob;
2603 blob_T *b = blob_alloc();
2604 int i;
2605
2606 if (b != NULL)
2607 {
2608 for (i = 0; i < blob_len(b1); i++)
2609 ga_append(&b->bv_ga, blob_get(b1, i));
2610 for (i = 0; i < blob_len(b2); i++)
2611 ga_append(&b->bv_ga, blob_get(b2, i));
2612
2613 clear_tv(tv1);
2614 rettv_blob_set(tv1, b);
2615 }
2616}
2617
2618 int
2619eval_addlist(typval_T *tv1, typval_T *tv2)
2620{
2621 typval_T var3;
2622
2623 // concatenate Lists
2624 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2625 {
2626 clear_tv(tv1);
2627 clear_tv(tv2);
2628 return FAIL;
2629 }
2630 clear_tv(tv1);
2631 *tv1 = var3;
2632 return OK;
2633}
2634
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635/*
2636 * Handle fourth level expression:
2637 * + number addition
2638 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002639 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002640 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 *
2642 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002643 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 *
2645 * Return OK or FAIL.
2646 */
2647 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002648eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 /*
2651 * Get the first variable.
2652 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002653 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 return FAIL;
2655
2656 /*
2657 * Repeat computing, until no '+', '-' or '.' is following.
2658 */
2659 for (;;)
2660 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002661 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002662 int getnext;
2663 char_u *p;
2664 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002665 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002666 int concat;
2667 typval_T var2;
2668
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002669 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002670 p = eval_next_non_blank(*arg, evalarg, &getnext);
2671 op = *p;
2672 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002673 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002675
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002676 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002677 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002678 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002679 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002680 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002681 {
2682 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
2683 {
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002684 error_white_both(p, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002685 clear_tv(rettv);
2686 return FAIL;
2687 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002688 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002689 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002690 if ((op != '+' || (rettv->v_type != VAR_LIST
2691 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002692#ifdef FEAT_FLOAT
2693 && (op == '.' || rettv->v_type != VAR_FLOAT)
2694#endif
2695 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002696 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002697 // For "list + ...", an illegal use of the first operand as
2698 // a number cannot be determined before evaluating the 2nd
2699 // operand: if this is also a list, all is ok.
2700 // For "something . ...", "something - ..." or "non-list + ...",
2701 // we know that the first operand needs to be a string or number
2702 // without evaluating the 2nd operand. So check before to avoid
2703 // side effects after an error.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002704 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002705 {
2706 clear_tv(rettv);
2707 return FAIL;
2708 }
2709 }
2710
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 /*
2712 * Get the second variable.
2713 */
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002714 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[oplen]))
2715 {
2716 error_white_both(p, oplen);
2717 clear_tv(rettv);
2718 return FAIL;
2719 }
2720 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002721 if (eval6(arg, &var2, evalarg, !in_vim9script() && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002723 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 return FAIL;
2725 }
2726
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002727 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 {
2729 /*
2730 * Compute the result.
2731 */
2732 if (op == '.')
2733 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002734 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2735 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002736 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002737
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002738 if (in_vim9script() && (var2.v_type == VAR_VOID
2739 || var2.v_type == VAR_CHANNEL
2740 || var2.v_type == VAR_JOB))
2741 emsg(_(e_inval_string));
2742#ifdef FEAT_FLOAT
2743 else if (var2.v_type == VAR_FLOAT)
2744 {
2745 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2746 var2.vval.v_float);
2747 s2 = buf2;
2748 }
2749#endif
2750 else
2751 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002752 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002753 {
2754 clear_tv(rettv);
2755 clear_tv(&var2);
2756 return FAIL;
2757 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002758 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002759 clear_tv(rettv);
2760 rettv->v_type = VAR_STRING;
2761 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002763 else if (op == '+' && rettv->v_type == VAR_BLOB
2764 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002765 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002766 else if (op == '+' && rettv->v_type == VAR_LIST
2767 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002768 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002769 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002770 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 else
2773 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002774 int error = FALSE;
2775 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002776#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002777 float_T f1 = 0, f2 = 0;
2778
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002779 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002780 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002781 f1 = rettv->vval.v_float;
2782 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002783 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002784 else
2785#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002786 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002787 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002788 if (error)
2789 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002790 // This can only happen for "list + non-list". For
2791 // "non-list + ..." or "something - ...", we returned
2792 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002793 clear_tv(rettv);
2794 return FAIL;
2795 }
2796#ifdef FEAT_FLOAT
2797 if (var2.v_type == VAR_FLOAT)
2798 f1 = n1;
2799#endif
2800 }
2801#ifdef FEAT_FLOAT
2802 if (var2.v_type == VAR_FLOAT)
2803 {
2804 f2 = var2.vval.v_float;
2805 n2 = 0;
2806 }
2807 else
2808#endif
2809 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002810 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002811 if (error)
2812 {
2813 clear_tv(rettv);
2814 clear_tv(&var2);
2815 return FAIL;
2816 }
2817#ifdef FEAT_FLOAT
2818 if (rettv->v_type == VAR_FLOAT)
2819 f2 = n2;
2820#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002821 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002822 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002823
2824#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002825 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002826 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2827 {
2828 if (op == '+')
2829 f1 = f1 + f2;
2830 else
2831 f1 = f1 - f2;
2832 rettv->v_type = VAR_FLOAT;
2833 rettv->vval.v_float = f1;
2834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002836#endif
2837 {
2838 if (op == '+')
2839 n1 = n1 + n2;
2840 else
2841 n1 = n1 - n2;
2842 rettv->v_type = VAR_NUMBER;
2843 rettv->vval.v_number = n1;
2844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002846 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 }
2848 }
2849 return OK;
2850}
2851
2852/*
2853 * Handle fifth level expression:
2854 * * number multiplication
2855 * / number division
2856 * % number modulo
2857 *
2858 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002859 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 *
2861 * Return OK or FAIL.
2862 */
2863 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002864eval6(
2865 char_u **arg,
2866 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002867 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002868 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002870#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002871 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873
2874 /*
2875 * Get the first variable.
2876 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002877 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 return FAIL;
2879
2880 /*
2881 * Repeat computing, until no '*', '/' or '%' is following.
2882 */
2883 for (;;)
2884 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002885 int evaluate;
2886 int getnext;
2887 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002888 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002889 int op;
2890 varnumber_T n1, n2;
2891#ifdef FEAT_FLOAT
2892 float_T f1, f2;
2893#endif
2894 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002895
Bram Moolenaar9d489562020-07-30 20:08:50 +02002896 p = eval_next_non_blank(*arg, evalarg, &getnext);
2897 op = *p;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002898 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002900
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002901 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002902 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002903 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002904 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002905 {
2906 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
2907 {
2908 error_white_both(p, 1);
2909 clear_tv(rettv);
2910 return FAIL;
2911 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002912 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002915#ifdef FEAT_FLOAT
2916 f1 = 0;
2917 f2 = 0;
2918#endif
2919 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002920 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002922#ifdef FEAT_FLOAT
2923 if (rettv->v_type == VAR_FLOAT)
2924 {
2925 f1 = rettv->vval.v_float;
2926 use_float = TRUE;
2927 n1 = 0;
2928 }
2929 else
2930#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002931 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002932 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002933 if (error)
2934 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 }
2936 else
2937 n1 = 0;
2938
2939 /*
2940 * Get the second variable.
2941 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002942 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
2943 {
2944 error_white_both(p, 1);
2945 clear_tv(rettv);
2946 return FAIL;
2947 }
2948 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002949 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 return FAIL;
2951
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002952 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002954#ifdef FEAT_FLOAT
2955 if (var2.v_type == VAR_FLOAT)
2956 {
2957 if (!use_float)
2958 {
2959 f1 = n1;
2960 use_float = TRUE;
2961 }
2962 f2 = var2.vval.v_float;
2963 n2 = 0;
2964 }
2965 else
2966#endif
2967 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002968 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002969 clear_tv(&var2);
2970 if (error)
2971 return FAIL;
2972#ifdef FEAT_FLOAT
2973 if (use_float)
2974 f2 = n2;
2975#endif
2976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977
2978 /*
2979 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002980 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002982#ifdef FEAT_FLOAT
2983 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002985 if (op == '*')
2986 f1 = f1 * f2;
2987 else if (op == '/')
2988 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002989# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002990 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002991 if (f2 == 0.0)
2992 {
2993 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002994 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002995 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002996 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002997 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002998 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002999 }
3000 else
3001 f1 = f1 / f2;
3002# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003003 // We rely on the floating point library to handle divide
3004 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003005 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003006# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003009 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003010 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003011 return FAIL;
3012 }
3013 rettv->v_type = VAR_FLOAT;
3014 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 }
3016 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003017#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003019 if (op == '*')
3020 n1 = n1 * n2;
3021 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01003022 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01003024 n1 = num_modulus(n1, n2);
3025
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003026 rettv->v_type = VAR_NUMBER;
3027 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 }
3030 }
3031
3032 return OK;
3033}
3034
3035/*
3036 * Handle sixth level expression:
3037 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003038 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003039 * "string" string constant
3040 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 * &option-name option value
3042 * @r register contents
3043 * identifier variable value
3044 * function() function call
3045 * $VAR environment variable
3046 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003047 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003048 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003049 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003050 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 *
3052 * Also handle:
3053 * ! in front logical NOT
3054 * - in front unary minus
3055 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003056 * trailing [] subscript in String or List
3057 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003058 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 *
3060 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003061 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 *
3063 * Return OK or FAIL.
3064 */
3065 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003066eval7(
3067 char_u **arg,
3068 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003069 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003070 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003072 int evaluate = evalarg != NULL
3073 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 int len;
3075 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 char_u *start_leader, *end_leader;
3077 int ret = OK;
3078 char_u *alias;
3079
3080 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003081 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003082 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003084 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085
3086 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003087 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 */
3089 start_leader = *arg;
3090 while (**arg == '!' || **arg == '-' || **arg == '+')
3091 *arg = skipwhite(*arg + 1);
3092 end_leader = *arg;
3093
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003094 if (**arg == '.' && (!isdigit(*(*arg + 1))
3095#ifdef FEAT_FLOAT
3096 || current_sctx.sc_version < 2
3097#endif
3098 ))
3099 {
3100 semsg(_(e_invexpr2), *arg);
3101 ++*arg;
3102 return FAIL;
3103 }
3104
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 switch (**arg)
3106 {
3107 /*
3108 * Number constant.
3109 */
3110 case '0':
3111 case '1':
3112 case '2':
3113 case '3':
3114 case '4':
3115 case '5':
3116 case '6':
3117 case '7':
3118 case '8':
3119 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003120 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003121
3122 // Apply prefixed "-" and "+" now. Matters especially when
3123 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003124 if (ret == OK && evaluate && end_leader > start_leader
3125 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003126 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 break;
3128
3129 /*
3130 * String constant: "string".
3131 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003132 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 break;
3134
3135 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003136 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003138 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003139 break;
3140
3141 /*
3142 * List: [expr, expr]
3143 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003144 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 break;
3146
3147 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003148 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003149 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003150 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003151 {
3152 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003153 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003154 }
3155 else
3156 ret = NOTDONE;
3157 break;
3158
3159 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003160 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003161 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003162 */
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003163 case '{': ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003164 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003165 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003166 break;
3167
3168 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003169 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003171 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 break;
3173
3174 /*
3175 * Environment variable: $VAR.
3176 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003177 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 break;
3179
3180 /*
3181 * Register contents: @r.
3182 */
3183 case '@': ++*arg;
3184 if (evaluate)
3185 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003186 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02003187 rettv->vval.v_string = get_reg_contents(**arg,
3188 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 }
3190 if (**arg != NUL)
3191 ++*arg;
3192 break;
3193
3194 /*
3195 * nested expression: (expression).
3196 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003197 case '(': {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003198 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003199 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003200
Bram Moolenaar9215f012020-06-27 21:18:00 +02003201 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003202 if (**arg == ')')
3203 ++*arg;
3204 else if (ret == OK)
3205 {
3206 emsg(_(e_missing_close));
3207 clear_tv(rettv);
3208 ret = FAIL;
3209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 }
3211 break;
3212
Bram Moolenaar8c711452005-01-14 21:53:12 +00003213 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 break;
3215 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003216
3217 if (ret == NOTDONE)
3218 {
3219 /*
3220 * Must be a variable or function name.
3221 * Can also be a curly-braces kind of name: {expr}.
3222 */
3223 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003224 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003225 if (alias != NULL)
3226 s = alias;
3227
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003228 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003229 ret = FAIL;
3230 else
3231 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003232 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3233
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003234 if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
3235 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003236 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003237 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003238 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003239 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003240 else if (flags & EVAL_CONSTANT)
3241 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003242 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003243 {
3244 // get the value of "true", "false" or a variable
3245 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3246 {
3247 rettv->v_type = VAR_BOOL;
3248 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003249 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003250 }
3251 else if (len == 5 && in_vim9script()
3252 && STRNCMP(s, "false", 4) == 0)
3253 {
3254 rettv->v_type = VAR_BOOL;
3255 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003256 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003257 }
3258 else
3259 ret = eval_variable(s, len, rettv, NULL, TRUE, FALSE);
3260 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003261 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003262 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003263 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003264 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003265 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003266 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003267 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003268 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003269 }
3270
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003271 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3272 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003273 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003274 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275
3276 /*
3277 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3278 */
3279 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003280 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003281 return ret;
3282}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003283
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003284/*
3285 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003286 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003287 * Adjusts "end_leaderp" until it is at "start_leader".
3288 */
3289 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003290eval7_leader(
3291 typval_T *rettv,
3292 int numeric_only,
3293 char_u *start_leader,
3294 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003295{
3296 char_u *end_leader = *end_leaderp;
3297 int ret = OK;
3298 int error = FALSE;
3299 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003300 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003301#ifdef FEAT_FLOAT
3302 float_T f = 0.0;
3303
3304 if (rettv->v_type == VAR_FLOAT)
3305 f = rettv->vval.v_float;
3306 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003307#endif
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003308 if (in_vim9script() && end_leader[-1] == '!')
3309 val = tv2bool(rettv);
3310 else
3311 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003312 if (error)
3313 {
3314 clear_tv(rettv);
3315 ret = FAIL;
3316 }
3317 else
3318 {
3319 while (end_leader > start_leader)
3320 {
3321 --end_leader;
3322 if (*end_leader == '!')
3323 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003324 if (numeric_only)
3325 {
3326 ++end_leader;
3327 break;
3328 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003329#ifdef FEAT_FLOAT
3330 if (rettv->v_type == VAR_FLOAT)
3331 f = !f;
3332 else
3333#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003334 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003335 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003336 type = VAR_BOOL;
3337 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003338 }
3339 else if (*end_leader == '-')
3340 {
3341#ifdef FEAT_FLOAT
3342 if (rettv->v_type == VAR_FLOAT)
3343 f = -f;
3344 else
3345#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003346 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003347 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003348 type = VAR_NUMBER;
3349 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003350 }
3351 }
3352#ifdef FEAT_FLOAT
3353 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003355 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003356 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003358 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003359#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003360 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003361 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003362 if (in_vim9script())
3363 rettv->v_type = type;
3364 else
3365 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003366 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003369 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 return ret;
3371}
3372
3373/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003374 * Call the function referred to in "rettv".
3375 */
3376 static int
3377call_func_rettv(
3378 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003379 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003380 typval_T *rettv,
3381 int evaluate,
3382 dict_T *selfdict,
3383 typval_T *basetv)
3384{
3385 partial_T *pt = NULL;
3386 funcexe_T funcexe;
3387 typval_T functv;
3388 char_u *s;
3389 int ret;
3390
3391 // need to copy the funcref so that we can clear rettv
3392 if (evaluate)
3393 {
3394 functv = *rettv;
3395 rettv->v_type = VAR_UNKNOWN;
3396
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003397 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003398 if (functv.v_type == VAR_PARTIAL)
3399 {
3400 pt = functv.vval.v_partial;
3401 s = partial_name(pt);
3402 }
3403 else
3404 s = functv.vval.v_string;
3405 }
3406 else
3407 s = (char_u *)"";
3408
Bram Moolenaara80faa82020-04-12 19:37:17 +02003409 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003410 funcexe.firstline = curwin->w_cursor.lnum;
3411 funcexe.lastline = curwin->w_cursor.lnum;
3412 funcexe.evaluate = evaluate;
3413 funcexe.partial = pt;
3414 funcexe.selfdict = selfdict;
3415 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003416 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003417
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003418 // Clear the funcref afterwards, so that deleting it while
3419 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003420 if (evaluate)
3421 clear_tv(&functv);
3422
3423 return ret;
3424}
3425
3426/*
3427 * Evaluate "->method()".
3428 * "*arg" points to the '-'.
3429 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3430 */
3431 static int
3432eval_lambda(
3433 char_u **arg,
3434 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003435 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003436 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003437{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003438 int evaluate = evalarg != NULL
3439 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003440 typval_T base = *rettv;
3441 int ret;
3442
3443 // Skip over the ->.
3444 *arg += 2;
3445 rettv->v_type = VAR_UNKNOWN;
3446
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003447 ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003448 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003449 return FAIL;
3450 else if (**arg != '(')
3451 {
3452 if (verbose)
3453 {
3454 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003455 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003456 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003457 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003458 }
3459 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003460 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003461 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003462 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003463 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003464
3465 // Clear the funcref afterwards, so that deleting it while
3466 // evaluating the arguments is possible (see test55).
3467 if (evaluate)
3468 clear_tv(&base);
3469
3470 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003471}
3472
3473/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003474 * Evaluate "->method()".
3475 * "*arg" points to the '-'.
3476 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3477 */
3478 static int
3479eval_method(
3480 char_u **arg,
3481 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003482 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003483 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003484{
3485 char_u *name;
3486 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003487 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003488 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003489 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003490 int evaluate = evalarg != NULL
3491 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003492
3493 // Skip over the ->.
3494 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003495 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003496
Bram Moolenaarac92e252019-08-03 21:58:38 +02003497 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003498 len = get_name_len(arg, &alias, evaluate, TRUE);
3499 if (alias != NULL)
3500 name = alias;
3501
3502 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003503 {
3504 if (verbose)
3505 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003506 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003507 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003508 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003509 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003510 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003511 if (**arg != '(')
3512 {
3513 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003514 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003515 ret = FAIL;
3516 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003517 else if (VIM_ISWHITE((*arg)[-1]))
3518 {
3519 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003520 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003521 ret = FAIL;
3522 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003523 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003524 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003525 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003526 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003527
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003528 // Clear the funcref afterwards, so that deleting it while
3529 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003530 if (evaluate)
3531 clear_tv(&base);
3532
Bram Moolenaarac92e252019-08-03 21:58:38 +02003533 return ret;
3534}
3535
3536/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003537 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3538 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003539 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3540 */
3541 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003542eval_index(
3543 char_u **arg,
3544 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003545 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003546 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003547{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003548 int evaluate = evalarg != NULL
3549 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003550 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003551 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003552 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003553 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003554 int keylen = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003555
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003556 if (check_can_index(rettv, evaluate, verbose) == FAIL)
3557 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003558
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003559 init_tv(&var1);
3560 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003561 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003562 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003563 /*
3564 * dict.name
3565 */
3566 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003567 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003568 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003569 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003570 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003571 *arg = skipwhite(key + keylen);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003572 }
3573 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003574 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003575 /*
3576 * something[idx]
3577 *
3578 * Get the (first) variable from inside the [].
3579 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003580 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003581 if (**arg == ':')
3582 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003583 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003584 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003585 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003586 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003587 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003588 clear_tv(&var1);
3589 return FAIL;
3590 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003591
3592 /*
3593 * Get the second variable from inside the [:].
3594 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003595 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003596 if (**arg == ':')
3597 {
3598 range = TRUE;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003599 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003600 if (**arg == ']')
3601 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003602 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003603 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003604 if (!empty1)
3605 clear_tv(&var1);
3606 return FAIL;
3607 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003608 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003609 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003610 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003611 if (!empty1)
3612 clear_tv(&var1);
3613 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003614 return FAIL;
3615 }
3616 }
3617
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003618 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003619 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003620 if (**arg != ']')
3621 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003622 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003623 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003624 clear_tv(&var1);
3625 if (range)
3626 clear_tv(&var2);
3627 return FAIL;
3628 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02003629 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003630 }
3631
3632 if (evaluate)
3633 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003634 int res = eval_index_inner(rettv, range,
3635 empty1 ? NULL : &var1, empty2 ? NULL : &var2,
3636 key, keylen, verbose);
3637 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003638 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003639 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003640 clear_tv(&var2);
3641 return res;
3642 }
3643 return OK;
3644}
3645
3646/*
3647 * Check if "rettv" can have an [index] or [sli:ce]
3648 */
3649 int
3650check_can_index(typval_T *rettv, int evaluate, int verbose)
3651{
3652 switch (rettv->v_type)
3653 {
3654 case VAR_FUNC:
3655 case VAR_PARTIAL:
3656 if (verbose)
3657 emsg(_("E695: Cannot index a Funcref"));
3658 return FAIL;
3659 case VAR_FLOAT:
3660#ifdef FEAT_FLOAT
3661 if (verbose)
3662 emsg(_(e_float_as_string));
3663 return FAIL;
3664#endif
3665 case VAR_BOOL:
3666 case VAR_SPECIAL:
3667 case VAR_JOB:
3668 case VAR_CHANNEL:
3669 if (verbose)
3670 emsg(_(e_cannot_index_special_variable));
3671 return FAIL;
3672 case VAR_UNKNOWN:
3673 case VAR_ANY:
3674 case VAR_VOID:
3675 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003676 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003677 emsg(_(e_cannot_index_special_variable));
3678 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003679 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003680 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003681
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003682 case VAR_STRING:
3683 case VAR_LIST:
3684 case VAR_DICT:
3685 case VAR_BLOB:
3686 break;
3687 case VAR_NUMBER:
3688 if (in_vim9script())
3689 emsg(_(e_cannot_index_number));
3690 break;
3691 }
3692 return OK;
3693}
3694
3695/*
3696 * Apply index or range to "rettv".
3697 * "var1" is the first index, NULL for [:expr].
3698 * "var2" is the second index, NULL for [expr] and [expr: ]
3699 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
3700 */
3701 int
3702eval_index_inner(
3703 typval_T *rettv,
3704 int is_range,
3705 typval_T *var1,
3706 typval_T *var2,
3707 char_u *key,
3708 int keylen,
3709 int verbose)
3710{
3711 long n1, n2 = 0;
3712 long len;
3713
3714 n1 = 0;
3715 if (var1 != NULL && rettv->v_type != VAR_DICT)
3716 n1 = tv_get_number(var1);
3717
3718 if (is_range)
3719 {
3720 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003721 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003722 if (verbose)
3723 emsg(_(e_cannot_slice_dictionary));
3724 return FAIL;
3725 }
3726 if (var2 == NULL)
3727 n2 = -1;
3728 else
3729 n2 = tv_get_number(var2);
3730 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01003731
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003732 switch (rettv->v_type)
3733 {
3734 case VAR_UNKNOWN:
3735 case VAR_ANY:
3736 case VAR_VOID:
3737 case VAR_FUNC:
3738 case VAR_PARTIAL:
3739 case VAR_FLOAT:
3740 case VAR_BOOL:
3741 case VAR_SPECIAL:
3742 case VAR_JOB:
3743 case VAR_CHANNEL:
3744 break; // not evaluating, skipping over subscript
3745
3746 case VAR_NUMBER:
3747 case VAR_STRING:
3748 {
3749 char_u *s = tv_get_string(rettv);
3750
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003751 len = (long)STRLEN(s);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003752 if (in_vim9script())
3753 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003754 if (is_range)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003755 s = string_slice(s, n1, n2);
3756 else
3757 s = char_from_string(s, n1);
3758 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003759 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003760 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003761 // The resulting variable is a substring. If the indexes
3762 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003763 if (n1 < 0)
3764 {
3765 n1 = len + n1;
3766 if (n1 < 0)
3767 n1 = 0;
3768 }
3769 if (n2 < 0)
3770 n2 = len + n2;
3771 else if (n2 >= len)
3772 n2 = len;
3773 if (n1 >= len || n2 < 0 || n1 > n2)
3774 s = NULL;
3775 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02003776 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003777 }
3778 else
3779 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003780 // The resulting variable is a string of a single
3781 // character. If the index is too big or negative the
3782 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003783 if (n1 >= len || n1 < 0)
3784 s = NULL;
3785 else
3786 s = vim_strnsave(s + n1, 1);
3787 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003788 clear_tv(rettv);
3789 rettv->v_type = VAR_STRING;
3790 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003791 }
3792 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003793
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003794 case VAR_BLOB:
3795 len = blob_len(rettv->vval.v_blob);
3796 if (is_range)
3797 {
3798 // The resulting variable is a sub-blob. If the indexes
3799 // are out of range the result is empty.
3800 if (n1 < 0)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003801 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003802 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003803 if (n1 < 0)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003804 n1 = 0;
3805 }
3806 if (n2 < 0)
3807 n2 = len + n2;
3808 else if (n2 >= len)
3809 n2 = len - 1;
3810 if (n1 >= len || n2 < 0 || n1 > n2)
3811 {
3812 clear_tv(rettv);
3813 rettv->v_type = VAR_BLOB;
3814 rettv->vval.v_blob = NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003815 }
3816 else
3817 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003818 blob_T *blob = blob_alloc();
3819 long i;
3820
3821 if (blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003822 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003823 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003824 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003825 blob_free(blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003826 return FAIL;
3827 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003828 blob->bv_ga.ga_len = n2 - n1 + 1;
3829 for (i = n1; i <= n2; i++)
3830 blob_set(blob, i - n1,
3831 blob_get(rettv->vval.v_blob, i));
3832
3833 clear_tv(rettv);
3834 rettv_blob_set(rettv, blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003835 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003836 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003837 }
3838 else
3839 {
3840 // The resulting variable is a byte value.
3841 // If the index is too big or negative that is an error.
3842 if (n1 < 0)
3843 n1 = len + n1;
3844 if (n1 < len && n1 >= 0)
3845 {
3846 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003847
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003848 clear_tv(rettv);
3849 rettv->v_type = VAR_NUMBER;
3850 rettv->vval.v_number = v;
3851 }
3852 else
3853 semsg(_(e_blobidx), n1);
3854 }
3855 break;
3856
3857 case VAR_LIST:
3858 if (var1 == NULL)
3859 n1 = 0;
3860 if (var2 == NULL)
3861 n2 = -1;
3862 if (list_slice_or_index(rettv->vval.v_list,
3863 is_range, n1, n2, rettv, verbose) == FAIL)
3864 return FAIL;
3865 break;
3866
3867 case VAR_DICT:
3868 {
3869 dictitem_T *item;
3870 typval_T tmp;
3871
3872 if (key == NULL)
3873 {
3874 key = tv_get_string_chk(var1);
3875 if (key == NULL)
3876 return FAIL;
3877 }
3878
3879 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
3880
3881 if (item == NULL && verbose)
3882 semsg(_(e_dictkey), key);
3883 if (item == NULL)
3884 return FAIL;
3885
3886 copy_tv(&item->di_tv, &tmp);
3887 clear_tv(rettv);
3888 *rettv = tmp;
3889 }
3890 break;
3891 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003892 return OK;
3893}
3894
3895/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02003896 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003897 */
3898 char_u *
3899partial_name(partial_T *pt)
3900{
3901 if (pt->pt_name != NULL)
3902 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02003903 if (pt->pt_func != NULL)
3904 return pt->pt_func->uf_name;
3905 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003906}
3907
Bram Moolenaarddecc252016-04-06 22:59:37 +02003908 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003909partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003910{
3911 int i;
3912
3913 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003914 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003915 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003916 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003917 if (pt->pt_name != NULL)
3918 {
3919 func_unref(pt->pt_name);
3920 vim_free(pt->pt_name);
3921 }
3922 else
3923 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003924
3925 if (pt->pt_funcstack != NULL)
3926 {
3927 // Decrease the reference count for the context of a closure. If down
3928 // to zero free it and clear the variables on the stack.
3929 if (--pt->pt_funcstack->fs_refcount == 0)
3930 {
3931 garray_T *gap = &pt->pt_funcstack->fs_ga;
3932 typval_T *stack = gap->ga_data;
3933
3934 for (i = 0; i < gap->ga_len; ++i)
3935 clear_tv(stack + i);
3936 ga_clear(gap);
3937 vim_free(pt->pt_funcstack);
3938 }
3939 pt->pt_funcstack = NULL;
3940 }
3941
Bram Moolenaarddecc252016-04-06 22:59:37 +02003942 vim_free(pt);
3943}
3944
3945/*
3946 * Unreference a closure: decrement the reference count and free it when it
3947 * becomes zero.
3948 */
3949 void
3950partial_unref(partial_T *pt)
3951{
3952 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003953 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003954}
3955
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003956/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003957 * Return the next (unique) copy ID.
3958 * Used for serializing nested structures.
3959 */
3960 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003961get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003962{
3963 current_copyID += COPYID_INC;
3964 return current_copyID;
3965}
3966
3967/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003968 * Garbage collection for lists and dictionaries.
3969 *
3970 * We use reference counts to be able to free most items right away when they
3971 * are no longer used. But for composite items it's possible that it becomes
3972 * unused while the reference count is > 0: When there is a recursive
3973 * reference. Example:
3974 * :let l = [1, 2, 3]
3975 * :let d = {9: l}
3976 * :let l[1] = d
3977 *
3978 * Since this is quite unusual we handle this with garbage collection: every
3979 * once in a while find out which lists and dicts are not referenced from any
3980 * variable.
3981 *
3982 * Here is a good reference text about garbage collection (refers to Python
3983 * but it applies to all reference-counting mechanisms):
3984 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00003985 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00003986
3987/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003988 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02003989 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003990 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00003991 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003992 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003993garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003994{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003995 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003996 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003997 buf_T *buf;
3998 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01003999 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004000 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004001
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004002 if (!testing)
4003 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004004 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004005 want_garbage_collect = FALSE;
4006 may_garbage_collect = FALSE;
4007 garbage_collect_at_exit = FALSE;
4008 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004009
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004010 // The execution stack can grow big, limit the size.
4011 if (exestack.ga_maxlen - exestack.ga_len > 500)
4012 {
4013 size_t new_len;
4014 char_u *pp;
4015 int n;
4016
4017 // Keep 150% of the current size, with a minimum of the growth size.
4018 n = exestack.ga_len / 2;
4019 if (n < exestack.ga_growsize)
4020 n = exestack.ga_growsize;
4021
4022 // Don't make it bigger though.
4023 if (exestack.ga_len + n < exestack.ga_maxlen)
4024 {
4025 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4026 pp = vim_realloc(exestack.ga_data, new_len);
4027 if (pp == NULL)
4028 return FAIL;
4029 exestack.ga_maxlen = exestack.ga_len + n;
4030 exestack.ga_data = pp;
4031 }
4032 }
4033
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004034 // We advance by two because we add one for items referenced through
4035 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004036 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004037
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004038 /*
4039 * 1. Go through all accessible variables and mark all lists and dicts
4040 * with copyID.
4041 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004042
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004043 // Don't free variables in the previous_funccal list unless they are only
4044 // referenced through previous_funccal. This must be first, because if
4045 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004046 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004047
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004048 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004049 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004050
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004051 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004052 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004053 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4054 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004055
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004056 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004057 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004058 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4059 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004060 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004061 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4062 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004063#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004064 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004065 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4066 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004067 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004068 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004069 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4070 NULL, NULL);
4071#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004072
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004073 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004074 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004075 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4076 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004077 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004078 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004079
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004080 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004081 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004082
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004083 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004084 abort = abort || set_ref_in_functions(copyID);
4085
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004086 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004087 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004088
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004089 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004090 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004091
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004092 // callbacks in buffers
4093 abort = abort || set_ref_in_buffers(copyID);
4094
Bram Moolenaar1dced572012-04-05 16:54:08 +02004095#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004096 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004097#endif
4098
Bram Moolenaardb913952012-06-29 12:54:53 +02004099#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004100 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004101#endif
4102
4103#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004104 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004105#endif
4106
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004107#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004108 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004109 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004110#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004111#ifdef FEAT_NETBEANS_INTG
4112 abort = abort || set_ref_in_nb_channel(copyID);
4113#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004114
Bram Moolenaare3188e22016-05-31 21:13:04 +02004115#ifdef FEAT_TIMERS
4116 abort = abort || set_ref_in_timer(copyID);
4117#endif
4118
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004119#ifdef FEAT_QUICKFIX
4120 abort = abort || set_ref_in_quickfix(copyID);
4121#endif
4122
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004123#ifdef FEAT_TERMINAL
4124 abort = abort || set_ref_in_term(copyID);
4125#endif
4126
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004127#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004128 abort = abort || set_ref_in_popups(copyID);
4129#endif
4130
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004131 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004132 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004133 /*
4134 * 2. Free lists and dictionaries that are not referenced.
4135 */
4136 did_free = free_unref_items(copyID);
4137
4138 /*
4139 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004140 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004141 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004142 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004143 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004144 else if (p_verbose > 0)
4145 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004146 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004147 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004148
4149 return did_free;
4150}
4151
4152/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004153 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004154 */
4155 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004156free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004157{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004158 int did_free = FALSE;
4159
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004160 // Let all "free" functions know that we are here. This means no
4161 // dictionaries, lists, channels or jobs are to be freed, because we will
4162 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004163 in_free_unref_items = TRUE;
4164
4165 /*
4166 * PASS 1: free the contents of the items. We don't free the items
4167 * themselves yet, so that it is possible to decrement refcount counters
4168 */
4169
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004170 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004171 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004172
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004173 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004174 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004175
4176#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004177 // Go through the list of jobs and free items without the copyID. This
4178 // must happen before doing channels, because jobs refer to channels, but
4179 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004180 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4181
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004182 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004183 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4184#endif
4185
4186 /*
4187 * PASS 2: free the items themselves.
4188 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004189 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004190 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004191
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004192#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004193 // Go through the list of jobs and free items without the copyID. This
4194 // must happen before doing channels, because jobs refer to channels, but
4195 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004196 free_unused_jobs(copyID, COPYID_MASK);
4197
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004198 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004199 free_unused_channels(copyID, COPYID_MASK);
4200#endif
4201
4202 in_free_unref_items = FALSE;
4203
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004204 return did_free;
4205}
4206
4207/*
4208 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004209 * "list_stack" is used to add lists to be marked. Can be NULL.
4210 *
4211 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004212 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004213 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004214set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004215{
4216 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004217 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004218 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004219 hashtab_T *cur_ht;
4220 ht_stack_T *ht_stack = NULL;
4221 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004222
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004223 cur_ht = ht;
4224 for (;;)
4225 {
4226 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004227 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004228 // Mark each item in the hashtab. If the item contains a hashtab
4229 // it is added to ht_stack, if it contains a list it is added to
4230 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004231 todo = (int)cur_ht->ht_used;
4232 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4233 if (!HASHITEM_EMPTY(hi))
4234 {
4235 --todo;
4236 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4237 &ht_stack, list_stack);
4238 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004239 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004240
4241 if (ht_stack == NULL)
4242 break;
4243
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004244 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004245 cur_ht = ht_stack->ht;
4246 tempitem = ht_stack;
4247 ht_stack = ht_stack->prev;
4248 free(tempitem);
4249 }
4250
4251 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004252}
4253
4254/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004255 * Mark a dict and its items with "copyID".
4256 * Returns TRUE if setting references failed somehow.
4257 */
4258 int
4259set_ref_in_dict(dict_T *d, int copyID)
4260{
4261 if (d != NULL && d->dv_copyID != copyID)
4262 {
4263 d->dv_copyID = copyID;
4264 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4265 }
4266 return FALSE;
4267}
4268
4269/*
4270 * Mark a list and its items with "copyID".
4271 * Returns TRUE if setting references failed somehow.
4272 */
4273 int
4274set_ref_in_list(list_T *ll, int copyID)
4275{
4276 if (ll != NULL && ll->lv_copyID != copyID)
4277 {
4278 ll->lv_copyID = copyID;
4279 return set_ref_in_list_items(ll, copyID, NULL);
4280 }
4281 return FALSE;
4282}
4283
4284/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004285 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004286 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4287 *
4288 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004289 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004290 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004291set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004292{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004293 listitem_T *li;
4294 int abort = FALSE;
4295 list_T *cur_l;
4296 list_stack_T *list_stack = NULL;
4297 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004298
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004299 cur_l = l;
4300 for (;;)
4301 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004302 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004303 // Mark each item in the list. If the item contains a hashtab
4304 // it is added to ht_stack, if it contains a list it is added to
4305 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004306 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4307 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4308 ht_stack, &list_stack);
4309 if (list_stack == NULL)
4310 break;
4311
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004312 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004313 cur_l = list_stack->list;
4314 tempitem = list_stack;
4315 list_stack = list_stack->prev;
4316 free(tempitem);
4317 }
4318
4319 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004320}
4321
4322/*
4323 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004324 * "list_stack" is used to add lists to be marked. Can be NULL.
4325 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4326 *
4327 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004328 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004329 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004330set_ref_in_item(
4331 typval_T *tv,
4332 int copyID,
4333 ht_stack_T **ht_stack,
4334 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004335{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004336 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004337
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004338 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004339 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004340 dict_T *dd = tv->vval.v_dict;
4341
Bram Moolenaara03f2332016-02-06 18:09:59 +01004342 if (dd != NULL && dd->dv_copyID != copyID)
4343 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004344 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004345 dd->dv_copyID = copyID;
4346 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004347 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004348 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4349 }
4350 else
4351 {
4352 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
4353 if (newitem == NULL)
4354 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004355 else
4356 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004357 newitem->ht = &dd->dv_hashtab;
4358 newitem->prev = *ht_stack;
4359 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004360 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004361 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004362 }
4363 }
4364 else if (tv->v_type == VAR_LIST)
4365 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004366 list_T *ll = tv->vval.v_list;
4367
Bram Moolenaara03f2332016-02-06 18:09:59 +01004368 if (ll != NULL && ll->lv_copyID != copyID)
4369 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004370 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004371 ll->lv_copyID = copyID;
4372 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004373 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004374 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004375 }
4376 else
4377 {
4378 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004379 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004380 if (newitem == NULL)
4381 abort = TRUE;
4382 else
4383 {
4384 newitem->list = ll;
4385 newitem->prev = *list_stack;
4386 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004387 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004388 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004389 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004390 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004391 else if (tv->v_type == VAR_FUNC)
4392 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004393 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004394 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004395 else if (tv->v_type == VAR_PARTIAL)
4396 {
4397 partial_T *pt = tv->vval.v_partial;
4398 int i;
4399
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004400 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004401 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004402 // Didn't see this partial yet.
4403 pt->pt_copyID = copyID;
4404
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004405 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004406
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004407 if (pt->pt_dict != NULL)
4408 {
4409 typval_T dtv;
4410
4411 dtv.v_type = VAR_DICT;
4412 dtv.vval.v_dict = pt->pt_dict;
4413 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4414 }
4415
4416 for (i = 0; i < pt->pt_argc; ++i)
4417 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4418 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004419 if (pt->pt_funcstack != NULL)
4420 {
4421 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4422
4423 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4424 abort = abort || set_ref_in_item(stack + i, copyID,
4425 ht_stack, list_stack);
4426 }
4427
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004428 }
4429 }
4430#ifdef FEAT_JOB_CHANNEL
4431 else if (tv->v_type == VAR_JOB)
4432 {
4433 job_T *job = tv->vval.v_job;
4434 typval_T dtv;
4435
4436 if (job != NULL && job->jv_copyID != copyID)
4437 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004438 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004439 if (job->jv_channel != NULL)
4440 {
4441 dtv.v_type = VAR_CHANNEL;
4442 dtv.vval.v_channel = job->jv_channel;
4443 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4444 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004445 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004446 {
4447 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004448 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004449 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4450 }
4451 }
4452 }
4453 else if (tv->v_type == VAR_CHANNEL)
4454 {
4455 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004456 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004457 typval_T dtv;
4458 jsonq_T *jq;
4459 cbq_T *cq;
4460
4461 if (ch != NULL && ch->ch_copyID != copyID)
4462 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004463 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004464 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004465 {
4466 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4467 jq = jq->jq_next)
4468 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4469 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4470 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004471 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004472 {
4473 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004474 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004475 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4476 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004477 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004478 {
4479 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004480 dtv.vval.v_partial =
4481 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004482 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4483 }
4484 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004485 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004486 {
4487 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004488 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004489 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4490 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004491 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004492 {
4493 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004494 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004495 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4496 }
4497 }
4498 }
4499#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004500 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004501}
4502
Bram Moolenaar8c711452005-01-14 21:53:12 +00004503/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004504 * Return a string with the string representation of a variable.
4505 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004506 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004507 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004508 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4509 * stings as "string()", otherwise does not put quotes around strings, as
4510 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004511 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4512 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004513 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004514 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004515 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004516echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004517 typval_T *tv,
4518 char_u **tofree,
4519 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004520 int copyID,
4521 int echo_style,
4522 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004523 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004524{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004525 static int recurse = 0;
4526 char_u *r = NULL;
4527
Bram Moolenaar33570922005-01-25 22:26:29 +00004528 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004529 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004530 if (!did_echo_string_emsg)
4531 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004532 // Only give this message once for a recursive call to avoid
4533 // flooding the user with errors. And stop iterating over lists
4534 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004535 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004536 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004537 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004538 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004539 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004540 }
4541 ++recurse;
4542
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004543 switch (tv->v_type)
4544 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004545 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004546 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004547 {
4548 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004549 r = tv->vval.v_string;
4550 if (r == NULL)
4551 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004552 }
4553 else
4554 {
4555 *tofree = string_quote(tv->vval.v_string, FALSE);
4556 r = *tofree;
4557 }
4558 break;
4559
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004560 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004561 if (echo_style)
4562 {
4563 *tofree = NULL;
4564 r = tv->vval.v_string;
4565 }
4566 else
4567 {
4568 *tofree = string_quote(tv->vval.v_string, TRUE);
4569 r = *tofree;
4570 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004571 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004572
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004573 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004574 {
4575 partial_T *pt = tv->vval.v_partial;
4576 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004577 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004578 garray_T ga;
4579 int i;
4580 char_u *tf;
4581
4582 ga_init2(&ga, 1, 100);
4583 ga_concat(&ga, (char_u *)"function(");
4584 if (fname != NULL)
4585 {
4586 ga_concat(&ga, fname);
4587 vim_free(fname);
4588 }
4589 if (pt != NULL && pt->pt_argc > 0)
4590 {
4591 ga_concat(&ga, (char_u *)", [");
4592 for (i = 0; i < pt->pt_argc; ++i)
4593 {
4594 if (i > 0)
4595 ga_concat(&ga, (char_u *)", ");
4596 ga_concat(&ga,
4597 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4598 vim_free(tf);
4599 }
4600 ga_concat(&ga, (char_u *)"]");
4601 }
4602 if (pt != NULL && pt->pt_dict != NULL)
4603 {
4604 typval_T dtv;
4605
4606 ga_concat(&ga, (char_u *)", ");
4607 dtv.v_type = VAR_DICT;
4608 dtv.vval.v_dict = pt->pt_dict;
4609 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4610 vim_free(tf);
4611 }
4612 ga_concat(&ga, (char_u *)")");
4613
4614 *tofree = ga.ga_data;
4615 r = *tofree;
4616 break;
4617 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004618
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004619 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004620 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004621 break;
4622
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004623 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004624 if (tv->vval.v_list == NULL)
4625 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004626 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004627 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004628 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004629 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004630 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4631 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004632 {
4633 *tofree = NULL;
4634 r = (char_u *)"[...]";
4635 }
4636 else
4637 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004638 int old_copyID = tv->vval.v_list->lv_copyID;
4639
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004640 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004641 *tofree = list2string(tv, copyID, restore_copyID);
4642 if (restore_copyID)
4643 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004644 r = *tofree;
4645 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004646 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004647
Bram Moolenaar8c711452005-01-14 21:53:12 +00004648 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004649 if (tv->vval.v_dict == NULL)
4650 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004651 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004652 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004653 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004654 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004655 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4656 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004657 {
4658 *tofree = NULL;
4659 r = (char_u *)"{...}";
4660 }
4661 else
4662 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004663 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004664
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004665 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004666 *tofree = dict2string(tv, copyID, restore_copyID);
4667 if (restore_copyID)
4668 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004669 r = *tofree;
4670 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004671 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004672
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004673 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004674 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004675 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004676 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004677 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004678 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004679 break;
4680
Bram Moolenaar835dc632016-02-07 14:27:38 +01004681 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004682 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004683 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004684 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004685 if (composite_val)
4686 {
4687 *tofree = string_quote(r, FALSE);
4688 r = *tofree;
4689 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004690 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004691
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004692 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004693#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004694 *tofree = NULL;
4695 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4696 r = numbuf;
4697 break;
4698#endif
4699
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004700 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004701 case VAR_SPECIAL:
4702 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004703 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004704 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004705 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004706
Bram Moolenaar8502c702014-06-17 12:51:16 +02004707 if (--recurse == 0)
4708 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004709 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004710}
4711
4712/*
4713 * Return a string with the string representation of a variable.
4714 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4715 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004716 * Does not put quotes around strings, as ":echo" displays values.
4717 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4718 * May return NULL.
4719 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004720 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004721echo_string(
4722 typval_T *tv,
4723 char_u **tofree,
4724 char_u *numbuf,
4725 int copyID)
4726{
4727 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4728}
4729
4730/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004731 * Return string "str" in ' quotes, doubling ' characters.
4732 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004733 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004734 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004735 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004736string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004737{
Bram Moolenaar33570922005-01-25 22:26:29 +00004738 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004739 char_u *p, *r, *s;
4740
Bram Moolenaar33570922005-01-25 22:26:29 +00004741 len = (function ? 13 : 3);
4742 if (str != NULL)
4743 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004744 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004745 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004746 if (*p == '\'')
4747 ++len;
4748 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004749 s = r = alloc(len);
4750 if (r != NULL)
4751 {
4752 if (function)
4753 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004754 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004755 r += 10;
4756 }
4757 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004758 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004759 if (str != NULL)
4760 for (p = str; *p != NUL; )
4761 {
4762 if (*p == '\'')
4763 *r++ = '\'';
4764 MB_COPY_CHAR(p, r);
4765 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004766 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004767 if (function)
4768 *r++ = ')';
4769 *r++ = NUL;
4770 }
4771 return s;
4772}
4773
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004774#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004775/*
4776 * Convert the string "text" to a floating point number.
4777 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4778 * this always uses a decimal point.
4779 * Returns the length of the text that was consumed.
4780 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004781 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004782string2float(
4783 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004784 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004785{
4786 char *s = (char *)text;
4787 float_T f;
4788
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004789 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004790 if (STRNICMP(text, "inf", 3) == 0)
4791 {
4792 *value = INFINITY;
4793 return 3;
4794 }
4795 if (STRNICMP(text, "-inf", 3) == 0)
4796 {
4797 *value = -INFINITY;
4798 return 4;
4799 }
4800 if (STRNICMP(text, "nan", 3) == 0)
4801 {
4802 *value = NAN;
4803 return 3;
4804 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004805 f = strtod(s, &s);
4806 *value = f;
4807 return (int)((char_u *)s - text);
4808}
4809#endif
4810
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004811/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004813 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004815 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004816var2fpos(
4817 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004818 int dollar_lnum, // TRUE when $ is last line
4819 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004821 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004823 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004825 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004826 if (varp->v_type == VAR_LIST)
4827 {
4828 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004829 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004830 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004831 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004832
4833 l = varp->vval.v_list;
4834 if (l == NULL)
4835 return NULL;
4836
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004837 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004838 pos.lnum = list_find_nr(l, 0L, &error);
4839 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004840 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004841
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004842 // Get the column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004843 pos.col = list_find_nr(l, 1L, &error);
4844 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004845 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004846 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004847
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004848 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004849 li = list_find(l, 1L);
4850 if (li != NULL && li->li_tv.v_type == VAR_STRING
4851 && li->li_tv.vval.v_string != NULL
4852 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
4853 pos.col = len + 1;
4854
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004855 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004856 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004857 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004858 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004859
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004860 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004861 pos.coladd = list_find_nr(l, 2L, &error);
4862 if (error)
4863 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004864
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004865 return &pos;
4866 }
4867
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004868 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004869 if (name == NULL)
4870 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004871 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004873 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004874 {
4875 if (VIsual_active)
4876 return &VIsual;
4877 return &curwin->w_cursor;
4878 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004879 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004881 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4883 return NULL;
4884 return pp;
4885 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004886
Bram Moolenaara5525202006-03-02 22:52:09 +00004887 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004888
Bram Moolenaar477933c2007-07-17 14:32:23 +00004889 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004890 {
4891 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004892 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004893 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004894 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004895 // In silent Ex mode topline is zero, but that's not a valid line
4896 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004897 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004898 return &pos;
4899 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004900 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004901 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004902 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004903 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004904 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004905 return &pos;
4906 }
4907 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004908 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004910 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 {
4912 pos.lnum = curbuf->b_ml.ml_line_count;
4913 pos.col = 0;
4914 }
4915 else
4916 {
4917 pos.lnum = curwin->w_cursor.lnum;
4918 pos.col = (colnr_T)STRLEN(ml_get_curline());
4919 }
4920 return &pos;
4921 }
4922 return NULL;
4923}
4924
4925/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004926 * Convert list in "arg" into a position and optional file number.
4927 * When "fnump" is NULL there is no file number, only 3 items.
4928 * Note that the column is passed on as-is, the caller may want to decrement
4929 * it to use 1 for the first column.
4930 * Return FAIL when conversion is not possible, doesn't check the position for
4931 * validity.
4932 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004933 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004934list2fpos(
4935 typval_T *arg,
4936 pos_T *posp,
4937 int *fnump,
4938 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004939{
4940 list_T *l = arg->vval.v_list;
4941 long i = 0;
4942 long n;
4943
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004944 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
4945 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00004946 if (arg->v_type != VAR_LIST
4947 || l == NULL
4948 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02004949 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004950 return FAIL;
4951
4952 if (fnump != NULL)
4953 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004954 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004955 if (n < 0)
4956 return FAIL;
4957 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004958 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004959 *fnump = n;
4960 }
4961
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004962 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004963 if (n < 0)
4964 return FAIL;
4965 posp->lnum = n;
4966
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004967 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004968 if (n < 0)
4969 return FAIL;
4970 posp->col = n;
4971
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004972 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004973 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00004974 posp->coladd = 0;
4975 else
4976 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004977
Bram Moolenaar493c1782014-05-28 14:34:46 +02004978 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004979 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02004980
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004981 return OK;
4982}
4983
4984/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 * Get the length of an environment variable name.
4986 * Advance "arg" to the first character after the name.
4987 * Return 0 for error.
4988 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004989 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004990get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991{
4992 char_u *p;
4993 int len;
4994
4995 for (p = *arg; vim_isIDc(*p); ++p)
4996 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004997 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 return 0;
4999
5000 len = (int)(p - *arg);
5001 *arg = p;
5002 return len;
5003}
5004
5005/*
5006 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005007 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 * Return 0 if something is wrong.
5009 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005010 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005011get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012{
5013 char_u *p;
5014 int len;
5015
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005016 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005018 {
5019 if (*p == ':')
5020 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005021 // "s:" is start of "s:var", but "n:" is not and can be used in
5022 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005023 len = (int)(p - *arg);
5024 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5025 || len > 1)
5026 break;
5027 }
5028 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005029 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 return 0;
5031
5032 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005033 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034
5035 return len;
5036}
5037
5038/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005039 * Get the length of the name of a variable or function.
5040 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005042 * Return -1 if curly braces expansion failed.
5043 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 * If the name contains 'magic' {}'s, expand them and return the
5045 * expanded name in an allocated string via 'alias' - caller must free.
5046 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005047 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005048get_name_len(
5049 char_u **arg,
5050 char_u **alias,
5051 int evaluate,
5052 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053{
5054 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 char_u *p;
5056 char_u *expr_start;
5057 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005059 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060
5061 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5062 && (*arg)[2] == (int)KE_SNR)
5063 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005064 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 *arg += 3;
5066 return get_id_len(arg) + 3;
5067 }
5068 len = eval_fname_script(*arg);
5069 if (len > 0)
5070 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005071 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 *arg += len;
5073 }
5074
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005076 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005078 p = find_name_end(*arg, &expr_start, &expr_end,
5079 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 if (expr_start != NULL)
5081 {
5082 char_u *temp_string;
5083
5084 if (!evaluate)
5085 {
5086 len += (int)(p - *arg);
5087 *arg = skipwhite(p);
5088 return len;
5089 }
5090
5091 /*
5092 * Include any <SID> etc in the expanded string:
5093 * Thus the -len here.
5094 */
5095 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5096 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005097 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 *alias = temp_string;
5099 *arg = skipwhite(p);
5100 return (int)STRLEN(temp_string);
5101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102
5103 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005104 // Only give an error when there is something, otherwise it will be
5105 // reported at a higher level.
5106 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005107 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108
5109 return len;
5110}
5111
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005112/*
5113 * Find the end of a variable or function name, taking care of magic braces.
5114 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5115 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005116 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005117 * Return a pointer to just after the name. Equal to "arg" if there is no
5118 * valid name.
5119 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005120 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005121find_name_end(
5122 char_u *arg,
5123 char_u **expr_start,
5124 char_u **expr_end,
5125 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005127 int mb_nest = 0;
5128 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005130 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005131 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005133 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005135 *expr_start = NULL;
5136 *expr_end = NULL;
5137 }
5138
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005139 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005140 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5141 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005142 return arg;
5143
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005144 for (p = arg; *p != NUL
5145 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005146 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005147 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005148 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005149 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005150 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005151 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005152 if (*p == '\'')
5153 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005154 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005155 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005156 ;
5157 if (*p == NUL)
5158 break;
5159 }
5160 else if (*p == '"')
5161 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005162 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005163 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005164 if (*p == '\\' && p[1] != NUL)
5165 ++p;
5166 if (*p == NUL)
5167 break;
5168 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005169 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5170 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005171 // "s:" is start of "s:var", but "n:" is not and can be used in
5172 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005173 len = (int)(p - arg);
5174 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005175 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005176 break;
5177 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005178
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005179 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005181 if (*p == '[')
5182 ++br_nest;
5183 else if (*p == ']')
5184 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005186
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005187 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005189 if (*p == '{')
5190 {
5191 mb_nest++;
5192 if (expr_start != NULL && *expr_start == NULL)
5193 *expr_start = p;
5194 }
5195 else if (*p == '}')
5196 {
5197 mb_nest--;
5198 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5199 *expr_end = p;
5200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 }
5203
5204 return p;
5205}
5206
5207/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005208 * Expands out the 'magic' {}'s in a variable/function name.
5209 * Note that this can call itself recursively, to deal with
5210 * constructs like foo{bar}{baz}{bam}
5211 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5212 * "in_start" ^
5213 * "expr_start" ^
5214 * "expr_end" ^
5215 * "in_end" ^
5216 *
5217 * Returns a new allocated string, which the caller must free.
5218 * Returns NULL for failure.
5219 */
5220 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005221make_expanded_name(
5222 char_u *in_start,
5223 char_u *expr_start,
5224 char_u *expr_end,
5225 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005226{
5227 char_u c1;
5228 char_u *retval = NULL;
5229 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005230
5231 if (expr_end == NULL || in_end == NULL)
5232 return NULL;
5233 *expr_start = NUL;
5234 *expr_end = NUL;
5235 c1 = *in_end;
5236 *in_end = NUL;
5237
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005238 temp_result = eval_to_string(expr_start + 1, FALSE);
5239 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005240 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005241 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5242 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005243 if (retval != NULL)
5244 {
5245 STRCPY(retval, in_start);
5246 STRCAT(retval, temp_result);
5247 STRCAT(retval, expr_end + 1);
5248 }
5249 }
5250 vim_free(temp_result);
5251
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005252 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005253 *expr_start = '{';
5254 *expr_end = '}';
5255
5256 if (retval != NULL)
5257 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005258 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005259 if (expr_start != NULL)
5260 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005261 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005262 temp_result = make_expanded_name(retval, expr_start,
5263 expr_end, temp_result);
5264 vim_free(retval);
5265 retval = temp_result;
5266 }
5267 }
5268
5269 return retval;
5270}
5271
5272/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005274 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005276 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005277eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005279 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005280}
5281
5282/*
5283 * Return TRUE if character "c" can be used as the first character in a
5284 * variable or function name (excluding '{' and '}').
5285 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005286 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005287eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005288{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005289 return ASCII_ISALPHA(c) || c == '_';
5290}
5291
5292/*
5293 * Return TRUE if character "c" can be used as the first character of a
5294 * dictionary key.
5295 */
5296 int
5297eval_isdictc(int c)
5298{
5299 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300}
5301
5302/*
Bram Moolenaare3c37d82020-08-15 18:39:05 +02005303 * Return the character "str[index]" where "index" is the character index. If
5304 * "index" is out of range NULL is returned.
5305 */
5306 char_u *
5307char_from_string(char_u *str, varnumber_T index)
5308{
5309 size_t nbyte = 0;
5310 varnumber_T nchar = index;
5311 size_t slen;
5312
5313 if (str == NULL || index < 0)
5314 return NULL;
5315 slen = STRLEN(str);
5316 while (nchar > 0 && nbyte < slen)
5317 {
5318 nbyte += MB_CPTR2LEN(str + nbyte);
5319 --nchar;
5320 }
5321 if (nbyte >= slen)
5322 return NULL;
5323 return vim_strnsave(str + nbyte, MB_CPTR2LEN(str + nbyte));
5324}
5325
5326/*
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005327 * Get the byte index for character index "idx" in string "str" with length
5328 * "str_len".
5329 * If going over the end return "str_len".
5330 * If "idx" is negative count from the end, -1 is the last character.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005331 * When going over the start return -1.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005332 */
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005333 static long
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005334char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
5335{
5336 varnumber_T nchar = idx;
5337 size_t nbyte = 0;
5338
5339 if (nchar >= 0)
5340 {
5341 while (nchar > 0 && nbyte < str_len)
5342 {
5343 nbyte += MB_CPTR2LEN(str + nbyte);
5344 --nchar;
5345 }
5346 }
5347 else
5348 {
5349 nbyte = str_len;
5350 while (nchar < 0 && nbyte > 0)
5351 {
5352 --nbyte;
5353 nbyte -= mb_head_off(str, str + nbyte);
5354 ++nchar;
5355 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005356 if (nchar < 0)
5357 return -1;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005358 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005359 return (long)nbyte;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005360}
5361
5362/*
5363 * Return the slice "str[first:last]" using character indexes.
5364 * Return NULL when the result is empty.
5365 */
5366 char_u *
5367string_slice(char_u *str, varnumber_T first, varnumber_T last)
5368{
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005369 long start_byte, end_byte;
5370 size_t slen;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005371
5372 if (str == NULL)
5373 return NULL;
5374 slen = STRLEN(str);
5375 start_byte = char_idx2byte(str, slen, first);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005376 if (start_byte < 0)
5377 start_byte = 0; // first index very negative: use zero
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005378 if (last == -1)
5379 end_byte = slen;
5380 else
5381 {
5382 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005383 if (end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005384 // end index is inclusive
5385 end_byte += MB_CPTR2LEN(str + end_byte);
5386 }
5387
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005388 if (start_byte >= (long)slen || end_byte <= start_byte)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005389 return NULL;
5390 return vim_strnsave(str + start_byte, end_byte - start_byte);
5391}
5392
5393/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005394 * Handle:
5395 * - expr[expr], expr[expr:expr] subscript
5396 * - ".name" lookup
5397 * - function call with Funcref variable: func(expr)
5398 * - method call: var->method()
5399 *
5400 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005401 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005402 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005403handle_subscript(
5404 char_u **arg,
5405 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005406 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005407 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005408{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005409 int evaluate = evalarg != NULL
5410 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005411 int ret = OK;
5412 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005413 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005414 int getnext;
5415 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005416
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005417 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005418 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005419 // When at the end of the line and ".name" or "->{" or "->X" follows in
5420 // the next line then consume the line break.
5421 p = eval_next_non_blank(*arg, evalarg, &getnext);
5422 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005423 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaar9d489562020-07-30 20:08:50 +02005424 || (p[0] == '-' && p[1] == '>'
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005425 && (p[2] == '{' || ASCII_ISALPHA(p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005426 {
5427 *arg = eval_next_line(evalarg);
5428 check_white = FALSE;
5429 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005430
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005431 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5432 || rettv->v_type == VAR_PARTIAL))
5433 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005434 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005435 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5436 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005437
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005438 // Stop the expression evaluation when immediately aborting on
5439 // error, or when an interrupt occurred or an exception was thrown
5440 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005441 if (aborting())
5442 {
5443 if (ret == OK)
5444 clear_tv(rettv);
5445 ret = FAIL;
5446 }
5447 dict_unref(selfdict);
5448 selfdict = NULL;
5449 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005450 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005451 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02005452 *arg = p;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005453 if (ret == OK)
5454 {
5455 if ((*arg)[2] == '{')
5456 // expr->{lambda}()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005457 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005458 else
5459 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005460 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005461 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005462 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005463 // "." is ".name" lookup when we found a dict or when evaluating and
5464 // scriptversion is at least 2, where string concatenation is "..".
5465 else if (**arg == '['
5466 || (**arg == '.' && (rettv->v_type == VAR_DICT
5467 || (!evaluate
5468 && (*arg)[1] != '.'
5469 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005470 {
5471 dict_unref(selfdict);
5472 if (rettv->v_type == VAR_DICT)
5473 {
5474 selfdict = rettv->vval.v_dict;
5475 if (selfdict != NULL)
5476 ++selfdict->dv_refcount;
5477 }
5478 else
5479 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005480 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005481 {
5482 clear_tv(rettv);
5483 ret = FAIL;
5484 }
5485 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005486 else
5487 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005488 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005489
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005490 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5491 // Don't do this when "Func" is already a partial that was bound
5492 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005493 if (selfdict != NULL
5494 && (rettv->v_type == VAR_FUNC
5495 || (rettv->v_type == VAR_PARTIAL
5496 && (rettv->vval.v_partial->pt_auto
5497 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005498 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005499
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005500 dict_unref(selfdict);
5501 return ret;
5502}
5503
5504/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005505 * Make a copy of an item.
5506 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005507 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5508 * reference to an already copied list/dict can be used.
5509 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005510 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005511 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005512item_copy(
5513 typval_T *from,
5514 typval_T *to,
5515 int deep,
5516 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005517{
5518 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005519 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005520
Bram Moolenaar33570922005-01-25 22:26:29 +00005521 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005522 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005523 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005524 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005525 }
5526 ++recurse;
5527
5528 switch (from->v_type)
5529 {
5530 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005531 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005532 case VAR_STRING:
5533 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005534 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005535 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005536 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005537 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005538 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005539 copy_tv(from, to);
5540 break;
5541 case VAR_LIST:
5542 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005543 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005544 if (from->vval.v_list == NULL)
5545 to->vval.v_list = NULL;
5546 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5547 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005548 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005549 to->vval.v_list = from->vval.v_list->lv_copylist;
5550 ++to->vval.v_list->lv_refcount;
5551 }
5552 else
5553 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5554 if (to->vval.v_list == NULL)
5555 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005556 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005557 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005558 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005559 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005560 case VAR_DICT:
5561 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005562 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005563 if (from->vval.v_dict == NULL)
5564 to->vval.v_dict = NULL;
5565 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5566 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005567 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005568 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5569 ++to->vval.v_dict->dv_refcount;
5570 }
5571 else
5572 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5573 if (to->vval.v_dict == NULL)
5574 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005575 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005576 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005577 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005578 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005579 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005580 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005581 }
5582 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005583 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005584}
5585
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005586 void
5587echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5588{
5589 char_u *tofree;
5590 char_u numbuf[NUMBUFLEN];
5591 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5592
5593 if (*atstart)
5594 {
5595 *atstart = FALSE;
5596 // Call msg_start() after eval1(), evaluating the expression
5597 // may cause a message to appear.
5598 if (with_space)
5599 {
5600 // Mark the saved text as finishing the line, so that what
5601 // follows is displayed on a new line when scrolling back
5602 // at the more prompt.
5603 msg_sb_eol();
5604 msg_start();
5605 }
5606 }
5607 else if (with_space)
5608 msg_puts_attr(" ", echo_attr);
5609
5610 if (p != NULL)
5611 for ( ; *p != NUL && !got_int; ++p)
5612 {
5613 if (*p == '\n' || *p == '\r' || *p == TAB)
5614 {
5615 if (*p != TAB && *needclr)
5616 {
5617 // remove any text still there from the command
5618 msg_clr_eos();
5619 *needclr = FALSE;
5620 }
5621 msg_putchar_attr(*p, echo_attr);
5622 }
5623 else
5624 {
5625 if (has_mbyte)
5626 {
5627 int i = (*mb_ptr2len)(p);
5628
5629 (void)msg_outtrans_len_attr(p, i, echo_attr);
5630 p += i - 1;
5631 }
5632 else
5633 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5634 }
5635 }
5636 vim_free(tofree);
5637}
5638
Bram Moolenaare9a41262005-01-15 22:18:47 +00005639/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 * ":echo expr1 ..." print each argument separated with a space, add a
5641 * newline at the end.
5642 * ":echon expr1 ..." print each argument plain.
5643 */
5644 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005645ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646{
5647 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005648 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 char_u *p;
5650 int needclr = TRUE;
5651 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01005652 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005653 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005654 evalarg_T evalarg;
5655
Bram Moolenaare707c882020-07-01 13:07:37 +02005656 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657
5658 if (eap->skip)
5659 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005660 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005662 // If eval1() causes an error message the text from the command may
5663 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005664 need_clr_eos = needclr;
5665
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005667 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 {
5669 /*
5670 * Report the invalid expression unless the expression evaluation
5671 * has been cancelled due to an aborting error, an interrupt, or an
5672 * exception.
5673 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005674 if (!aborting() && did_emsg == did_emsg_before
5675 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005676 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005677 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 break;
5679 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005680 need_clr_eos = FALSE;
5681
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005683 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005684
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005685 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 arg = skipwhite(arg);
5687 }
5688 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02005689 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690
5691 if (eap->skip)
5692 --emsg_skip;
5693 else
5694 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005695 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 if (needclr)
5697 msg_clr_eos();
5698 if (eap->cmdidx == CMD_echo)
5699 msg_end();
5700 }
5701}
5702
5703/*
5704 * ":echohl {name}".
5705 */
5706 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005707ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005709 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710}
5711
5712/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005713 * Returns the :echo attribute
5714 */
5715 int
5716get_echo_attr(void)
5717{
5718 return echo_attr;
5719}
5720
5721/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 * ":execute expr1 ..." execute the result of an expression.
5723 * ":echomsg expr1 ..." Print a message
5724 * ":echoerr expr1 ..." Print an error
5725 * Each gets spaces around each argument and a newline at the end for
5726 * echo commands
5727 */
5728 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005729ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730{
5731 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005732 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 int ret = OK;
5734 char_u *p;
5735 garray_T ga;
5736 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737
5738 ga_init2(&ga, 1, 80);
5739
5740 if (eap->skip)
5741 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02005742 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02005744 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01005745 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747
5748 if (!eap->skip)
5749 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005750 char_u buf[NUMBUFLEN];
5751
5752 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01005753 {
5754 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
5755 {
5756 emsg(_(e_inval_string));
5757 p = NULL;
5758 }
5759 else
5760 p = tv_get_string_buf(&rettv, buf);
5761 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005762 else
5763 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005764 if (p == NULL)
5765 {
5766 clear_tv(&rettv);
5767 ret = FAIL;
5768 break;
5769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 len = (int)STRLEN(p);
5771 if (ga_grow(&ga, len + 2) == FAIL)
5772 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005773 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 ret = FAIL;
5775 break;
5776 }
5777 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 ga.ga_len += len;
5781 }
5782
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005783 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 arg = skipwhite(arg);
5785 }
5786
5787 if (ret != FAIL && ga.ga_data != NULL)
5788 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005789 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
5790 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005791 // Mark the already saved text as finishing the line, so that what
5792 // follows is displayed on a new line when scrolling back at the
5793 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005794 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005795 }
5796
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005798 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005799 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005800 out_flush();
5801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 else if (eap->cmdidx == CMD_echoerr)
5803 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005804 int save_did_emsg = did_emsg;
5805
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005806 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005807 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 if (!force_abort)
5809 did_emsg = save_did_emsg;
5810 }
5811 else if (eap->cmdidx == CMD_execute)
5812 do_cmdline((char_u *)ga.ga_data,
5813 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
5814 }
5815
5816 ga_clear(&ga);
5817
5818 if (eap->skip)
5819 --emsg_skip;
5820
5821 eap->nextcmd = check_nextcmd(arg);
5822}
5823
5824/*
5825 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
5826 * "arg" points to the "&" or '+' when called, to "option" when returning.
5827 * Returns NULL when no option name found. Otherwise pointer to the char
5828 * after the option name.
5829 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005830 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005831find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832{
5833 char_u *p = *arg;
5834
5835 ++p;
5836 if (*p == 'g' && p[1] == ':')
5837 {
5838 *opt_flags = OPT_GLOBAL;
5839 p += 2;
5840 }
5841 else if (*p == 'l' && p[1] == ':')
5842 {
5843 *opt_flags = OPT_LOCAL;
5844 p += 2;
5845 }
5846 else
5847 *opt_flags = 0;
5848
5849 if (!ASCII_ISALPHA(*p))
5850 return NULL;
5851 *arg = p;
5852
5853 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005854 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 else
5856 while (ASCII_ISALPHA(*p))
5857 ++p;
5858 return p;
5859}
5860
5861/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00005862 * Display script name where an item was last set.
5863 * Should only be invoked when 'verbose' is non-zero.
5864 */
5865 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005866last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005867{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005868 char_u *p;
5869
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005870 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005871 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005872 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005873 if (p != NULL)
5874 {
5875 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01005876 msg_puts(_("\n\tLast set from "));
5877 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005878 if (script_ctx.sc_lnum > 0)
5879 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01005880 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005881 msg_outnum((long)script_ctx.sc_lnum);
5882 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005883 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005884 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005885 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00005886 }
5887}
5888
Bram Moolenaarb005cd82019-09-04 15:54:55 +02005889#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890
5891/*
5892 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005893 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 * "flags" can be "g" to do a global substitute.
5895 * Returns an allocated string, NULL for error.
5896 */
5897 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005898do_string_sub(
5899 char_u *str,
5900 char_u *pat,
5901 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005902 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01005903 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904{
5905 int sublen;
5906 regmatch_T regmatch;
5907 int i;
5908 int do_all;
5909 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005910 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 garray_T ga;
5912 char_u *ret;
5913 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005914 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005916 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005918 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919
5920 ga_init2(&ga, 1, 200);
5921
5922 do_all = (flags[0] == 'g');
5923
5924 regmatch.rm_ic = p_ic;
5925 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
5926 if (regmatch.regprog != NULL)
5927 {
5928 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005929 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
5931 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005932 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01005933 if (regmatch.startp[0] == regmatch.endp[0])
5934 {
5935 if (zero_width == regmatch.startp[0])
5936 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005937 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02005938 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02005939 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
5940 (size_t)i);
5941 ga.ga_len += i;
5942 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005943 continue;
5944 }
5945 zero_width = regmatch.startp[0];
5946 }
5947
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 /*
5949 * Get some space for a temporary buffer to do the substitution
5950 * into. It will contain:
5951 * - The text up to where the match is.
5952 * - The substituted text.
5953 * - The text after the match.
5954 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005955 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01005956 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
5958 {
5959 ga_clear(&ga);
5960 break;
5961 }
5962
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005963 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 i = (int)(regmatch.startp[0] - tail);
5965 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005966 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005967 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 + ga.ga_len + i, TRUE, TRUE, FALSE);
5969 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02005970 tail = regmatch.endp[0];
5971 if (*tail == NUL)
5972 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 if (!do_all)
5974 break;
5975 }
5976
5977 if (ga.ga_data != NULL)
5978 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
5979
Bram Moolenaar473de612013-06-08 18:19:48 +02005980 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 }
5982
5983 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
5984 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005985 if (p_cpo == empty_option)
5986 p_cpo = save_cpo;
5987 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005988 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005989 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990
5991 return ret;
5992}