blob: 8115c7cb023e778947f318279e5c0944dac5f067 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010023#define NAMESPACE_CHAR (char_u *)"abglstvw"
24
Bram Moolenaar532c7802005-01-27 14:44:31 +000025/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000026 * When recursively copying lists and dicts we need to remember which ones we
27 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000028 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000029 */
30static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020031
Bram Moolenaar071d4272004-06-13 20:20:40 +000032/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000033 * Info used by a ":for" loop.
34 */
Bram Moolenaar33570922005-01-25 22:26:29 +000035typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000036{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010037 int fi_semicolon; // TRUE if ending in '; var]'
38 int fi_varcount; // nr of variables in the list
Bram Moolenaarb7a78f72020-06-28 18:43:40 +020039 int fi_break_count; // nr of line breaks encountered
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010040 listwatch_T fi_lw; // keep an eye on the item used.
41 list_T *fi_list; // list being used
42 int fi_bi; // index of blob
43 blob_T *fi_blob; // blob being used
Bram Moolenaar33570922005-01-25 22:26:29 +000044} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000045
Bram Moolenaar48e697e2016-01-23 22:17:30 +010046static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020047static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
48static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
49static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
50static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
51static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
52static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +020053static int eval7_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000054
Bram Moolenaar48e697e2016-01-23 22:17:30 +010055static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020056static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020057
Bram Moolenaare21c1582019-03-02 11:57:09 +010058/*
59 * Return "n1" divided by "n2", taking care of dividing by zero.
60 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020061 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010062num_divide(varnumber_T n1, varnumber_T n2)
63{
64 varnumber_T result;
65
66 if (n2 == 0) // give an error message?
67 {
68 if (n1 == 0)
69 result = VARNUM_MIN; // similar to NaN
70 else if (n1 < 0)
71 result = -VARNUM_MAX;
72 else
73 result = VARNUM_MAX;
74 }
75 else
76 result = n1 / n2;
77
78 return result;
79}
80
81/*
82 * Return "n1" modulus "n2", taking care of dividing by zero.
83 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020084 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010085num_modulus(varnumber_T n1, varnumber_T n2)
86{
87 // Give an error when n2 is 0?
88 return (n2 == 0) ? 0 : (n1 % n2);
89}
90
Bram Moolenaara1fa8922017-01-12 20:06:33 +010091#if defined(EBCDIC) || defined(PROTO)
92/*
93 * Compare struct fst by function name.
94 */
95 static int
96compare_func_name(const void *s1, const void *s2)
97{
98 struct fst *p1 = (struct fst *)s1;
99 struct fst *p2 = (struct fst *)s2;
100
101 return STRCMP(p1->f_name, p2->f_name);
102}
103
104/*
105 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100106 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100107 * On machines using EBCDIC we have to sort it.
108 */
109 static void
110sortFunctions(void)
111{
112 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
113
114 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
115}
116#endif
117
Bram Moolenaar33570922005-01-25 22:26:29 +0000118/*
119 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000120 */
121 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100122eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000123{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200124 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200125 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000126
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200127#ifdef EBCDIC
128 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100129 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200130 */
131 sortFunctions();
132#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000133}
134
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000135#if defined(EXITFREE) || defined(PROTO)
136 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100137eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000138{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200139 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100140 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200141 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000142
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200143 // autoloaded script names
144 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000145
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200146 // unreferenced lists and dicts
147 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200148
149 // functions not garbage collected
150 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000151}
152#endif
153
Bram Moolenaare6b53242020-07-01 17:28:33 +0200154 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200155fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
156{
157 CLEAR_FIELD(*evalarg);
158 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
159 if (eap != NULL && getline_equal(eap->getline, eap->cookie, getsourceline))
160 {
161 evalarg->eval_getline = eap->getline;
162 evalarg->eval_cookie = eap->cookie;
163 }
164}
165
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166/*
167 * Top level evaluation function, returning a boolean.
168 * Sets "error" to TRUE if there was an error.
169 * Return TRUE or FALSE.
170 */
171 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100172eval_to_bool(
173 char_u *arg,
174 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200175 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100176 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177{
Bram Moolenaar33570922005-01-25 22:26:29 +0000178 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200179 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200180 evalarg_T evalarg;
181
Bram Moolenaar37c83712020-06-30 21:18:36 +0200182 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183
184 if (skip)
185 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200186 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 else
189 {
190 *error = FALSE;
191 if (!skip)
192 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200193 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200194 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200195 else
196 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000197 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 }
199 }
200 if (skip)
201 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200202 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200204 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205}
206
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100207/*
208 * Call eval1() and give an error message if not done at a lower level.
209 */
210 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200211eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100212{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100213 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100214 int ret;
215 int did_emsg_before = did_emsg;
216 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200217 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100218
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200219 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
220
221 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100222 if (ret == FAIL)
223 {
224 // Report the invalid expression unless the expression evaluation has
225 // been cancelled due to an aborting error, an interrupt, or an
226 // exception, or we already gave a more specific error.
227 // Also check called_emsg for when using assert_fails().
228 if (!aborting() && did_emsg == did_emsg_before
229 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100230 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100231 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200232 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100233 return ret;
234}
235
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100236/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200237 * Return whether a typval is a valid expression to pass to eval_expr_typval()
238 * or eval_expr_to_bool(). An empty string returns FALSE;
239 */
240 int
241eval_expr_valid_arg(typval_T *tv)
242{
243 return tv->v_type != VAR_UNKNOWN
244 && (tv->v_type != VAR_STRING
245 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
246}
247
248/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100249 * Evaluate an expression, which can be a function, partial or string.
250 * Pass arguments "argv[argc]".
251 * Return the result in "rettv" and OK or FAIL.
252 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200253 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100254eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
255{
256 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100257 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200258 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100259
260 if (expr->v_type == VAR_FUNC)
261 {
262 s = expr->vval.v_string;
263 if (s == NULL || *s == NUL)
264 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200265 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200266 funcexe.evaluate = TRUE;
267 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100268 return FAIL;
269 }
270 else if (expr->v_type == VAR_PARTIAL)
271 {
272 partial_T *partial = expr->vval.v_partial;
273
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200274 if (partial == NULL)
275 return FAIL;
276
Bram Moolenaar822ba242020-05-24 23:00:18 +0200277 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200278 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100279 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200280 if (call_def_function(partial->pt_func, argc, argv,
281 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100282 return FAIL;
283 }
284 else
285 {
286 s = partial_name(partial);
287 if (s == NULL || *s == NUL)
288 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200289 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100290 funcexe.evaluate = TRUE;
291 funcexe.partial = partial;
292 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
293 return FAIL;
294 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100295 }
296 else
297 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100298 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100299 if (s == NULL)
300 return FAIL;
301 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200302 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100303 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200304 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100305 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200306 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100307 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100308 return FAIL;
309 }
310 }
311 return OK;
312}
313
314/*
315 * Like eval_to_bool() but using a typval_T instead of a string.
316 * Works for string, funcref and partial.
317 */
318 int
319eval_expr_to_bool(typval_T *expr, int *error)
320{
321 typval_T rettv;
322 int res;
323
324 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
325 {
326 *error = TRUE;
327 return FALSE;
328 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200329 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100330 clear_tv(&rettv);
331 return res;
332}
333
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334/*
335 * Top level evaluation function, returning a string. If "skip" is TRUE,
336 * only parsing to "nextcmd" is done, without reporting errors. Return
337 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
338 */
339 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100340eval_to_string_skip(
341 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200342 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100343 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344{
Bram Moolenaar33570922005-01-25 22:26:29 +0000345 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200347 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348
Bram Moolenaar37c83712020-06-30 21:18:36 +0200349 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 if (skip)
351 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200352 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 retval = NULL;
354 else
355 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100356 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000357 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 }
359 if (skip)
360 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200361 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362
363 return retval;
364}
365
366/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000367 * Skip over an expression at "*pp".
368 * Return FAIL for an error, OK otherwise.
369 */
370 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200371skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000372{
Bram Moolenaar33570922005-01-25 22:26:29 +0000373 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000374
375 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200376 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000377}
378
379/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200380 * Skip over an expression at "*pp".
381 * If in Vim9 script and line breaks are encountered, the lines are
382 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200383 * "arg" is advanced to just after the expression.
384 * "start" is set to the start of the expression, "end" to just after the end.
385 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200386 * Return FAIL for an error, OK otherwise.
387 */
388 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200389skip_expr_concatenate(
390 char_u **arg,
391 char_u **start,
392 char_u **end,
393 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200394{
395 typval_T rettv;
396 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200397 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200398 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200399 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200400 int evaluate = evalarg == NULL
401 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200402
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200403 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200404 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200405 {
406 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200407 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200408 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200409 ++gap->ga_len;
410 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200411 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200412
413 // Don't evaluate the expression.
414 if (evalarg != NULL)
415 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200416 *arg = skipwhite(*arg);
417 res = eval1(arg, &rettv, evalarg);
418 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200419 if (evalarg != NULL)
420 evalarg->eval_flags = save_flags;
421
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200422 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200423 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200424 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200425 if (evalarg->eval_ga.ga_len == 1)
426 {
427 // just one line, no need to concatenate
428 ga_clear(gap);
429 gap->ga_itemsize = 0;
430 }
431 else
432 {
433 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200434 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200435
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200436 // Line breaks encountered, concatenate all the lines.
437 *((char_u **)gap->ga_data) = *start;
438 p = ga_concat_strings(gap, "");
439
440 // free the lines only when using getsourceline()
441 if (evalarg->eval_cookie != NULL)
442 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200443 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200444 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200445 // Do not free the last line, "arg" points into it, free it
446 // later.
447 vim_free(evalarg->eval_tofree);
448 evalarg->eval_tofree =
449 ((char_u **)gap->ga_data)[gap->ga_len - 1];
450 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200451 ga_clear_strings(gap);
452 }
453 else
454 ga_clear(gap);
455 gap->ga_itemsize = 0;
456 if (p == NULL)
457 return FAIL;
458 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200459 vim_free(evalarg->eval_tofree_lambda);
460 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200461 // Compute "end" relative to the end.
462 *end = *start + STRLEN(*start) - endoff;
463 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200464 }
465
466 return res;
467}
468
469/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200470 * Top level evaluation function, returning a string. Does not handle line
471 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000472 * When "convert" is TRUE convert a List into a sequence of lines and convert
473 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 * Return pointer to allocated memory, or NULL for failure.
475 */
476 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100477eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100478 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100479 int convert,
480 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481{
Bram Moolenaar33570922005-01-25 22:26:29 +0000482 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000484 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000485#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000486 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000487#endif
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100488 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100490 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
491 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 retval = NULL;
493 else
494 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000495 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000496 {
497 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000498 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200499 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200500 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200501 if (tv.vval.v_list->lv_len > 0)
502 ga_append(&ga, NL);
503 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000504 ga_append(&ga, NUL);
505 retval = (char_u *)ga.ga_data;
506 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000507#ifdef FEAT_FLOAT
508 else if (convert && tv.v_type == VAR_FLOAT)
509 {
510 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
511 retval = vim_strsave(numbuf);
512 }
513#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000514 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100515 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000516 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100518 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519
520 return retval;
521}
522
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100523 char_u *
524eval_to_string(
525 char_u *arg,
526 int convert)
527{
528 return eval_to_string_eap(arg, convert, NULL);
529}
530
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000532 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200533 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200534 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 */
536 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100537eval_to_string_safe(
538 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100539 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540{
541 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200542 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200543 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200545 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200546 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000547 if (use_sandbox)
548 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200549 ++textwinlock;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200550 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000551 if (use_sandbox)
552 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200553 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200554 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200555 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 return retval;
557}
558
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559/*
560 * Top level evaluation function, returning a number.
561 * Evaluates "expr" silently.
562 * Returns -1 for an error.
563 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200564 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100565eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566{
Bram Moolenaar33570922005-01-25 22:26:29 +0000567 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200568 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000569 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570
571 ++emsg_off;
572
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200573 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 retval = -1;
575 else
576 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100577 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000578 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 }
580 --emsg_off;
581
582 return retval;
583}
584
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000585/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000586 * Top level evaluation function.
587 * Returns an allocated typval_T with the result.
588 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000589 */
590 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200591eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000592{
593 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200594 evalarg_T evalarg;
595
596 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000597
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200598 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200599 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100600 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000601
Bram Moolenaar37c83712020-06-30 21:18:36 +0200602 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000603 return tv;
604}
605
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100607 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200608 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
609 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000610 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200612 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100613call_vim_function(
614 char_u *func,
615 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200616 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200617 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000619 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200620 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100622 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200623 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200624 funcexe.firstline = curwin->w_cursor.lnum;
625 funcexe.lastline = curwin->w_cursor.lnum;
626 funcexe.evaluate = TRUE;
627 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000628 if (ret == FAIL)
629 clear_tv(rettv);
630
631 return ret;
632}
633
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100634/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100635 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100636 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200637 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
638 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100639 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200640 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100641call_func_retnr(
642 char_u *func,
643 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200644 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100645{
646 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200647 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100648
Bram Moolenaarded27a12018-08-01 19:06:03 +0200649 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100650 return -1;
651
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100652 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100653 clear_tv(&rettv);
654 return retval;
655}
656
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000657/*
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100658 * Call Vim script function like call_func_retnr() and drop the result.
659 * Returns FAIL when calling the function fails.
660 */
661 int
662call_func_noret(
663 char_u *func,
664 int argc,
665 typval_T *argv)
666{
667 typval_T rettv;
668
669 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
670 return FAIL;
671 clear_tv(&rettv);
672 return OK;
673}
674
675/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100676 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100677 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000678 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000679 */
680 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100681call_func_retstr(
682 char_u *func,
683 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200684 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000685{
686 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000687 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000688
Bram Moolenaarded27a12018-08-01 19:06:03 +0200689 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000690 return NULL;
691
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100692 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000693 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 return retval;
695}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000696
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000697/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100698 * Call Vim script function "func" and return the result as a List.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100699 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000700 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000701 */
702 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100703call_func_retlist(
704 char_u *func,
705 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200706 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000707{
708 typval_T rettv;
709
Bram Moolenaarded27a12018-08-01 19:06:03 +0200710 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000711 return NULL;
712
713 if (rettv.v_type != VAR_LIST)
714 {
715 clear_tv(&rettv);
716 return NULL;
717 }
718
719 return rettv.vval.v_list;
720}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722#ifdef FEAT_FOLDING
723/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100724 * Evaluate "arg", which is 'foldexpr'.
725 * Note: caller must set "curwin" to match "arg".
726 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
727 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 */
729 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100730eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731{
Bram Moolenaar33570922005-01-25 22:26:29 +0000732 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200733 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000735 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
736 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737
738 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000739 if (use_sandbox)
740 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200741 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200743 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 retval = 0;
745 else
746 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100747 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000748 if (tv.v_type == VAR_NUMBER)
749 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000750 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 retval = 0;
752 else
753 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100754 // If the result is a string, check if there is a non-digit before
755 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000756 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 if (!VIM_ISDIGIT(*s) && *s != '-')
758 *cp = *s++;
759 retval = atol((char *)s);
760 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000761 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 }
763 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000764 if (use_sandbox)
765 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200766 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200767 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200769 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770}
771#endif
772
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000774 * Get an lval: variable, Dict item or List item that can be assigned a value
775 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
776 * "name.key", "name.key[expr]" etc.
777 * Indexing only works if "name" is an existing List or Dictionary.
778 * "name" points to the start of the name.
779 * If "rettv" is not NULL it points to the value to be assigned.
780 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
781 * wrong; must end in space or cmd separator.
782 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100783 * flags:
784 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100785 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100786 * GLV_NO_AUTOLOAD: do not use script autoloading
787 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000788 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000789 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000790 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000791 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200792 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100793get_lval(
794 char_u *name,
795 typval_T *rettv,
796 lval_T *lp,
797 int unlet,
798 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100799 int flags, // GLV_ values
800 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000801{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000802 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000803 char_u *expr_start, *expr_end;
804 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000805 dictitem_T *v;
806 typval_T var1;
807 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000808 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000809 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000810 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000811 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100812 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100813 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100814 int writing;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000815
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100816 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200817 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000818
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100819 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000820 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100821 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000822 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200823 lp->ll_name_end = find_name_end(name, NULL, NULL,
824 FNE_INCL_BR | fne_flags);
825 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000826 }
827
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100828 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000829 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100830 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000831 if (expr_start != NULL)
832 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100833 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100834 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000835 && *p != '[' && *p != '.')
836 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200837 semsg(_(e_trailing_arg), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000838 return NULL;
839 }
840
841 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
842 if (lp->ll_exp_name == NULL)
843 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100844 // Report an invalid expression in braces, unless the
845 // expression evaluation has been cancelled due to an
846 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000847 if (!aborting() && !quiet)
848 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000849 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100850 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000851 return NULL;
852 }
853 }
854 lp->ll_name = lp->ll_exp_name;
855 }
856 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100857 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000858 lp->ll_name = name;
859
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200860 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100861 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200862 // "a: type" is declaring variable "a" with a type, not "a:".
863 if (p == name + 2 && p[-1] == ':')
864 {
865 --p;
866 lp->ll_name_end = p;
867 }
868 if (*p == ':')
869 {
870 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
871 char_u *tp = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100872
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200873 // parse the type after the name
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100874 lp->ll_type = parse_type(&tp, &si->sn_type_list, !quiet);
875 if (lp->ll_type == NULL && !quiet)
876 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200877 lp->ll_name_end = tp;
878 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100879 }
880 }
881
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100882 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000883 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
884 return p;
885
886 cc = *p;
887 *p = NUL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100888 // When we would write to the variable pass &ht and prevent autoload.
889 writing = !(flags & GLV_READ_ONLY);
890 v = find_var(lp->ll_name, writing ? &ht : NULL,
891 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000892 if (v == NULL && !quiet)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200893 semsg(_(e_undefined_variable_str), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000894 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000895 if (v == NULL)
896 return NULL;
897
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +0100898 if (in_vim9script() && (flags & GLV_NO_DECL) == 0)
899 {
900 if (!quiet)
901 semsg(_(e_variable_already_declared), lp->ll_name);
902 return NULL;
903 }
904
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000905 /*
906 * Loop until no more [idx] or .key is following.
907 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000908 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100909 var1.v_type = VAR_UNKNOWN;
910 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000911 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000912 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000913 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
Bram Moolenaar81ed4962020-09-23 15:56:50 +0200914 && !(lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100915 && !(lp->ll_tv->v_type == VAR_BLOB
916 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000917 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000918 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100919 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000920 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000921 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000922 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000923 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000924 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100925 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000926 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000927 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000928
Bram Moolenaar10c65862020-10-08 21:16:42 +0200929 if (in_vim9script() && lp->ll_valtype == NULL
930 && lp->ll_tv == &v->di_tv
931 && ht != NULL && ht == get_script_local_ht())
932 {
933 svar_T *sv = find_typval_in_script(lp->ll_tv);
934
935 // Vim9 script local variable: get the type
936 if (sv != NULL)
937 lp->ll_valtype = sv->sv_type;
938 }
939
Bram Moolenaar8c711452005-01-14 21:53:12 +0000940 len = -1;
941 if (*p == '.')
942 {
943 key = p + 1;
944 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
945 ;
946 if (len == 0)
947 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000948 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100949 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000950 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000951 }
952 p = key + len;
953 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000954 else
955 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100956 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000957 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000958 if (*p == ':')
959 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000960 else
961 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000962 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200963 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000964 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100965 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000966 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100967 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000968 clear_tv(&var1);
969 return NULL;
970 }
Bram Moolenaar6a250262020-08-04 15:53:01 +0200971 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000972 }
973
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100974 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000975 if (*p == ':')
976 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000977 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000978 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000979 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +0200980 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100981 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000982 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000983 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100984 if (rettv != NULL
985 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100986 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100987 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100988 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +0000989 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000990 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100991 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100992 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000993 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000994 }
995 p = skipwhite(p + 1);
996 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000997 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000998 else
999 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001000 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001001 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001002 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001003 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001004 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001005 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001006 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001007 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001008 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001009 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001010 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001011 clear_tv(&var2);
1012 return NULL;
1013 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001014 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001015 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001016 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001017 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001018 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001019
Bram Moolenaar8c711452005-01-14 21:53:12 +00001020 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001021 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001022 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001023 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001024 clear_tv(&var1);
1025 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001026 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001027 }
1028
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001029 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001030 ++p;
1031 }
1032
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001033 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001034 {
1035 if (len == -1)
1036 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001037 // "[key]": get key from "var1"
1038 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001039 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001040 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001041 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001042 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001043 }
1044 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001045 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001046
1047 // a NULL dict is equivalent with an empty dict
1048 if (lp->ll_tv->vval.v_dict == NULL)
1049 {
1050 lp->ll_tv->vval.v_dict = dict_alloc();
1051 if (lp->ll_tv->vval.v_dict == NULL)
1052 {
1053 clear_tv(&var1);
1054 return NULL;
1055 }
1056 ++lp->ll_tv->vval.v_dict->dv_refcount;
1057 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001058 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001059
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001060 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001061
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001062 // When assigning to a scope dictionary check that a function and
1063 // variable name is valid (only variable name unless it is l: or
1064 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001065 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001066 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001067 int prevval;
1068 int wrong;
1069
1070 if (len != -1)
1071 {
1072 prevval = key[len];
1073 key[len] = NUL;
1074 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001075 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001076 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001077 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1078 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001079 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar03290b82020-12-19 16:30:44 +01001080 || !valid_varname(key, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001081 if (len != -1)
1082 key[len] = prevval;
1083 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001084 {
1085 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001086 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001087 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001088 }
1089
Bram Moolenaar10c65862020-10-08 21:16:42 +02001090 if (lp->ll_valtype != NULL)
1091 // use the type of the member
1092 lp->ll_valtype = lp->ll_valtype->tt_member;
1093
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001094 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001095 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001096 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001097 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001098 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001099 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001100 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001101 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001102 return NULL;
1103 }
1104
Bram Moolenaar31b81602019-02-10 22:14:27 +01001105 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001106 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001107 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001108 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001109 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001110 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001111 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001112 }
1113 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001114 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001115 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001116 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001117 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001118 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001119 p = NULL;
1120 break;
1121 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001122 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001123 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001124 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1125 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001126 {
1127 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001128 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001129 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001130
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001131 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001132 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001133 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001134 else if (lp->ll_tv->v_type == VAR_BLOB)
1135 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001136 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1137
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001138 /*
1139 * Get the number and item for the only or first index of the List.
1140 */
1141 if (empty1)
1142 lp->ll_n1 = 0;
1143 else
1144 // is number or string
1145 lp->ll_n1 = (long)tv_get_number(&var1);
1146 clear_tv(&var1);
1147
1148 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001149 || lp->ll_n1 > bloblen
1150 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001151 {
1152 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001153 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001154 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001155 return NULL;
1156 }
1157 if (lp->ll_range && !lp->ll_empty2)
1158 {
1159 lp->ll_n2 = (long)tv_get_number(&var2);
1160 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001161 if (lp->ll_n2 < 0
1162 || lp->ll_n2 >= bloblen
1163 || lp->ll_n2 < lp->ll_n1)
1164 {
1165 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001166 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001167 return NULL;
1168 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001169 }
1170 lp->ll_blob = lp->ll_tv->vval.v_blob;
1171 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001172 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001173 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001174 else
1175 {
1176 /*
1177 * Get the number and item for the only or first index of the List.
1178 */
1179 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001180 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001181 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001182 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001183 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001184 clear_tv(&var1);
1185
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001186 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001187 lp->ll_list = lp->ll_tv->vval.v_list;
1188 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1189 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001190 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001191 if (lp->ll_n1 < 0)
1192 {
1193 lp->ll_n1 = 0;
1194 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1195 }
1196 }
1197 if (lp->ll_li == NULL)
1198 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001199 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001200 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001201 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001202 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001203 }
1204
Bram Moolenaar10c65862020-10-08 21:16:42 +02001205 if (lp->ll_valtype != NULL)
1206 // use the type of the member
1207 lp->ll_valtype = lp->ll_valtype->tt_member;
1208
Bram Moolenaar8c711452005-01-14 21:53:12 +00001209 /*
1210 * May need to find the item or absolute index for the second
1211 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001212 * When no index given: "lp->ll_empty2" is TRUE.
1213 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001214 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001215 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001216 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001217 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001218 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001219 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001220 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001221 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001222 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001223 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001224 {
1225 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001226 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001227 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001228 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001229 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001230 }
1231
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001232 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001233 if (lp->ll_n1 < 0)
1234 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1235 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001236 {
1237 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001238 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001239 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001240 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001241 }
1242
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001243 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001244 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001245 }
1246
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001247 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001248 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001249 return p;
1250}
1251
1252/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001253 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001254 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001255 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001256clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001257{
1258 vim_free(lp->ll_exp_name);
1259 vim_free(lp->ll_newkey);
1260}
1261
1262/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001263 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001264 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001265 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1266 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001267 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001268 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001269set_var_lval(
1270 lval_T *lp,
1271 char_u *endp,
1272 typval_T *rettv,
1273 int copy,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001274 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar7454a062016-01-30 15:14:10 +01001275 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001276{
1277 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001278 listitem_T *ri;
1279 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001280
1281 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001282 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001283 cc = *endp;
1284 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001285 if (lp->ll_blob != NULL)
1286 {
1287 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001288
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001289 if (op != NULL && *op != '=')
1290 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001291 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001292 return;
1293 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001294 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001295 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001296
1297 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1298 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001299 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001300
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001301 if (lp->ll_empty2)
1302 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1303
1304 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001305 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001306 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001307 return;
1308 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001309 if (lp->ll_empty2)
1310 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001311
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001312 ir = 0;
1313 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1314 blob_set(lp->ll_blob, il,
1315 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001316 }
1317 else
1318 {
1319 val = (int)tv_get_number_chk(rettv, &error);
1320 if (!error)
1321 {
1322 garray_T *gap = &lp->ll_blob->bv_ga;
1323
1324 // Allow for appending a byte. Setting a byte beyond
1325 // the end is an error otherwise.
1326 if (lp->ll_n1 < gap->ga_len
1327 || (lp->ll_n1 == gap->ga_len
1328 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1329 {
1330 blob_set(lp->ll_blob, lp->ll_n1, val);
1331 if (lp->ll_n1 == gap->ga_len)
1332 ++gap->ga_len;
1333 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001334 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001335 }
1336 }
1337 }
1338 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001339 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001340 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001341
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001342 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar9937a052019-06-15 15:45:06 +02001343 {
1344 emsg(_(e_cannot_mod));
1345 *endp = cc;
1346 return;
1347 }
1348
Bram Moolenaarff697e62019-02-12 22:28:33 +01001349 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001350 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001351 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar79518e22017-02-17 16:31:35 +01001352 &tv, &di, TRUE, FALSE) == OK)
1353 {
1354 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001355 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1356 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001357 && tv_op(&tv, rettv, op) == OK)
1358 set_var(lp->ll_name, &tv, FALSE);
1359 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001360 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001361 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001362 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001363 {
1364 if (lp->ll_type != NULL
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001365 && check_typval_type(lp->ll_type, rettv, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001366 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001367 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001368 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001369 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001370 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001371 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001372 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001373 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001374 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001375 else if (lp->ll_range)
1376 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001377 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001378 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001379
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001380 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar9937a052019-06-15 15:45:06 +02001381 {
1382 emsg(_("E996: Cannot lock a range"));
1383 return;
1384 }
1385
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001386 /*
1387 * Check whether any of the list items is locked
1388 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001389 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001390 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001391 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001392 return;
1393 ri = ri->li_next;
1394 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1395 break;
1396 ll_li = ll_li->li_next;
1397 ++ll_n1;
1398 }
1399
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001400 /*
1401 * Assign the List values to the list items.
1402 */
1403 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001404 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001405 if (op != NULL && *op != '=')
1406 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1407 else
1408 {
1409 clear_tv(&lp->ll_li->li_tv);
1410 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1411 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001412 ri = ri->li_next;
1413 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1414 break;
1415 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001416 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001417 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001418 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001419 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001420 ri = NULL;
1421 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001422 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001423 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001424 lp->ll_li = lp->ll_li->li_next;
1425 ++lp->ll_n1;
1426 }
1427 if (ri != NULL)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001428 emsg(_(e_list_value_has_more_items_than_targets));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001429 else if (lp->ll_empty2
1430 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001431 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001432 emsg(_(e_list_value_does_not_have_enough_items));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001433 }
1434 else
1435 {
1436 /*
1437 * Assign to a List or Dictionary item.
1438 */
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001439 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar9937a052019-06-15 15:45:06 +02001440 {
1441 emsg(_("E996: Cannot lock a list or dict"));
1442 return;
1443 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001444
1445 if (lp->ll_valtype != NULL
1446 && check_typval_type(lp->ll_valtype, rettv, 0) == FAIL)
1447 return;
1448
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001449 if (lp->ll_newkey != NULL)
1450 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001451 if (op != NULL && *op != '=')
1452 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001453 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001454 return;
1455 }
1456
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001457 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001458 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001459 if (di == NULL)
1460 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001461 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1462 {
1463 vim_free(di);
1464 return;
1465 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001466 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001467 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001468 else if (op != NULL && *op != '=')
1469 {
1470 tv_op(lp->ll_tv, rettv, op);
1471 return;
1472 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001473 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001474 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001475
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001476 /*
1477 * Assign the value to the variable or list item.
1478 */
1479 if (copy)
1480 copy_tv(rettv, lp->ll_tv);
1481 else
1482 {
1483 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001484 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001485 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001486 }
1487 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001488}
1489
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001490/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001491 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1492 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001493 * Returns OK or FAIL.
1494 */
1495 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001496tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001497{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001498 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001499 char_u numbuf[NUMBUFLEN];
1500 char_u *s;
1501
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001502 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001503 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001504 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001505 {
1506 switch (tv1->v_type)
1507 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001508 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001509 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001510 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001511 case VAR_DICT:
1512 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001513 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001514 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001515 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001516 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001517 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001518 break;
1519
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001520 case VAR_BLOB:
1521 if (*op != '+' || tv2->v_type != VAR_BLOB)
1522 break;
1523 // BLOB += BLOB
1524 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1525 {
1526 blob_T *b1 = tv1->vval.v_blob;
1527 blob_T *b2 = tv2->vval.v_blob;
1528 int i, len = blob_len(b2);
1529 for (i = 0; i < len; i++)
1530 ga_append(&b1->bv_ga, blob_get(b2, i));
1531 }
1532 return OK;
1533
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001534 case VAR_LIST:
1535 if (*op != '+' || tv2->v_type != VAR_LIST)
1536 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001537 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001538 if (tv2->vval.v_list != NULL)
1539 {
1540 if (tv1->vval.v_list == NULL)
1541 {
1542 tv1->vval.v_list = tv2->vval.v_list;
1543 ++tv1->vval.v_list->lv_refcount;
1544 }
1545 else
1546 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1547 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001548 return OK;
1549
1550 case VAR_NUMBER:
1551 case VAR_STRING:
1552 if (tv2->v_type == VAR_LIST)
1553 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001554 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001555 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001556 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001557 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001558#ifdef FEAT_FLOAT
1559 if (tv2->v_type == VAR_FLOAT)
1560 {
1561 float_T f = n;
1562
Bram Moolenaarff697e62019-02-12 22:28:33 +01001563 if (*op == '%')
1564 break;
1565 switch (*op)
1566 {
1567 case '+': f += tv2->vval.v_float; break;
1568 case '-': f -= tv2->vval.v_float; break;
1569 case '*': f *= tv2->vval.v_float; break;
1570 case '/': f /= tv2->vval.v_float; break;
1571 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001572 clear_tv(tv1);
1573 tv1->v_type = VAR_FLOAT;
1574 tv1->vval.v_float = f;
1575 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001576 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001577#endif
1578 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001579 switch (*op)
1580 {
1581 case '+': n += tv_get_number(tv2); break;
1582 case '-': n -= tv_get_number(tv2); break;
1583 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001584 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1585 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001586 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001587 clear_tv(tv1);
1588 tv1->v_type = VAR_NUMBER;
1589 tv1->vval.v_number = n;
1590 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001591 }
1592 else
1593 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001594 if (tv2->v_type == VAR_FLOAT)
1595 break;
1596
Bram Moolenaarff697e62019-02-12 22:28:33 +01001597 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001598 s = tv_get_string(tv1);
1599 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001600 clear_tv(tv1);
1601 tv1->v_type = VAR_STRING;
1602 tv1->vval.v_string = s;
1603 }
1604 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001605
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001606 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001607#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001608 {
1609 float_T f;
1610
Bram Moolenaarff697e62019-02-12 22:28:33 +01001611 if (*op == '%' || *op == '.'
1612 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001613 && tv2->v_type != VAR_NUMBER
1614 && tv2->v_type != VAR_STRING))
1615 break;
1616 if (tv2->v_type == VAR_FLOAT)
1617 f = tv2->vval.v_float;
1618 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001619 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001620 switch (*op)
1621 {
1622 case '+': tv1->vval.v_float += f; break;
1623 case '-': tv1->vval.v_float -= f; break;
1624 case '*': tv1->vval.v_float *= f; break;
1625 case '/': tv1->vval.v_float /= f; break;
1626 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001627 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001628#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001629 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001630 }
1631 }
1632
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001633 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001634 return FAIL;
1635}
1636
1637/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001638 * Evaluate the expression used in a ":for var in expr" command.
1639 * "arg" points to "var".
1640 * Set "*errp" to TRUE for an error, FALSE otherwise;
1641 * Return a pointer that holds the info. Null when there is an error.
1642 */
1643 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001644eval_for_line(
1645 char_u *arg,
1646 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001647 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001648 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001649{
Bram Moolenaar33570922005-01-25 22:26:29 +00001650 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001651 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001652 typval_T tv;
1653 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001654 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001655
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001656 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001657
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001658 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001659 if (fi == NULL)
1660 return NULL;
1661
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001662 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001663 if (expr == NULL)
1664 return fi;
1665
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001666 expr = skipwhite_and_linebreak(expr, evalarg);
1667 if (expr[0] != 'i' || expr[1] != 'n'
1668 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001669 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001670 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001671 return fi;
1672 }
1673
1674 if (skip)
1675 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001676 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1677 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001678 {
1679 *errp = FALSE;
1680 if (!skip)
1681 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001682 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001683 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001684 l = tv.vval.v_list;
1685 if (l == NULL)
1686 {
1687 // a null list is like an empty list: do nothing
1688 clear_tv(&tv);
1689 }
1690 else
1691 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001692 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001693 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001694
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001695 // No need to increment the refcount, it's already set for
1696 // the list being used in "tv".
1697 fi->fi_list = l;
1698 list_add_watch(l, &fi->fi_lw);
1699 fi->fi_lw.lw_item = l->lv_first;
1700 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001701 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001702 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001703 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001704 fi->fi_bi = 0;
1705 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001706 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001707 typval_T btv;
1708
1709 // Make a copy, so that the iteration still works when the
1710 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001711 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001712 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001713 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001714 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001715 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001716 else
1717 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001718 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001719 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001720 }
1721 }
1722 }
1723 if (skip)
1724 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001725 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001726
1727 return fi;
1728}
1729
1730/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001731 * Used when looping over a :for line, skip the "in expr" part.
1732 */
1733 void
1734skip_for_lines(void *fi_void, evalarg_T *evalarg)
1735{
1736 forinfo_T *fi = (forinfo_T *)fi_void;
1737 int i;
1738
1739 for (i = 0; i < fi->fi_break_count; ++i)
1740 eval_next_line(evalarg);
1741}
1742
1743/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001744 * Use the first item in a ":for" list. Advance to the next.
1745 * Assign the values to the variable (list). "arg" points to the first one.
1746 * Return TRUE when a valid item was found, FALSE when at end of list or
1747 * something wrong.
1748 */
1749 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001750next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001751{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001752 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001753 int result;
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001754 int flag = in_vim9script() ? ASSIGN_DECL : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001755 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001756
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001757 if (fi->fi_blob != NULL)
1758 {
1759 typval_T tv;
1760
1761 if (fi->fi_bi >= blob_len(fi->fi_blob))
1762 return FALSE;
1763 tv.v_type = VAR_NUMBER;
1764 tv.v_lock = VAR_FIXED;
1765 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1766 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001767 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001768 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001769 }
1770
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001771 item = fi->fi_lw.lw_item;
1772 if (item == NULL)
1773 result = FALSE;
1774 else
1775 {
1776 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001777 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001778 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001779 }
1780 return result;
1781}
1782
1783/*
1784 * Free the structure used to store info used by ":for".
1785 */
1786 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001787free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001788{
Bram Moolenaar33570922005-01-25 22:26:29 +00001789 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001790
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001791 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001792 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001793 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001794 list_unref(fi->fi_list);
1795 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001796 if (fi != NULL && fi->fi_blob != NULL)
1797 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001798 vim_free(fi);
1799}
1800
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001802set_context_for_expression(
1803 expand_T *xp,
1804 char_u *arg,
1805 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001807 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001809 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001811 if (cmdidx == CMD_let || cmdidx == CMD_var
1812 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001813 {
1814 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001815 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001816 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001817 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001818 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001819 {
1820 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001821 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001822 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001823 break;
1824 }
1825 return;
1826 }
1827 }
1828 else
1829 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1830 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 while ((xp->xp_pattern = vim_strpbrk(arg,
1832 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1833 {
1834 c = *xp->xp_pattern;
1835 if (c == '&')
1836 {
1837 c = xp->xp_pattern[1];
1838 if (c == '&')
1839 {
1840 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001841 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 }
1843 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001844 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001846 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1847 xp->xp_pattern += 2;
1848
1849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 }
1851 else if (c == '$')
1852 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001853 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 xp->xp_context = EXPAND_ENV_VARS;
1855 }
1856 else if (c == '=')
1857 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001858 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 xp->xp_context = EXPAND_EXPRESSION;
1860 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001861 else if (c == '#'
1862 && xp->xp_context == EXPAND_EXPRESSION)
1863 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001864 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001865 break;
1866 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001867 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 && xp->xp_context == EXPAND_FUNCTIONS
1869 && vim_strchr(xp->xp_pattern, '(') == NULL)
1870 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001871 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 break;
1873 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001874 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001876 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 {
1878 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1879 if (c == '\\' && xp->xp_pattern[1] != NUL)
1880 ++xp->xp_pattern;
1881 xp->xp_context = EXPAND_NOTHING;
1882 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001883 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001885 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1887 /* skip */ ;
1888 xp->xp_context = EXPAND_NOTHING;
1889 }
1890 else if (c == '|')
1891 {
1892 if (xp->xp_pattern[1] == '|')
1893 {
1894 ++xp->xp_pattern;
1895 xp->xp_context = EXPAND_EXPRESSION;
1896 }
1897 else
1898 xp->xp_context = EXPAND_COMMANDS;
1899 }
1900 else
1901 xp->xp_context = EXPAND_EXPRESSION;
1902 }
1903 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001904 // Doesn't look like something valid, expand as an expression
1905 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001906 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 arg = xp->xp_pattern;
1908 if (*arg != NUL)
1909 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1910 /* skip */ ;
1911 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01001912
1913 // ":exe one two" completes "two"
1914 if ((cmdidx == CMD_execute
1915 || cmdidx == CMD_echo
1916 || cmdidx == CMD_echon
1917 || cmdidx == CMD_echomsg)
1918 && xp->xp_context == EXPAND_EXPRESSION)
1919 {
1920 for (;;)
1921 {
1922 char_u *n = skiptowhite(arg);
1923
1924 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
1925 break;
1926 arg = skipwhite(n);
1927 }
1928 }
1929
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 xp->xp_pattern = arg;
1931}
1932
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001934 * Return TRUE if "pat" matches "text".
1935 * Does not use 'cpo' and always uses 'magic'.
1936 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001937 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001938pattern_match(char_u *pat, char_u *text, int ic)
1939{
1940 int matches = FALSE;
1941 char_u *save_cpo;
1942 regmatch_T regmatch;
1943
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001944 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001945 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01001946 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001947 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1948 if (regmatch.regprog != NULL)
1949 {
1950 regmatch.rm_ic = ic;
1951 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1952 vim_regfree(regmatch.regprog);
1953 }
1954 p_cpo = save_cpo;
1955 return matches;
1956}
1957
1958/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001959 * Handle a name followed by "(". Both for just "name(arg)" and for
1960 * "expr->name(arg)".
1961 * Returns OK or FAIL.
1962 */
1963 static int
1964eval_func(
1965 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02001966 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001967 char_u *name,
1968 int name_len,
1969 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001970 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001971 typval_T *basetv) // "expr" for "expr->name(arg)"
1972{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001973 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001974 char_u *s = name;
1975 int len = name_len;
1976 partial_T *partial;
1977 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001978 type_T *type = NULL;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001979
1980 if (!evaluate)
1981 check_vars(s, len);
1982
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001983 // If "s" is the name of a variable of type VAR_FUNC
1984 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001985 s = deref_func_name(s, &len, &partial,
1986 in_vim9script() ? &type : NULL, !evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001987
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001988 // Need to make a copy, in case evaluating the arguments makes
1989 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001990 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001991 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001992 ret = FAIL;
1993 else
1994 {
1995 funcexe_T funcexe;
1996
1997 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001998 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001999 funcexe.firstline = curwin->w_cursor.lnum;
2000 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002001 funcexe.evaluate = evaluate;
2002 funcexe.partial = partial;
2003 funcexe.basetv = basetv;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002004 funcexe.check_type = type;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002005 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002006 }
2007 vim_free(s);
2008
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002009 // If evaluate is FALSE rettv->v_type was not set in
2010 // get_func_tv, but it's needed in handle_subscript() to parse
2011 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002012 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2013 {
2014 rettv->vval.v_string = NULL;
2015 rettv->v_type = VAR_FUNC;
2016 }
2017
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002018 // Stop the expression evaluation when immediately
2019 // aborting on error, or when an interrupt occurred or
2020 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002021 if (evaluate && aborting())
2022 {
2023 if (ret == OK)
2024 clear_tv(rettv);
2025 ret = FAIL;
2026 }
2027 return ret;
2028}
2029
2030/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002031 * Get the next line source line without advancing. But do skip over comment
2032 * lines.
2033 */
2034 static char_u *
2035getline_peek_skip_comments(evalarg_T *evalarg)
2036{
2037 for (;;)
2038 {
2039 char_u *next = getline_peek(evalarg->eval_getline,
2040 evalarg->eval_cookie);
2041 char_u *p;
2042
2043 if (next == NULL)
2044 break;
2045 p = skipwhite(next);
2046 if (*p != NUL && !vim9_comment_start(p))
2047 return next;
2048 (void)eval_next_line(evalarg);
2049 }
2050 return NULL;
2051}
2052
2053/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002054 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2055 * comment) and there is a next line, return the next line (skipping blanks)
2056 * and set "getnext".
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002057 * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
2058 * "arg" must point somewhere inside a line, not at the start.
2059 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002060 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002061eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2062{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002063 char_u *p = skipwhite(arg);
2064
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002065 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002066 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002067 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002068 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaar9d489562020-07-30 20:08:50 +02002069 && (*p == NUL || (VIM_ISWHITE(p[-1]) && vim9_comment_start(p))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002070 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002071 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002072
2073 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002074 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002075 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002076 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002077
Bram Moolenaar9d489562020-07-30 20:08:50 +02002078 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002079 {
2080 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002081 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002082 }
2083 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002084 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002085}
2086
2087/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002088 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002089 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002090 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002091eval_next_line(evalarg_T *evalarg)
2092{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002093 garray_T *gap = &evalarg->eval_ga;
2094 char_u *line;
2095
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002096 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002097 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2098 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002099 else
2100 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002101 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002102 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2103 {
2104 // Going to concatenate the lines after parsing.
2105 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2106 ++gap->ga_len;
2107 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002108 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002109 {
2110 vim_free(evalarg->eval_tofree);
2111 evalarg->eval_tofree = line;
2112 }
2113 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002114}
2115
Bram Moolenaare6b53242020-07-01 17:28:33 +02002116/*
2117 * Call eval_next_non_blank() and get the next line if needed.
2118 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002119 char_u *
2120skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2121{
2122 int getnext;
2123 char_u *p = skipwhite(arg);
2124
Bram Moolenaar962d7212020-07-04 14:15:00 +02002125 if (evalarg == NULL)
2126 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002127 eval_next_non_blank(p, evalarg, &getnext);
2128 if (getnext)
2129 return eval_next_line(evalarg);
2130 return p;
2131}
2132
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002133/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002134 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002135 */
2136 void
2137clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2138{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002139 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002140 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002141 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002142 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002143 if (eap != NULL)
2144 {
2145 // We may need to keep the original command line, e.g. for
2146 // ":let" it has the variable names. But we may also need the
2147 // new one, "nextcmd" points into it. Keep both.
2148 vim_free(eap->cmdline_tofree);
2149 eap->cmdline_tofree = *eap->cmdlinep;
2150 *eap->cmdlinep = evalarg->eval_tofree;
2151 }
2152 else
2153 vim_free(evalarg->eval_tofree);
2154 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002155 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002156
2157 vim_free(evalarg->eval_tofree_lambda);
2158 evalarg->eval_tofree_lambda = NULL;
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002159 }
2160}
2161
2162/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002164 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2166 */
2167
2168/*
2169 * Handle zero level expression.
2170 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002171 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002172 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002173 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 * Return OK or FAIL.
2175 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002176 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002177eval0(
2178 char_u *arg,
2179 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002180 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002181 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182{
2183 int ret;
2184 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002185 int did_emsg_before = did_emsg;
2186 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002187 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188
2189 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002190 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002191 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002192
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002193 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 {
2195 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002196 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 /*
2198 * Report the invalid expression unless the expression evaluation has
2199 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002200 * exception, or we already gave a more specific error.
2201 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002203 if (!aborting()
2204 && did_emsg == did_emsg_before
2205 && called_emsg == called_emsg_before
2206 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002207 semsg(_(e_invexpr2), arg);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002208
2209 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002210 // a next command to avoid more errors, unless "|" is following, which
2211 // could only be a command separator.
2212 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2213 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002214 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002216
2217 if (eap != NULL)
2218 eap->nextcmd = check_nextcmd(p);
2219
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 return ret;
2221}
2222
2223/*
2224 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002225 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002226 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 *
2228 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002229 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002231 * Note: "rettv.v_lock" is not set.
2232 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 * Return OK or FAIL.
2234 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002235 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002236eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002238 char_u *p;
2239 int getnext;
2240
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002241 CLEAR_POINTER(rettv);
2242
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 /*
2244 * Get the first variable.
2245 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002246 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 return FAIL;
2248
Bram Moolenaar793648f2020-06-26 21:28:25 +02002249 p = eval_next_non_blank(*arg, evalarg, &getnext);
2250 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002252 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002253 int result;
2254 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002255 evalarg_T *evalarg_used = evalarg;
2256 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002257 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002258 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002259 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002260
2261 if (evalarg == NULL)
2262 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002263 CLEAR_FIELD(local_evalarg);
2264 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002265 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002266 orig_flags = evalarg_used->eval_flags;
2267 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002268
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002269 if (getnext)
2270 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002271 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002272 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002273 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002274 {
2275 error_white_both(p, 1);
2276 clear_tv(rettv);
2277 return FAIL;
2278 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002279 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002280 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002281
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002283 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002285 int error = FALSE;
2286
Bram Moolenaar13106602020-10-04 16:06:05 +02002287 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002288 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002289 else if (vim9script)
2290 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002291 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002293 if (error || !op_falsy || !result)
2294 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002295 if (error)
2296 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 }
2298
2299 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002300 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002302 if (op_falsy)
2303 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002304 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002305 {
2306 error_white_both(p, 1);
2307 clear_tv(rettv);
2308 return FAIL;
2309 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002310 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002311 evalarg_used->eval_flags = (op_falsy ? !result : result)
2312 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2313 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002314 {
2315 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002317 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002318 if (!op_falsy || !result)
2319 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002321 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002323 /*
2324 * Check for the ":".
2325 */
2326 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2327 if (*p != ':')
2328 {
2329 emsg(_(e_missing_colon));
2330 if (evaluate && result)
2331 clear_tv(rettv);
2332 evalarg_used->eval_flags = orig_flags;
2333 return FAIL;
2334 }
2335 if (getnext)
2336 *arg = eval_next_line(evalarg_used);
2337 else
2338 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002339 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002340 {
2341 error_white_both(p, 1);
2342 clear_tv(rettv);
2343 evalarg_used->eval_flags = orig_flags;
2344 return FAIL;
2345 }
2346 *arg = p;
2347 }
2348
2349 /*
2350 * Get the third variable. Recursive!
2351 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002352 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002353 {
2354 error_white_both(p, 1);
2355 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002356 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002357 return FAIL;
2358 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002359 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2360 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002361 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002362 if (eval1(arg, &var2, evalarg_used) == FAIL)
2363 {
2364 if (evaluate && result)
2365 clear_tv(rettv);
2366 evalarg_used->eval_flags = orig_flags;
2367 return FAIL;
2368 }
2369 if (evaluate && !result)
2370 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002372
2373 if (evalarg == NULL)
2374 clear_evalarg(&local_evalarg, NULL);
2375 else
2376 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 }
2378
2379 return OK;
2380}
2381
2382/*
2383 * Handle first level expression:
2384 * expr2 || expr2 || expr2 logical OR
2385 *
2386 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002387 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 *
2389 * Return OK or FAIL.
2390 */
2391 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002392eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002394 char_u *p;
2395 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396
2397 /*
2398 * Get the first variable.
2399 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002400 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 return FAIL;
2402
2403 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002404 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002406 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002407 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002409 evalarg_T *evalarg_used = evalarg;
2410 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002411 int evaluate;
2412 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002413 long result = FALSE;
2414 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002415 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002416 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002417
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002418 if (evalarg == NULL)
2419 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002420 CLEAR_FIELD(local_evalarg);
2421 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002422 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002423 orig_flags = evalarg_used->eval_flags;
2424 evaluate = orig_flags & EVAL_EVALUATE;
2425 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002426 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002427 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002428 result = tv_get_bool_chk(rettv, &error);
2429 else if (tv_get_number_chk(rettv, &error) != 0)
2430 result = TRUE;
2431 clear_tv(rettv);
2432 if (error)
2433 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 }
2435
2436 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002437 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002439 while (p[0] == '|' && p[1] == '|')
2440 {
2441 if (getnext)
2442 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002443 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002444 {
2445 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2446 {
2447 error_white_both(p, 2);
2448 clear_tv(rettv);
2449 return FAIL;
2450 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002451 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002452 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002453
2454 /*
2455 * Get the second variable.
2456 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002457 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2458 {
2459 error_white_both(p, 2);
2460 clear_tv(rettv);
2461 return FAIL;
2462 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002463 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2464 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002465 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002466 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002467 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002468
2469 /*
2470 * Compute the result.
2471 */
2472 if (evaluate && !result)
2473 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002474 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002475 result = tv_get_bool_chk(&var2, &error);
2476 else if (tv_get_number_chk(&var2, &error) != 0)
2477 result = TRUE;
2478 clear_tv(&var2);
2479 if (error)
2480 return FAIL;
2481 }
2482 if (evaluate)
2483 {
2484 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002485 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002486 rettv->v_type = VAR_BOOL;
2487 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002488 }
2489 else
2490 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002491 rettv->v_type = VAR_NUMBER;
2492 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002493 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002494 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002495
2496 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002498
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002499 if (evalarg == NULL)
2500 clear_evalarg(&local_evalarg, NULL);
2501 else
2502 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 }
2504
2505 return OK;
2506}
2507
2508/*
2509 * Handle second level expression:
2510 * expr3 && expr3 && expr3 logical AND
2511 *
2512 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002513 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 *
2515 * Return OK or FAIL.
2516 */
2517 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002518eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002520 char_u *p;
2521 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522
2523 /*
2524 * Get the first variable.
2525 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002526 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 return FAIL;
2528
2529 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002530 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002532 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002533 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002535 evalarg_T *evalarg_used = evalarg;
2536 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002537 int orig_flags;
2538 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002539 long result = TRUE;
2540 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002541 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002542 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002543
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002544 if (evalarg == NULL)
2545 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002546 CLEAR_FIELD(local_evalarg);
2547 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002548 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002549 orig_flags = evalarg_used->eval_flags;
2550 evaluate = orig_flags & EVAL_EVALUATE;
2551 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002552 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002553 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002554 result = tv_get_bool_chk(rettv, &error);
2555 else if (tv_get_number_chk(rettv, &error) == 0)
2556 result = FALSE;
2557 clear_tv(rettv);
2558 if (error)
2559 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 }
2561
2562 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002563 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002565 while (p[0] == '&' && p[1] == '&')
2566 {
2567 if (getnext)
2568 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002569 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002570 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002571 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002572 {
2573 error_white_both(p, 2);
2574 clear_tv(rettv);
2575 return FAIL;
2576 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002577 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002578 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002579
2580 /*
2581 * Get the second variable.
2582 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002583 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2584 {
2585 error_white_both(p, 2);
2586 clear_tv(rettv);
2587 return FAIL;
2588 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002589 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2590 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002591 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002592 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002593 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002594 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002595
2596 /*
2597 * Compute the result.
2598 */
2599 if (evaluate && result)
2600 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002601 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002602 result = tv_get_bool_chk(&var2, &error);
2603 else if (tv_get_number_chk(&var2, &error) == 0)
2604 result = FALSE;
2605 clear_tv(&var2);
2606 if (error)
2607 return FAIL;
2608 }
2609 if (evaluate)
2610 {
2611 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002612 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002613 rettv->v_type = VAR_BOOL;
2614 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002615 }
2616 else
2617 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002618 rettv->v_type = VAR_NUMBER;
2619 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002620 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002621 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002622
2623 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002625
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002626 if (evalarg == NULL)
2627 clear_evalarg(&local_evalarg, NULL);
2628 else
2629 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 }
2631
2632 return OK;
2633}
2634
2635/*
2636 * Handle third level expression:
2637 * var1 == var2
2638 * var1 =~ var2
2639 * var1 != var2
2640 * var1 !~ var2
2641 * var1 > var2
2642 * var1 >= var2
2643 * var1 < var2
2644 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002645 * var1 is var2
2646 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 *
2648 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002649 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 *
2651 * Return OK or FAIL.
2652 */
2653 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002654eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002657 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002658 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002660 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661
2662 /*
2663 * Get the first variable.
2664 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002665 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 return FAIL;
2667
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002668 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002669 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670
2671 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002672 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002674 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002676 typval_T var2;
2677 int ic;
2678 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002679 int evaluate = evalarg == NULL
2680 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002681
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002682 if (getnext)
2683 *arg = eval_next_line(evalarg);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002684 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2685 {
2686 error_white_both(p, len);
2687 clear_tv(rettv);
2688 return FAIL;
2689 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002690
Bram Moolenaar696ba232020-07-29 21:20:41 +02002691 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2692 {
2693 semsg(_(e_invexpr2), p);
2694 clear_tv(rettv);
2695 return FAIL;
2696 }
2697
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002698 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 if (p[len] == '?')
2700 {
2701 ic = TRUE;
2702 ++len;
2703 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002704 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 else if (p[len] == '#')
2706 {
2707 ic = FALSE;
2708 ++len;
2709 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002710 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002712 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713
2714 /*
2715 * Get the second variable.
2716 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002717 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2718 {
2719 error_white_both(p, 1);
2720 clear_tv(rettv);
2721 return FAIL;
2722 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002723 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002724 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002726 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 return FAIL;
2728 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002729 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002730 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002731 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002732
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002733 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002734 {
2735 ret = FAIL;
2736 clear_tv(rettv);
2737 }
2738 else
2739 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002740 clear_tv(&var2);
2741 return ret;
2742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 }
2744
2745 return OK;
2746}
2747
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002748/*
2749 * Make a copy of blob "tv1" and append blob "tv2".
2750 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002751 void
2752eval_addblob(typval_T *tv1, typval_T *tv2)
2753{
2754 blob_T *b1 = tv1->vval.v_blob;
2755 blob_T *b2 = tv2->vval.v_blob;
2756 blob_T *b = blob_alloc();
2757 int i;
2758
2759 if (b != NULL)
2760 {
2761 for (i = 0; i < blob_len(b1); i++)
2762 ga_append(&b->bv_ga, blob_get(b1, i));
2763 for (i = 0; i < blob_len(b2); i++)
2764 ga_append(&b->bv_ga, blob_get(b2, i));
2765
2766 clear_tv(tv1);
2767 rettv_blob_set(tv1, b);
2768 }
2769}
2770
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002771/*
2772 * Make a copy of list "tv1" and append list "tv2".
2773 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002774 int
2775eval_addlist(typval_T *tv1, typval_T *tv2)
2776{
2777 typval_T var3;
2778
2779 // concatenate Lists
2780 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2781 {
2782 clear_tv(tv1);
2783 clear_tv(tv2);
2784 return FAIL;
2785 }
2786 clear_tv(tv1);
2787 *tv1 = var3;
2788 return OK;
2789}
2790
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791/*
2792 * Handle fourth level expression:
2793 * + number addition
2794 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002795 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002796 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 *
2798 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002799 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 *
2801 * Return OK or FAIL.
2802 */
2803 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002804eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 /*
2807 * Get the first variable.
2808 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002809 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 return FAIL;
2811
2812 /*
2813 * Repeat computing, until no '+', '-' or '.' is following.
2814 */
2815 for (;;)
2816 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002817 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002818 int getnext;
2819 char_u *p;
2820 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002821 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002822 int concat;
2823 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002824 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002825
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002826 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarce2c5442020-11-28 21:21:17 +01002827 // "+=" and "-=" are assignment
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002828 p = eval_next_non_blank(*arg, evalarg, &getnext);
2829 op = *p;
2830 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaarce2c5442020-11-28 21:21:17 +01002831 if ((op != '+' && op != '-' && !concat) || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002833
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002834 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002835 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002836 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002837 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002838 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002839 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002840 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002841 {
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002842 error_white_both(p, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002843 clear_tv(rettv);
2844 return FAIL;
2845 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002846 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002847 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002848 if ((op != '+' || (rettv->v_type != VAR_LIST
2849 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002850#ifdef FEAT_FLOAT
2851 && (op == '.' || rettv->v_type != VAR_FLOAT)
2852#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002853 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002854 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002855 int error = FALSE;
2856
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002857 // For "list + ...", an illegal use of the first operand as
2858 // a number cannot be determined before evaluating the 2nd
2859 // operand: if this is also a list, all is ok.
2860 // For "something . ...", "something - ..." or "non-list + ...",
2861 // we know that the first operand needs to be a string or number
2862 // without evaluating the 2nd operand. So check before to avoid
2863 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002864 if (op != '.')
2865 tv_get_number_chk(rettv, &error);
2866 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002867 {
2868 clear_tv(rettv);
2869 return FAIL;
2870 }
2871 }
2872
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 /*
2874 * Get the second variable.
2875 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002876 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002877 {
2878 error_white_both(p, oplen);
2879 clear_tv(rettv);
2880 return FAIL;
2881 }
2882 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002883 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002885 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 return FAIL;
2887 }
2888
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002889 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 {
2891 /*
2892 * Compute the result.
2893 */
2894 if (op == '.')
2895 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002896 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2897 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002898 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002899
Bram Moolenaar2e086612020-08-25 22:37:48 +02002900 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002901 || var2.v_type == VAR_CHANNEL
2902 || var2.v_type == VAR_JOB))
2903 emsg(_(e_inval_string));
2904#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02002905 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002906 {
2907 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2908 var2.vval.v_float);
2909 s2 = buf2;
2910 }
2911#endif
2912 else
2913 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002914 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002915 {
2916 clear_tv(rettv);
2917 clear_tv(&var2);
2918 return FAIL;
2919 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002920 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002921 clear_tv(rettv);
2922 rettv->v_type = VAR_STRING;
2923 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002925 else if (op == '+' && rettv->v_type == VAR_BLOB
2926 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002927 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002928 else if (op == '+' && rettv->v_type == VAR_LIST
2929 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002930 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002931 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002932 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 else
2935 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002936 int error = FALSE;
2937 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002938#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002939 float_T f1 = 0, f2 = 0;
2940
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002941 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002942 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002943 f1 = rettv->vval.v_float;
2944 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002945 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002946 else
2947#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002948 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002949 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002950 if (error)
2951 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002952 // This can only happen for "list + non-list". For
2953 // "non-list + ..." or "something - ...", we returned
2954 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002955 clear_tv(rettv);
2956 return FAIL;
2957 }
2958#ifdef FEAT_FLOAT
2959 if (var2.v_type == VAR_FLOAT)
2960 f1 = n1;
2961#endif
2962 }
2963#ifdef FEAT_FLOAT
2964 if (var2.v_type == VAR_FLOAT)
2965 {
2966 f2 = var2.vval.v_float;
2967 n2 = 0;
2968 }
2969 else
2970#endif
2971 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002972 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002973 if (error)
2974 {
2975 clear_tv(rettv);
2976 clear_tv(&var2);
2977 return FAIL;
2978 }
2979#ifdef FEAT_FLOAT
2980 if (rettv->v_type == VAR_FLOAT)
2981 f2 = n2;
2982#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002983 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002984 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002985
2986#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002987 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002988 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2989 {
2990 if (op == '+')
2991 f1 = f1 + f2;
2992 else
2993 f1 = f1 - f2;
2994 rettv->v_type = VAR_FLOAT;
2995 rettv->vval.v_float = f1;
2996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002998#endif
2999 {
3000 if (op == '+')
3001 n1 = n1 + n2;
3002 else
3003 n1 = n1 - n2;
3004 rettv->v_type = VAR_NUMBER;
3005 rettv->vval.v_number = n1;
3006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003008 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 }
3010 }
3011 return OK;
3012}
3013
3014/*
3015 * Handle fifth level expression:
3016 * * number multiplication
3017 * / number division
3018 * % number modulo
3019 *
3020 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003021 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 *
3023 * Return OK or FAIL.
3024 */
3025 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003026eval6(
3027 char_u **arg,
3028 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003029 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003030 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003032#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003033 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003034#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035
3036 /*
3037 * Get the first variable.
3038 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003039 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 return FAIL;
3041
3042 /*
3043 * Repeat computing, until no '*', '/' or '%' is following.
3044 */
3045 for (;;)
3046 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003047 int evaluate;
3048 int getnext;
3049 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003050 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003051 int op;
3052 varnumber_T n1, n2;
3053#ifdef FEAT_FLOAT
3054 float_T f1, f2;
3055#endif
3056 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003057
Bram Moolenaar9d489562020-07-30 20:08:50 +02003058 p = eval_next_non_blank(*arg, evalarg, &getnext);
3059 op = *p;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02003060 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003062
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003063 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003064 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003065 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003066 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003067 {
3068 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3069 {
3070 error_white_both(p, 1);
3071 clear_tv(rettv);
3072 return FAIL;
3073 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003074 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003077#ifdef FEAT_FLOAT
3078 f1 = 0;
3079 f2 = 0;
3080#endif
3081 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003082 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003084#ifdef FEAT_FLOAT
3085 if (rettv->v_type == VAR_FLOAT)
3086 {
3087 f1 = rettv->vval.v_float;
3088 use_float = TRUE;
3089 n1 = 0;
3090 }
3091 else
3092#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003093 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003094 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003095 if (error)
3096 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 }
3098 else
3099 n1 = 0;
3100
3101 /*
3102 * Get the second variable.
3103 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003104 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3105 {
3106 error_white_both(p, 1);
3107 clear_tv(rettv);
3108 return FAIL;
3109 }
3110 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003111 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 return FAIL;
3113
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003114 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003116#ifdef FEAT_FLOAT
3117 if (var2.v_type == VAR_FLOAT)
3118 {
3119 if (!use_float)
3120 {
3121 f1 = n1;
3122 use_float = TRUE;
3123 }
3124 f2 = var2.vval.v_float;
3125 n2 = 0;
3126 }
3127 else
3128#endif
3129 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003130 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003131 clear_tv(&var2);
3132 if (error)
3133 return FAIL;
3134#ifdef FEAT_FLOAT
3135 if (use_float)
3136 f2 = n2;
3137#endif
3138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139
3140 /*
3141 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003142 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003144#ifdef FEAT_FLOAT
3145 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003147 if (op == '*')
3148 f1 = f1 * f2;
3149 else if (op == '/')
3150 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003151# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003152 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003153 if (f2 == 0.0)
3154 {
3155 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003156 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003157 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003158 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003159 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003160 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003161 }
3162 else
3163 f1 = f1 / f2;
3164# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003165 // We rely on the floating point library to handle divide
3166 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003167 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003168# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003171 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003172 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003173 return FAIL;
3174 }
3175 rettv->v_type = VAR_FLOAT;
3176 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 }
3178 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003181 if (op == '*')
3182 n1 = n1 * n2;
3183 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01003184 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01003186 n1 = num_modulus(n1, n2);
3187
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003188 rettv->v_type = VAR_NUMBER;
3189 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 }
3192 }
3193
3194 return OK;
3195}
3196
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003197 int
3198eval_leader(char_u **arg, int vim9)
3199{
3200 char_u *s = *arg;
3201 char_u *p = *arg;
3202
3203 while (*p == '!' || *p == '-' || *p == '+')
3204 {
3205 char_u *n = skipwhite(p + 1);
3206
3207 // ++, --, -+ and +- are not accepted in Vim9 script
3208 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3209 {
3210 semsg(_(e_invexpr2), s);
3211 return FAIL;
3212 }
3213 p = n;
3214 }
3215 *arg = p;
3216 return OK;
3217}
3218
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219/*
3220 * Handle sixth level expression:
3221 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003222 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003223 * "string" string constant
3224 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 * &option-name option value
3226 * @r register contents
3227 * identifier variable value
3228 * function() function call
3229 * $VAR environment variable
3230 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003231 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003232 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003233 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003234 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 *
3236 * Also handle:
3237 * ! in front logical NOT
3238 * - in front unary minus
3239 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003240 * trailing [] subscript in String or List
3241 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003242 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 *
3244 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003245 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 *
3247 * Return OK or FAIL.
3248 */
3249 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003250eval7(
3251 char_u **arg,
3252 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003253 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003254 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003256 int evaluate = evalarg != NULL
3257 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 int len;
3259 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 char_u *start_leader, *end_leader;
3261 int ret = OK;
3262 char_u *alias;
3263
3264 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003265 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003266 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003268 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269
3270 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003271 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 */
3273 start_leader = *arg;
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003274 if (eval_leader(arg, in_vim9script()) == FAIL)
3275 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 end_leader = *arg;
3277
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003278 if (**arg == '.' && (!isdigit(*(*arg + 1))
3279#ifdef FEAT_FLOAT
3280 || current_sctx.sc_version < 2
3281#endif
3282 ))
3283 {
3284 semsg(_(e_invexpr2), *arg);
3285 ++*arg;
3286 return FAIL;
3287 }
3288
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 switch (**arg)
3290 {
3291 /*
3292 * Number constant.
3293 */
3294 case '0':
3295 case '1':
3296 case '2':
3297 case '3':
3298 case '4':
3299 case '5':
3300 case '6':
3301 case '7':
3302 case '8':
3303 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003304 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003305
3306 // Apply prefixed "-" and "+" now. Matters especially when
3307 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003308 if (ret == OK && evaluate && end_leader > start_leader
3309 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003310 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 break;
3312
3313 /*
3314 * String constant: "string".
3315 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003316 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 break;
3318
3319 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003320 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003322 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003323 break;
3324
3325 /*
3326 * List: [expr, expr]
3327 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003328 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 break;
3330
3331 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003332 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003333 */
Bram Moolenaare0de1712020-12-02 17:36:54 +01003334 case '#': if (!in_vim9script() && (*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003335 {
3336 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003337 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003338 }
3339 else
3340 ret = NOTDONE;
3341 break;
3342
3343 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003344 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003345 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003346 */
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003347 case '{': if (in_vim9script())
3348 ret = NOTDONE;
3349 else
3350 ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003351 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003352 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003353 break;
3354
3355 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003356 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003358 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 break;
3360
3361 /*
3362 * Environment variable: $VAR.
3363 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003364 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 break;
3366
3367 /*
3368 * Register contents: @r.
3369 */
3370 case '@': ++*arg;
3371 if (evaluate)
3372 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003373 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02003374 rettv->vval.v_string = get_reg_contents(**arg,
3375 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 }
3377 if (**arg != NUL)
3378 ++*arg;
3379 break;
3380
3381 /*
3382 * nested expression: (expression).
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003383 * lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003385 case '(': ret = NOTDONE;
3386 if (in_vim9script())
3387 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
3388 if (ret == NOTDONE)
3389 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003390 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003391 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003392
Bram Moolenaar9215f012020-06-27 21:18:00 +02003393 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003394 if (**arg == ')')
3395 ++*arg;
3396 else if (ret == OK)
3397 {
3398 emsg(_(e_missing_close));
3399 clear_tv(rettv);
3400 ret = FAIL;
3401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 }
3403 break;
3404
Bram Moolenaar8c711452005-01-14 21:53:12 +00003405 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 break;
3407 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003408
3409 if (ret == NOTDONE)
3410 {
3411 /*
3412 * Must be a variable or function name.
3413 * Can also be a curly-braces kind of name: {expr}.
3414 */
3415 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003416 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003417 if (alias != NULL)
3418 s = alias;
3419
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003420 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003421 ret = FAIL;
3422 else
3423 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003424 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3425
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003426 if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
3427 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003428 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003429 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003430 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003431 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003432 else if (flags & EVAL_CONSTANT)
3433 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003434 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003435 {
3436 // get the value of "true", "false" or a variable
3437 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3438 {
3439 rettv->v_type = VAR_BOOL;
3440 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003441 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003442 }
3443 else if (len == 5 && in_vim9script()
Bram Moolenaar5f639382021-01-03 22:05:19 +01003444 && STRNCMP(s, "false", 5) == 0)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003445 {
3446 rettv->v_type = VAR_BOOL;
3447 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003448 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003449 }
Bram Moolenaar5f639382021-01-03 22:05:19 +01003450 else if (len == 4 && in_vim9script()
3451 && STRNCMP(s, "null", 4) == 0)
3452 {
3453 rettv->v_type = VAR_SPECIAL;
3454 rettv->vval.v_number = VVAL_NULL;
3455 ret = OK;
3456 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003457 else
3458 ret = eval_variable(s, len, rettv, NULL, TRUE, FALSE);
3459 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003460 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003461 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003462 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003463 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003464 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003465 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003466 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003467 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003468 }
3469
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003470 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3471 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003472 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003473 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474
3475 /*
3476 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3477 */
3478 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003479 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003480 return ret;
3481}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003482
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003483/*
3484 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003485 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003486 * Adjusts "end_leaderp" until it is at "start_leader".
3487 */
3488 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003489eval7_leader(
3490 typval_T *rettv,
3491 int numeric_only,
3492 char_u *start_leader,
3493 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003494{
3495 char_u *end_leader = *end_leaderp;
3496 int ret = OK;
3497 int error = FALSE;
3498 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003499 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003500#ifdef FEAT_FLOAT
3501 float_T f = 0.0;
3502
3503 if (rettv->v_type == VAR_FLOAT)
3504 f = rettv->vval.v_float;
3505 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003506#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003507 {
3508 while (VIM_ISWHITE(end_leader[-1]))
3509 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003510 if (in_vim9script() && end_leader[-1] == '!')
3511 val = tv2bool(rettv);
3512 else
3513 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003514 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003515 if (error)
3516 {
3517 clear_tv(rettv);
3518 ret = FAIL;
3519 }
3520 else
3521 {
3522 while (end_leader > start_leader)
3523 {
3524 --end_leader;
3525 if (*end_leader == '!')
3526 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003527 if (numeric_only)
3528 {
3529 ++end_leader;
3530 break;
3531 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003532#ifdef FEAT_FLOAT
3533 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003534 {
3535 if (in_vim9script())
3536 {
3537 rettv->v_type = VAR_BOOL;
3538 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3539 }
3540 else
3541 f = !f;
3542 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003543 else
3544#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003545 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003546 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003547 type = VAR_BOOL;
3548 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003549 }
3550 else if (*end_leader == '-')
3551 {
3552#ifdef FEAT_FLOAT
3553 if (rettv->v_type == VAR_FLOAT)
3554 f = -f;
3555 else
3556#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003557 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003558 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003559 type = VAR_NUMBER;
3560 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003561 }
3562 }
3563#ifdef FEAT_FLOAT
3564 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003566 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003567 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003569 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003570#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003571 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003572 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003573 if (in_vim9script())
3574 rettv->v_type = type;
3575 else
3576 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003577 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003580 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 return ret;
3582}
3583
3584/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003585 * Call the function referred to in "rettv".
3586 */
3587 static int
3588call_func_rettv(
3589 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003590 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003591 typval_T *rettv,
3592 int evaluate,
3593 dict_T *selfdict,
3594 typval_T *basetv)
3595{
3596 partial_T *pt = NULL;
3597 funcexe_T funcexe;
3598 typval_T functv;
3599 char_u *s;
3600 int ret;
3601
3602 // need to copy the funcref so that we can clear rettv
3603 if (evaluate)
3604 {
3605 functv = *rettv;
3606 rettv->v_type = VAR_UNKNOWN;
3607
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003608 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003609 if (functv.v_type == VAR_PARTIAL)
3610 {
3611 pt = functv.vval.v_partial;
3612 s = partial_name(pt);
3613 }
3614 else
3615 s = functv.vval.v_string;
3616 }
3617 else
3618 s = (char_u *)"";
3619
Bram Moolenaara80faa82020-04-12 19:37:17 +02003620 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003621 funcexe.firstline = curwin->w_cursor.lnum;
3622 funcexe.lastline = curwin->w_cursor.lnum;
3623 funcexe.evaluate = evaluate;
3624 funcexe.partial = pt;
3625 funcexe.selfdict = selfdict;
3626 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003627 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003628
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003629 // Clear the funcref afterwards, so that deleting it while
3630 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003631 if (evaluate)
3632 clear_tv(&functv);
3633
3634 return ret;
3635}
3636
3637/*
3638 * Evaluate "->method()".
3639 * "*arg" points to the '-'.
3640 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3641 */
3642 static int
3643eval_lambda(
3644 char_u **arg,
3645 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003646 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003647 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003648{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003649 int evaluate = evalarg != NULL
3650 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003651 typval_T base = *rettv;
3652 int ret;
3653
3654 // Skip over the ->.
3655 *arg += 2;
3656 rettv->v_type = VAR_UNKNOWN;
3657
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003658 if (**arg == '{')
3659 {
3660 // ->{lambda}()
3661 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
3662 }
3663 else
3664 {
3665 // ->(lambda)()
3666 ++*arg;
3667 ret = eval1(arg, rettv, evalarg);
3668 *arg = skipwhite_and_linebreak(*arg, evalarg);
3669 if (**arg != ')')
3670 {
3671 emsg(_(e_missing_close));
3672 ret = FAIL;
3673 }
3674 ++*arg;
3675 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003676 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003677 return FAIL;
3678 else if (**arg != '(')
3679 {
3680 if (verbose)
3681 {
3682 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003683 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003684 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003685 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003686 }
3687 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003688 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003689 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003690 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003691 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003692
3693 // Clear the funcref afterwards, so that deleting it while
3694 // evaluating the arguments is possible (see test55).
3695 if (evaluate)
3696 clear_tv(&base);
3697
3698 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003699}
3700
3701/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003702 * Evaluate "->method()".
3703 * "*arg" points to the '-'.
3704 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3705 */
3706 static int
3707eval_method(
3708 char_u **arg,
3709 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003710 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003711 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003712{
3713 char_u *name;
3714 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003715 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003716 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003717 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003718 int evaluate = evalarg != NULL
3719 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003720
3721 // Skip over the ->.
3722 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003723 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003724
Bram Moolenaarac92e252019-08-03 21:58:38 +02003725 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003726 len = get_name_len(arg, &alias, evaluate, TRUE);
3727 if (alias != NULL)
3728 name = alias;
3729
3730 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003731 {
3732 if (verbose)
3733 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003734 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003735 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003736 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003737 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003738 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003739 if (**arg != '(')
3740 {
3741 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003742 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003743 ret = FAIL;
3744 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003745 else if (VIM_ISWHITE((*arg)[-1]))
3746 {
3747 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003748 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003749 ret = FAIL;
3750 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003751 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003752 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003753 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003754 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003755
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003756 // Clear the funcref afterwards, so that deleting it while
3757 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003758 if (evaluate)
3759 clear_tv(&base);
3760
Bram Moolenaarac92e252019-08-03 21:58:38 +02003761 return ret;
3762}
3763
3764/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003765 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3766 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003767 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3768 */
3769 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003770eval_index(
3771 char_u **arg,
3772 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003773 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003774 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003775{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003776 int evaluate = evalarg != NULL
3777 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003778 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003779 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003780 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003781 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003782 int keylen = -1;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003783 int vim9 = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003784
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003785 if (check_can_index(rettv, evaluate, verbose) == FAIL)
3786 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003787
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003788 init_tv(&var1);
3789 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003790 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003791 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003792 /*
3793 * dict.name
3794 */
3795 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003796 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003797 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003798 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003799 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02003800 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003801 }
3802 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003803 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003804 /*
3805 * something[idx]
3806 *
3807 * Get the (first) variable from inside the [].
3808 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003809 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003810 if (**arg == ':')
3811 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003812 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003813 return FAIL;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003814 else if (vim9 && **arg == ':')
3815 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003816 semsg(_(e_white_space_required_before_and_after_str_at_str),
3817 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003818 clear_tv(&var1);
3819 return FAIL;
3820 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003821 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003822 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003823 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003824 clear_tv(&var1);
3825 return FAIL;
3826 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003827
3828 /*
3829 * Get the second variable from inside the [:].
3830 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003831 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003832 if (**arg == ':')
3833 {
3834 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003835 ++*arg;
Bram Moolenaara04d4472020-12-30 21:16:37 +01003836 if (vim9 && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003837 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01003838 semsg(_(e_white_space_required_before_and_after_str_at_str),
3839 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01003840 if (!empty1)
3841 clear_tv(&var1);
3842 return FAIL;
3843 }
3844 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003845 if (**arg == ']')
3846 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003847 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003848 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003849 if (!empty1)
3850 clear_tv(&var1);
3851 return FAIL;
3852 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003853 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003854 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003855 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003856 if (!empty1)
3857 clear_tv(&var1);
3858 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003859 return FAIL;
3860 }
3861 }
3862
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003863 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003864 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003865 if (**arg != ']')
3866 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003867 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003868 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003869 clear_tv(&var1);
3870 if (range)
3871 clear_tv(&var2);
3872 return FAIL;
3873 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02003874 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003875 }
3876
3877 if (evaluate)
3878 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003879 int res = eval_index_inner(rettv, range,
3880 empty1 ? NULL : &var1, empty2 ? NULL : &var2,
3881 key, keylen, verbose);
3882 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003883 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003884 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003885 clear_tv(&var2);
3886 return res;
3887 }
3888 return OK;
3889}
3890
3891/*
3892 * Check if "rettv" can have an [index] or [sli:ce]
3893 */
3894 int
3895check_can_index(typval_T *rettv, int evaluate, int verbose)
3896{
3897 switch (rettv->v_type)
3898 {
3899 case VAR_FUNC:
3900 case VAR_PARTIAL:
3901 if (verbose)
3902 emsg(_("E695: Cannot index a Funcref"));
3903 return FAIL;
3904 case VAR_FLOAT:
3905#ifdef FEAT_FLOAT
3906 if (verbose)
3907 emsg(_(e_float_as_string));
3908 return FAIL;
3909#endif
3910 case VAR_BOOL:
3911 case VAR_SPECIAL:
3912 case VAR_JOB:
3913 case VAR_CHANNEL:
3914 if (verbose)
3915 emsg(_(e_cannot_index_special_variable));
3916 return FAIL;
3917 case VAR_UNKNOWN:
3918 case VAR_ANY:
3919 case VAR_VOID:
3920 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003921 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003922 emsg(_(e_cannot_index_special_variable));
3923 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003924 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003925 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003926
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003927 case VAR_STRING:
3928 case VAR_LIST:
3929 case VAR_DICT:
3930 case VAR_BLOB:
3931 break;
3932 case VAR_NUMBER:
3933 if (in_vim9script())
3934 emsg(_(e_cannot_index_number));
3935 break;
3936 }
3937 return OK;
3938}
3939
3940/*
3941 * Apply index or range to "rettv".
3942 * "var1" is the first index, NULL for [:expr].
3943 * "var2" is the second index, NULL for [expr] and [expr: ]
3944 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
3945 */
3946 int
3947eval_index_inner(
3948 typval_T *rettv,
3949 int is_range,
3950 typval_T *var1,
3951 typval_T *var2,
3952 char_u *key,
3953 int keylen,
3954 int verbose)
3955{
3956 long n1, n2 = 0;
3957 long len;
3958
3959 n1 = 0;
3960 if (var1 != NULL && rettv->v_type != VAR_DICT)
3961 n1 = tv_get_number(var1);
3962
3963 if (is_range)
3964 {
3965 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003966 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003967 if (verbose)
3968 emsg(_(e_cannot_slice_dictionary));
3969 return FAIL;
3970 }
3971 if (var2 == NULL)
3972 n2 = -1;
3973 else
3974 n2 = tv_get_number(var2);
3975 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01003976
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003977 switch (rettv->v_type)
3978 {
3979 case VAR_UNKNOWN:
3980 case VAR_ANY:
3981 case VAR_VOID:
3982 case VAR_FUNC:
3983 case VAR_PARTIAL:
3984 case VAR_FLOAT:
3985 case VAR_BOOL:
3986 case VAR_SPECIAL:
3987 case VAR_JOB:
3988 case VAR_CHANNEL:
3989 break; // not evaluating, skipping over subscript
3990
3991 case VAR_NUMBER:
3992 case VAR_STRING:
3993 {
3994 char_u *s = tv_get_string(rettv);
3995
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003996 len = (long)STRLEN(s);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003997 if (in_vim9script())
3998 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003999 if (is_range)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004000 s = string_slice(s, n1, n2);
4001 else
4002 s = char_from_string(s, n1);
4003 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004004 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004005 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004006 // The resulting variable is a substring. If the indexes
4007 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004008 if (n1 < 0)
4009 {
4010 n1 = len + n1;
4011 if (n1 < 0)
4012 n1 = 0;
4013 }
4014 if (n2 < 0)
4015 n2 = len + n2;
4016 else if (n2 >= len)
4017 n2 = len;
4018 if (n1 >= len || n2 < 0 || n1 > n2)
4019 s = NULL;
4020 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004021 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004022 }
4023 else
4024 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004025 // The resulting variable is a string of a single
4026 // character. If the index is too big or negative the
4027 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004028 if (n1 >= len || n1 < 0)
4029 s = NULL;
4030 else
4031 s = vim_strnsave(s + n1, 1);
4032 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004033 clear_tv(rettv);
4034 rettv->v_type = VAR_STRING;
4035 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004036 }
4037 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004038
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004039 case VAR_BLOB:
4040 len = blob_len(rettv->vval.v_blob);
4041 if (is_range)
4042 {
4043 // The resulting variable is a sub-blob. If the indexes
4044 // are out of range the result is empty.
4045 if (n1 < 0)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004046 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004047 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004048 if (n1 < 0)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004049 n1 = 0;
4050 }
4051 if (n2 < 0)
4052 n2 = len + n2;
4053 else if (n2 >= len)
4054 n2 = len - 1;
4055 if (n1 >= len || n2 < 0 || n1 > n2)
4056 {
4057 clear_tv(rettv);
4058 rettv->v_type = VAR_BLOB;
4059 rettv->vval.v_blob = NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004060 }
4061 else
4062 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004063 blob_T *blob = blob_alloc();
4064 long i;
4065
4066 if (blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004067 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004068 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004069 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004070 blob_free(blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004071 return FAIL;
4072 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004073 blob->bv_ga.ga_len = n2 - n1 + 1;
4074 for (i = n1; i <= n2; i++)
4075 blob_set(blob, i - n1,
4076 blob_get(rettv->vval.v_blob, i));
4077
4078 clear_tv(rettv);
4079 rettv_blob_set(rettv, blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004080 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004081 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004082 }
4083 else
4084 {
4085 // The resulting variable is a byte value.
4086 // If the index is too big or negative that is an error.
4087 if (n1 < 0)
4088 n1 = len + n1;
4089 if (n1 < len && n1 >= 0)
4090 {
4091 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004092
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004093 clear_tv(rettv);
4094 rettv->v_type = VAR_NUMBER;
4095 rettv->vval.v_number = v;
4096 }
4097 else
4098 semsg(_(e_blobidx), n1);
4099 }
4100 break;
4101
4102 case VAR_LIST:
4103 if (var1 == NULL)
4104 n1 = 0;
4105 if (var2 == NULL)
4106 n2 = -1;
4107 if (list_slice_or_index(rettv->vval.v_list,
4108 is_range, n1, n2, rettv, verbose) == FAIL)
4109 return FAIL;
4110 break;
4111
4112 case VAR_DICT:
4113 {
4114 dictitem_T *item;
4115 typval_T tmp;
4116
4117 if (key == NULL)
4118 {
4119 key = tv_get_string_chk(var1);
4120 if (key == NULL)
4121 return FAIL;
4122 }
4123
4124 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
4125
4126 if (item == NULL && verbose)
4127 semsg(_(e_dictkey), key);
4128 if (item == NULL)
4129 return FAIL;
4130
4131 copy_tv(&item->di_tv, &tmp);
4132 clear_tv(rettv);
4133 *rettv = tmp;
4134 }
4135 break;
4136 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004137 return OK;
4138}
4139
4140/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004141 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004142 */
4143 char_u *
4144partial_name(partial_T *pt)
4145{
4146 if (pt->pt_name != NULL)
4147 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004148 if (pt->pt_func != NULL)
4149 return pt->pt_func->uf_name;
4150 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004151}
4152
Bram Moolenaarddecc252016-04-06 22:59:37 +02004153 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004154partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004155{
4156 int i;
4157
4158 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004159 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004160 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004161 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004162 if (pt->pt_name != NULL)
4163 {
4164 func_unref(pt->pt_name);
4165 vim_free(pt->pt_name);
4166 }
4167 else
4168 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004169
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004170 // Decrease the reference count for the context of a closure. If down
4171 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004172 if (pt->pt_funcstack != NULL)
4173 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004174 --pt->pt_funcstack->fs_refcount;
4175 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004176 }
4177
Bram Moolenaarddecc252016-04-06 22:59:37 +02004178 vim_free(pt);
4179}
4180
4181/*
4182 * Unreference a closure: decrement the reference count and free it when it
4183 * becomes zero.
4184 */
4185 void
4186partial_unref(partial_T *pt)
4187{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004188 if (pt != NULL)
4189 {
4190 if (--pt->pt_refcount <= 0)
4191 partial_free(pt);
4192
4193 // If the reference count goes down to one, the funcstack may be the
4194 // only reference and can be freed if no other partials reference it.
4195 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4196 funcstack_check_refcount(pt->pt_funcstack);
4197 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004198}
4199
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004200/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004201 * Return the next (unique) copy ID.
4202 * Used for serializing nested structures.
4203 */
4204 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004205get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004206{
4207 current_copyID += COPYID_INC;
4208 return current_copyID;
4209}
4210
4211/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004212 * Garbage collection for lists and dictionaries.
4213 *
4214 * We use reference counts to be able to free most items right away when they
4215 * are no longer used. But for composite items it's possible that it becomes
4216 * unused while the reference count is > 0: When there is a recursive
4217 * reference. Example:
4218 * :let l = [1, 2, 3]
4219 * :let d = {9: l}
4220 * :let l[1] = d
4221 *
4222 * Since this is quite unusual we handle this with garbage collection: every
4223 * once in a while find out which lists and dicts are not referenced from any
4224 * variable.
4225 *
4226 * Here is a good reference text about garbage collection (refers to Python
4227 * but it applies to all reference-counting mechanisms):
4228 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004229 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004230
4231/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004232 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004233 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004234 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004235 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004236 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004237garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004238{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004239 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004240 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004241 buf_T *buf;
4242 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004243 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004244 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004245
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004246 if (!testing)
4247 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004248 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004249 want_garbage_collect = FALSE;
4250 may_garbage_collect = FALSE;
4251 garbage_collect_at_exit = FALSE;
4252 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004253
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004254 // The execution stack can grow big, limit the size.
4255 if (exestack.ga_maxlen - exestack.ga_len > 500)
4256 {
4257 size_t new_len;
4258 char_u *pp;
4259 int n;
4260
4261 // Keep 150% of the current size, with a minimum of the growth size.
4262 n = exestack.ga_len / 2;
4263 if (n < exestack.ga_growsize)
4264 n = exestack.ga_growsize;
4265
4266 // Don't make it bigger though.
4267 if (exestack.ga_len + n < exestack.ga_maxlen)
4268 {
4269 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4270 pp = vim_realloc(exestack.ga_data, new_len);
4271 if (pp == NULL)
4272 return FAIL;
4273 exestack.ga_maxlen = exestack.ga_len + n;
4274 exestack.ga_data = pp;
4275 }
4276 }
4277
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004278 // We advance by two because we add one for items referenced through
4279 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004280 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004281
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004282 /*
4283 * 1. Go through all accessible variables and mark all lists and dicts
4284 * with copyID.
4285 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004286
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004287 // Don't free variables in the previous_funccal list unless they are only
4288 // referenced through previous_funccal. This must be first, because if
4289 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004290 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004291
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004292 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004293 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004294
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004295 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004296 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004297 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4298 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004299
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004300 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004301 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004302 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4303 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004304 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004305 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4306 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004307#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004308 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004309 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4310 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004311 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004312 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004313 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4314 NULL, NULL);
4315#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004316
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004317 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004318 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004319 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4320 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004321 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004322 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004323
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004324 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004325 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004326
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004327 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004328 abort = abort || set_ref_in_functions(copyID);
4329
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004330 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004331 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004332
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004333 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004334 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004335
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004336 // callbacks in buffers
4337 abort = abort || set_ref_in_buffers(copyID);
4338
Bram Moolenaar1dced572012-04-05 16:54:08 +02004339#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004340 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004341#endif
4342
Bram Moolenaardb913952012-06-29 12:54:53 +02004343#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004344 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004345#endif
4346
4347#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004348 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004349#endif
4350
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004351#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004352 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004353 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004354#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004355#ifdef FEAT_NETBEANS_INTG
4356 abort = abort || set_ref_in_nb_channel(copyID);
4357#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004358
Bram Moolenaare3188e22016-05-31 21:13:04 +02004359#ifdef FEAT_TIMERS
4360 abort = abort || set_ref_in_timer(copyID);
4361#endif
4362
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004363#ifdef FEAT_QUICKFIX
4364 abort = abort || set_ref_in_quickfix(copyID);
4365#endif
4366
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004367#ifdef FEAT_TERMINAL
4368 abort = abort || set_ref_in_term(copyID);
4369#endif
4370
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004371#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004372 abort = abort || set_ref_in_popups(copyID);
4373#endif
4374
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004375 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004376 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004377 /*
4378 * 2. Free lists and dictionaries that are not referenced.
4379 */
4380 did_free = free_unref_items(copyID);
4381
4382 /*
4383 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004384 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004385 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004386 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004387 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004388 else if (p_verbose > 0)
4389 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004390 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004391 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004392
4393 return did_free;
4394}
4395
4396/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004397 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004398 */
4399 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004400free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004401{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004402 int did_free = FALSE;
4403
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004404 // Let all "free" functions know that we are here. This means no
4405 // dictionaries, lists, channels or jobs are to be freed, because we will
4406 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004407 in_free_unref_items = TRUE;
4408
4409 /*
4410 * PASS 1: free the contents of the items. We don't free the items
4411 * themselves yet, so that it is possible to decrement refcount counters
4412 */
4413
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004414 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004415 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004416
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004417 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004418 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004419
4420#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004421 // Go through the list of jobs and free items without the copyID. This
4422 // must happen before doing channels, because jobs refer to channels, but
4423 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004424 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4425
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004426 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004427 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4428#endif
4429
4430 /*
4431 * PASS 2: free the items themselves.
4432 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004433 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004434 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004435
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004436#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004437 // Go through the list of jobs and free items without the copyID. This
4438 // must happen before doing channels, because jobs refer to channels, but
4439 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004440 free_unused_jobs(copyID, COPYID_MASK);
4441
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004442 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004443 free_unused_channels(copyID, COPYID_MASK);
4444#endif
4445
4446 in_free_unref_items = FALSE;
4447
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004448 return did_free;
4449}
4450
4451/*
4452 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004453 * "list_stack" is used to add lists to be marked. Can be NULL.
4454 *
4455 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004456 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004457 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004458set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004459{
4460 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004461 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004462 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004463 hashtab_T *cur_ht;
4464 ht_stack_T *ht_stack = NULL;
4465 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004466
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004467 cur_ht = ht;
4468 for (;;)
4469 {
4470 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004471 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004472 // Mark each item in the hashtab. If the item contains a hashtab
4473 // it is added to ht_stack, if it contains a list it is added to
4474 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004475 todo = (int)cur_ht->ht_used;
4476 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4477 if (!HASHITEM_EMPTY(hi))
4478 {
4479 --todo;
4480 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4481 &ht_stack, list_stack);
4482 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004483 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004484
4485 if (ht_stack == NULL)
4486 break;
4487
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004488 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004489 cur_ht = ht_stack->ht;
4490 tempitem = ht_stack;
4491 ht_stack = ht_stack->prev;
4492 free(tempitem);
4493 }
4494
4495 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004496}
4497
4498/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004499 * Mark a dict and its items with "copyID".
4500 * Returns TRUE if setting references failed somehow.
4501 */
4502 int
4503set_ref_in_dict(dict_T *d, int copyID)
4504{
4505 if (d != NULL && d->dv_copyID != copyID)
4506 {
4507 d->dv_copyID = copyID;
4508 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4509 }
4510 return FALSE;
4511}
4512
4513/*
4514 * Mark a list and its items with "copyID".
4515 * Returns TRUE if setting references failed somehow.
4516 */
4517 int
4518set_ref_in_list(list_T *ll, int copyID)
4519{
4520 if (ll != NULL && ll->lv_copyID != copyID)
4521 {
4522 ll->lv_copyID = copyID;
4523 return set_ref_in_list_items(ll, copyID, NULL);
4524 }
4525 return FALSE;
4526}
4527
4528/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004529 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004530 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4531 *
4532 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004533 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004534 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004535set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004536{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004537 listitem_T *li;
4538 int abort = FALSE;
4539 list_T *cur_l;
4540 list_stack_T *list_stack = NULL;
4541 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004542
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004543 cur_l = l;
4544 for (;;)
4545 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004546 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004547 // Mark each item in the list. If the item contains a hashtab
4548 // it is added to ht_stack, if it contains a list it is added to
4549 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004550 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4551 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4552 ht_stack, &list_stack);
4553 if (list_stack == NULL)
4554 break;
4555
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004556 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004557 cur_l = list_stack->list;
4558 tempitem = list_stack;
4559 list_stack = list_stack->prev;
4560 free(tempitem);
4561 }
4562
4563 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004564}
4565
4566/*
4567 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004568 * "list_stack" is used to add lists to be marked. Can be NULL.
4569 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4570 *
4571 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004572 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004573 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004574set_ref_in_item(
4575 typval_T *tv,
4576 int copyID,
4577 ht_stack_T **ht_stack,
4578 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004579{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004580 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004581
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004582 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004583 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004584 dict_T *dd = tv->vval.v_dict;
4585
Bram Moolenaara03f2332016-02-06 18:09:59 +01004586 if (dd != NULL && dd->dv_copyID != copyID)
4587 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004588 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004589 dd->dv_copyID = copyID;
4590 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004591 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004592 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4593 }
4594 else
4595 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004596 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4597
Bram Moolenaara03f2332016-02-06 18:09:59 +01004598 if (newitem == NULL)
4599 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004600 else
4601 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004602 newitem->ht = &dd->dv_hashtab;
4603 newitem->prev = *ht_stack;
4604 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004605 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004606 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004607 }
4608 }
4609 else if (tv->v_type == VAR_LIST)
4610 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004611 list_T *ll = tv->vval.v_list;
4612
Bram Moolenaara03f2332016-02-06 18:09:59 +01004613 if (ll != NULL && ll->lv_copyID != copyID)
4614 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004615 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004616 ll->lv_copyID = copyID;
4617 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004618 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004619 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004620 }
4621 else
4622 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004623 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4624
Bram Moolenaara03f2332016-02-06 18:09:59 +01004625 if (newitem == NULL)
4626 abort = TRUE;
4627 else
4628 {
4629 newitem->list = ll;
4630 newitem->prev = *list_stack;
4631 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004632 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004633 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004634 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004635 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004636 else if (tv->v_type == VAR_FUNC)
4637 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004638 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004639 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004640 else if (tv->v_type == VAR_PARTIAL)
4641 {
4642 partial_T *pt = tv->vval.v_partial;
4643 int i;
4644
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004645 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004646 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004647 // Didn't see this partial yet.
4648 pt->pt_copyID = copyID;
4649
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004650 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004651
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004652 if (pt->pt_dict != NULL)
4653 {
4654 typval_T dtv;
4655
4656 dtv.v_type = VAR_DICT;
4657 dtv.vval.v_dict = pt->pt_dict;
4658 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4659 }
4660
4661 for (i = 0; i < pt->pt_argc; ++i)
4662 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4663 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004664 if (pt->pt_funcstack != NULL)
4665 {
4666 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4667
4668 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4669 abort = abort || set_ref_in_item(stack + i, copyID,
4670 ht_stack, list_stack);
4671 }
4672
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004673 }
4674 }
4675#ifdef FEAT_JOB_CHANNEL
4676 else if (tv->v_type == VAR_JOB)
4677 {
4678 job_T *job = tv->vval.v_job;
4679 typval_T dtv;
4680
4681 if (job != NULL && job->jv_copyID != copyID)
4682 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004683 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004684 if (job->jv_channel != NULL)
4685 {
4686 dtv.v_type = VAR_CHANNEL;
4687 dtv.vval.v_channel = job->jv_channel;
4688 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4689 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004690 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004691 {
4692 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004693 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004694 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4695 }
4696 }
4697 }
4698 else if (tv->v_type == VAR_CHANNEL)
4699 {
4700 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004701 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004702 typval_T dtv;
4703 jsonq_T *jq;
4704 cbq_T *cq;
4705
4706 if (ch != NULL && ch->ch_copyID != copyID)
4707 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004708 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004709 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004710 {
4711 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4712 jq = jq->jq_next)
4713 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4714 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4715 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004716 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004717 {
4718 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004719 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004720 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4721 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004722 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004723 {
4724 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004725 dtv.vval.v_partial =
4726 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004727 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4728 }
4729 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004730 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004731 {
4732 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004733 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004734 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4735 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004736 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004737 {
4738 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004739 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004740 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4741 }
4742 }
4743 }
4744#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004745 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004746}
4747
Bram Moolenaar8c711452005-01-14 21:53:12 +00004748/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004749 * Return a string with the string representation of a variable.
4750 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004751 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004752 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004753 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004754 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02004755 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004756 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4757 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004758 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004759 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004760 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004761echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004762 typval_T *tv,
4763 char_u **tofree,
4764 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004765 int copyID,
4766 int echo_style,
4767 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004768 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004769{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004770 static int recurse = 0;
4771 char_u *r = NULL;
4772
Bram Moolenaar33570922005-01-25 22:26:29 +00004773 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004774 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004775 if (!did_echo_string_emsg)
4776 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004777 // Only give this message once for a recursive call to avoid
4778 // flooding the user with errors. And stop iterating over lists
4779 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004780 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004781 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004782 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004783 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004784 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004785 }
4786 ++recurse;
4787
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004788 switch (tv->v_type)
4789 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004790 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004791 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004792 {
4793 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004794 r = tv->vval.v_string;
4795 if (r == NULL)
4796 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004797 }
4798 else
4799 {
4800 *tofree = string_quote(tv->vval.v_string, FALSE);
4801 r = *tofree;
4802 }
4803 break;
4804
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004805 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004806 if (echo_style)
4807 {
4808 *tofree = NULL;
4809 r = tv->vval.v_string;
4810 }
4811 else
4812 {
4813 *tofree = string_quote(tv->vval.v_string, TRUE);
4814 r = *tofree;
4815 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004816 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004817
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004818 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004819 {
4820 partial_T *pt = tv->vval.v_partial;
4821 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004822 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004823 garray_T ga;
4824 int i;
4825 char_u *tf;
4826
4827 ga_init2(&ga, 1, 100);
4828 ga_concat(&ga, (char_u *)"function(");
4829 if (fname != NULL)
4830 {
4831 ga_concat(&ga, fname);
4832 vim_free(fname);
4833 }
4834 if (pt != NULL && pt->pt_argc > 0)
4835 {
4836 ga_concat(&ga, (char_u *)", [");
4837 for (i = 0; i < pt->pt_argc; ++i)
4838 {
4839 if (i > 0)
4840 ga_concat(&ga, (char_u *)", ");
4841 ga_concat(&ga,
4842 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4843 vim_free(tf);
4844 }
4845 ga_concat(&ga, (char_u *)"]");
4846 }
4847 if (pt != NULL && pt->pt_dict != NULL)
4848 {
4849 typval_T dtv;
4850
4851 ga_concat(&ga, (char_u *)", ");
4852 dtv.v_type = VAR_DICT;
4853 dtv.vval.v_dict = pt->pt_dict;
4854 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4855 vim_free(tf);
4856 }
4857 ga_concat(&ga, (char_u *)")");
4858
4859 *tofree = ga.ga_data;
4860 r = *tofree;
4861 break;
4862 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004863
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004864 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004865 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004866 break;
4867
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004868 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004869 if (tv->vval.v_list == NULL)
4870 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004871 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004872 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004873 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004874 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004875 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4876 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004877 {
4878 *tofree = NULL;
4879 r = (char_u *)"[...]";
4880 }
4881 else
4882 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004883 int old_copyID = tv->vval.v_list->lv_copyID;
4884
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004885 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004886 *tofree = list2string(tv, copyID, restore_copyID);
4887 if (restore_copyID)
4888 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004889 r = *tofree;
4890 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004891 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004892
Bram Moolenaar8c711452005-01-14 21:53:12 +00004893 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004894 if (tv->vval.v_dict == NULL)
4895 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004896 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004897 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004898 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004899 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004900 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4901 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004902 {
4903 *tofree = NULL;
4904 r = (char_u *)"{...}";
4905 }
4906 else
4907 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004908 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004909
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004910 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004911 *tofree = dict2string(tv, copyID, restore_copyID);
4912 if (restore_copyID)
4913 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004914 r = *tofree;
4915 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004916 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004917
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004918 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004919 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004920 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004921 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004922 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004923 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004924 break;
4925
Bram Moolenaar835dc632016-02-07 14:27:38 +01004926 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004927 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004928 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004929 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004930 if (composite_val)
4931 {
4932 *tofree = string_quote(r, FALSE);
4933 r = *tofree;
4934 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004935 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004936
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004937 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004938#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004939 *tofree = NULL;
4940 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4941 r = numbuf;
4942 break;
4943#endif
4944
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004945 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004946 case VAR_SPECIAL:
4947 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004948 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004949 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004950 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004951
Bram Moolenaar8502c702014-06-17 12:51:16 +02004952 if (--recurse == 0)
4953 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004954 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004955}
4956
4957/*
4958 * Return a string with the string representation of a variable.
4959 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4960 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004961 * Does not put quotes around strings, as ":echo" displays values.
4962 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4963 * May return NULL.
4964 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004965 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004966echo_string(
4967 typval_T *tv,
4968 char_u **tofree,
4969 char_u *numbuf,
4970 int copyID)
4971{
4972 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4973}
4974
4975/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004976 * Return string "str" in ' quotes, doubling ' characters.
4977 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004978 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004979 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004980 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004981string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004982{
Bram Moolenaar33570922005-01-25 22:26:29 +00004983 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004984 char_u *p, *r, *s;
4985
Bram Moolenaar33570922005-01-25 22:26:29 +00004986 len = (function ? 13 : 3);
4987 if (str != NULL)
4988 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004989 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004990 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004991 if (*p == '\'')
4992 ++len;
4993 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004994 s = r = alloc(len);
4995 if (r != NULL)
4996 {
4997 if (function)
4998 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004999 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005000 r += 10;
5001 }
5002 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005003 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005004 if (str != NULL)
5005 for (p = str; *p != NUL; )
5006 {
5007 if (*p == '\'')
5008 *r++ = '\'';
5009 MB_COPY_CHAR(p, r);
5010 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005011 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005012 if (function)
5013 *r++ = ')';
5014 *r++ = NUL;
5015 }
5016 return s;
5017}
5018
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005019#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005020/*
5021 * Convert the string "text" to a floating point number.
5022 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
5023 * this always uses a decimal point.
5024 * Returns the length of the text that was consumed.
5025 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005026 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005027string2float(
5028 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005029 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005030{
5031 char *s = (char *)text;
5032 float_T f;
5033
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005034 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01005035 if (STRNICMP(text, "inf", 3) == 0)
5036 {
5037 *value = INFINITY;
5038 return 3;
5039 }
5040 if (STRNICMP(text, "-inf", 3) == 0)
5041 {
5042 *value = -INFINITY;
5043 return 4;
5044 }
5045 if (STRNICMP(text, "nan", 3) == 0)
5046 {
5047 *value = NAN;
5048 return 3;
5049 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005050 f = strtod(s, &s);
5051 *value = f;
5052 return (int)((char_u *)s - text);
5053}
5054#endif
5055
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005056/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005057 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5058 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005059 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005060 */
5061 int
5062buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5063{
5064 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005065 char_u *t;
5066 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005067
5068 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5069 return -1;
5070
5071 if (lnum > buf->b_ml.ml_line_count)
5072 lnum = buf->b_ml.ml_line_count;
5073
5074 str = ml_get_buf(buf, lnum, FALSE);
5075 if (str == NULL)
5076 return -1;
5077
5078 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005079 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005080
Bram Moolenaar91458462021-01-13 20:08:38 +01005081 // count the number of characters
5082 t = str;
5083 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5084 t += mb_ptr2len(t);
5085
5086 // In insert mode, when the cursor is at the end of a non-empty line,
5087 // byteidx points to the NUL character immediately past the end of the
5088 // string. In this case, add one to the character count.
5089 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5090 count++;
5091
5092 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005093}
5094
5095/*
5096 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005097 * byte index. Works only for loaded buffers. Returns -1 on failure.
5098 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005099 */
5100 int
5101buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5102{
5103 char_u *str;
5104 char_u *t;
5105
5106 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5107 return -1;
5108
5109 if (lnum > buf->b_ml.ml_line_count)
5110 lnum = buf->b_ml.ml_line_count;
5111
5112 str = ml_get_buf(buf, lnum, FALSE);
5113 if (str == NULL)
5114 return -1;
5115
5116 // Convert the character offset to a byte offset
5117 t = str;
5118 while (*t != NUL && --charidx > 0)
5119 t += mb_ptr2len(t);
5120
Bram Moolenaar91458462021-01-13 20:08:38 +01005121 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005122}
5123
5124/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005126 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005128 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005129var2fpos(
5130 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005131 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005132 int *fnum, // set to fnum for '0, 'A, etc.
5133 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005135 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005137 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005139 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005140 if (varp->v_type == VAR_LIST)
5141 {
5142 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005143 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005144 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005145 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005146
5147 l = varp->vval.v_list;
5148 if (l == NULL)
5149 return NULL;
5150
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005151 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005152 pos.lnum = list_find_nr(l, 0L, &error);
5153 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005154 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005155 if (charcol)
5156 len = (long)mb_charlen(ml_get(pos.lnum));
5157 else
5158 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005159
Bram Moolenaarec65d772020-08-20 22:29:12 +02005160 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005161 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005162 li = list_find(l, 1L);
5163 if (li != NULL && li->li_tv.v_type == VAR_STRING
5164 && li->li_tv.vval.v_string != NULL
5165 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005166 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005167 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005168 }
5169 else
5170 {
5171 pos.col = list_find_nr(l, 1L, &error);
5172 if (error)
5173 return NULL;
5174 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005175
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005176 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005177 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005178 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005179 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005180
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005181 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005182 pos.coladd = list_find_nr(l, 2L, &error);
5183 if (error)
5184 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005185
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005186 return &pos;
5187 }
5188
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005189 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005190 if (name == NULL)
5191 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005192 if (name[0] == '.') // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005193 {
5194 pos = curwin->w_cursor;
5195 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005196 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005197 return &pos;
5198 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005199 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005200 {
5201 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005202 pos = VIsual;
5203 else
5204 pos = curwin->w_cursor;
5205 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005206 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005207 return &pos;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005208 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005209 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005211 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5213 return NULL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005214 if (charcol)
Bram Moolenaar91458462021-01-13 20:08:38 +01005215 pp->col = buf_byteidx_to_charidx(curbuf, pp->lnum, pp->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 return pp;
5217 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005218
Bram Moolenaara5525202006-03-02 22:52:09 +00005219 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005220
Bram Moolenaar477933c2007-07-17 14:32:23 +00005221 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005222 {
5223 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005224 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005225 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005226 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005227 // In silent Ex mode topline is zero, but that's not a valid line
5228 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005229 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005230 return &pos;
5231 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005232 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005233 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005234 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005235 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005236 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005237 return &pos;
5238 }
5239 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005240 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005242 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 {
5244 pos.lnum = curbuf->b_ml.ml_line_count;
5245 pos.col = 0;
5246 }
5247 else
5248 {
5249 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005250 if (charcol)
5251 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5252 else
5253 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 }
5255 return &pos;
5256 }
5257 return NULL;
5258}
5259
5260/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005261 * Convert list in "arg" into a position and optional file number.
5262 * When "fnump" is NULL there is no file number, only 3 items.
5263 * Note that the column is passed on as-is, the caller may want to decrement
5264 * it to use 1 for the first column.
5265 * Return FAIL when conversion is not possible, doesn't check the position for
5266 * validity.
5267 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005268 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005269list2fpos(
5270 typval_T *arg,
5271 pos_T *posp,
5272 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005273 colnr_T *curswantp,
5274 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005275{
5276 list_T *l = arg->vval.v_list;
5277 long i = 0;
5278 long n;
5279
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005280 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5281 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005282 if (arg->v_type != VAR_LIST
5283 || l == NULL
5284 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005285 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005286 return FAIL;
5287
5288 if (fnump != NULL)
5289 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005290 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005291 if (n < 0)
5292 return FAIL;
5293 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005294 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005295 *fnump = n;
5296 }
5297
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005298 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005299 if (n < 0)
5300 return FAIL;
5301 posp->lnum = n;
5302
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005303 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005304 if (n < 0)
5305 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005306 // If character position is specified, then convert to byte position
5307 if (charcol)
5308 {
5309 buf_T *buf;
5310
5311 // Get the text for the specified line in a loaded buffer
5312 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5313 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5314 return FAIL;
5315
Bram Moolenaar91458462021-01-13 20:08:38 +01005316 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005317 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005318 posp->col = n;
5319
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005320 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005321 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005322 posp->coladd = 0;
5323 else
5324 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005325
Bram Moolenaar493c1782014-05-28 14:34:46 +02005326 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005327 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005328
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005329 return OK;
5330}
5331
5332/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 * Get the length of an environment variable name.
5334 * Advance "arg" to the first character after the name.
5335 * Return 0 for error.
5336 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005337 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005338get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339{
5340 char_u *p;
5341 int len;
5342
5343 for (p = *arg; vim_isIDc(*p); ++p)
5344 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005345 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 return 0;
5347
5348 len = (int)(p - *arg);
5349 *arg = p;
5350 return len;
5351}
5352
5353/*
5354 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005355 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 * Return 0 if something is wrong.
5357 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005358 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005359get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360{
5361 char_u *p;
5362 int len;
5363
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005364 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005366 {
5367 if (*p == ':')
5368 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005369 // "s:" is start of "s:var", but "n:" is not and can be used in
5370 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005371 len = (int)(p - *arg);
5372 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5373 || len > 1)
5374 break;
5375 }
5376 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005377 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 return 0;
5379
5380 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005381 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382
5383 return len;
5384}
5385
5386/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005387 * Get the length of the name of a variable or function.
5388 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005390 * Return -1 if curly braces expansion failed.
5391 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 * If the name contains 'magic' {}'s, expand them and return the
5393 * expanded name in an allocated string via 'alias' - caller must free.
5394 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005395 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005396get_name_len(
5397 char_u **arg,
5398 char_u **alias,
5399 int evaluate,
5400 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401{
5402 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 char_u *p;
5404 char_u *expr_start;
5405 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005407 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408
5409 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5410 && (*arg)[2] == (int)KE_SNR)
5411 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005412 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 *arg += 3;
5414 return get_id_len(arg) + 3;
5415 }
5416 len = eval_fname_script(*arg);
5417 if (len > 0)
5418 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005419 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 *arg += len;
5421 }
5422
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005424 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005426 p = find_name_end(*arg, &expr_start, &expr_end,
5427 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 if (expr_start != NULL)
5429 {
5430 char_u *temp_string;
5431
5432 if (!evaluate)
5433 {
5434 len += (int)(p - *arg);
5435 *arg = skipwhite(p);
5436 return len;
5437 }
5438
5439 /*
5440 * Include any <SID> etc in the expanded string:
5441 * Thus the -len here.
5442 */
5443 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5444 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005445 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 *alias = temp_string;
5447 *arg = skipwhite(p);
5448 return (int)STRLEN(temp_string);
5449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450
5451 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005452 // Only give an error when there is something, otherwise it will be
5453 // reported at a higher level.
5454 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005455 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456
5457 return len;
5458}
5459
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005460/*
5461 * Find the end of a variable or function name, taking care of magic braces.
5462 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5463 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005464 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005465 * Return a pointer to just after the name. Equal to "arg" if there is no
5466 * valid name.
5467 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005468 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005469find_name_end(
5470 char_u *arg,
5471 char_u **expr_start,
5472 char_u **expr_end,
5473 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005475 int mb_nest = 0;
5476 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005478 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005479 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005481 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005483 *expr_start = NULL;
5484 *expr_end = NULL;
5485 }
5486
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005487 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005488 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5489 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005490 return arg;
5491
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005492 for (p = arg; *p != NUL
5493 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005494 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005495 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005496 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005497 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005498 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005499 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005500 if (*p == '\'')
5501 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005502 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005503 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005504 ;
5505 if (*p == NUL)
5506 break;
5507 }
5508 else if (*p == '"')
5509 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005510 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005511 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005512 if (*p == '\\' && p[1] != NUL)
5513 ++p;
5514 if (*p == NUL)
5515 break;
5516 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005517 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5518 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005519 // "s:" is start of "s:var", but "n:" is not and can be used in
5520 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005521 len = (int)(p - arg);
5522 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005523 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005524 break;
5525 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005526
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005527 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005529 if (*p == '[')
5530 ++br_nest;
5531 else if (*p == ']')
5532 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005534
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005535 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005537 if (*p == '{')
5538 {
5539 mb_nest++;
5540 if (expr_start != NULL && *expr_start == NULL)
5541 *expr_start = p;
5542 }
5543 else if (*p == '}')
5544 {
5545 mb_nest--;
5546 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5547 *expr_end = p;
5548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 }
5551
5552 return p;
5553}
5554
5555/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005556 * Expands out the 'magic' {}'s in a variable/function name.
5557 * Note that this can call itself recursively, to deal with
5558 * constructs like foo{bar}{baz}{bam}
5559 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5560 * "in_start" ^
5561 * "expr_start" ^
5562 * "expr_end" ^
5563 * "in_end" ^
5564 *
5565 * Returns a new allocated string, which the caller must free.
5566 * Returns NULL for failure.
5567 */
5568 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005569make_expanded_name(
5570 char_u *in_start,
5571 char_u *expr_start,
5572 char_u *expr_end,
5573 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005574{
5575 char_u c1;
5576 char_u *retval = NULL;
5577 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005578
5579 if (expr_end == NULL || in_end == NULL)
5580 return NULL;
5581 *expr_start = NUL;
5582 *expr_end = NUL;
5583 c1 = *in_end;
5584 *in_end = NUL;
5585
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005586 temp_result = eval_to_string(expr_start + 1, FALSE);
5587 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005588 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005589 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5590 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005591 if (retval != NULL)
5592 {
5593 STRCPY(retval, in_start);
5594 STRCAT(retval, temp_result);
5595 STRCAT(retval, expr_end + 1);
5596 }
5597 }
5598 vim_free(temp_result);
5599
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005600 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005601 *expr_start = '{';
5602 *expr_end = '}';
5603
5604 if (retval != NULL)
5605 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005606 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005607 if (expr_start != NULL)
5608 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005609 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005610 temp_result = make_expanded_name(retval, expr_start,
5611 expr_end, temp_result);
5612 vim_free(retval);
5613 retval = temp_result;
5614 }
5615 }
5616
5617 return retval;
5618}
5619
5620/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005622 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005624 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005625eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005627 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005628}
5629
5630/*
5631 * Return TRUE if character "c" can be used as the first character in a
5632 * variable or function name (excluding '{' and '}').
5633 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005634 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005635eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005636{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005637 return ASCII_ISALPHA(c) || c == '_';
5638}
5639
5640/*
5641 * Return TRUE if character "c" can be used as the first character of a
5642 * dictionary key.
5643 */
5644 int
5645eval_isdictc(int c)
5646{
5647 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648}
5649
5650/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005651 * Handle:
5652 * - expr[expr], expr[expr:expr] subscript
5653 * - ".name" lookup
5654 * - function call with Funcref variable: func(expr)
5655 * - method call: var->method()
5656 *
5657 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005658 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005659 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005660handle_subscript(
5661 char_u **arg,
5662 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005663 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005664 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005665{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005666 int evaluate = evalarg != NULL
5667 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005668 int ret = OK;
5669 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005670 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005671 int getnext;
5672 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005673
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005674 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005675 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005676 // When at the end of the line and ".name" or "->{" or "->X" follows in
5677 // the next line then consume the line break.
5678 p = eval_next_non_blank(*arg, evalarg, &getnext);
5679 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005680 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaar9d489562020-07-30 20:08:50 +02005681 || (p[0] == '-' && p[1] == '>'
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005682 && (p[2] == '{' || ASCII_ISALPHA(p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005683 {
5684 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005685 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005686 check_white = FALSE;
5687 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005688
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005689 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5690 || rettv->v_type == VAR_PARTIAL))
5691 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005692 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005693 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5694 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005695
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005696 // Stop the expression evaluation when immediately aborting on
5697 // error, or when an interrupt occurred or an exception was thrown
5698 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005699 if (aborting())
5700 {
5701 if (ret == OK)
5702 clear_tv(rettv);
5703 ret = FAIL;
5704 }
5705 dict_unref(selfdict);
5706 selfdict = NULL;
5707 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005708 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005709 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02005710 *arg = p;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005711 if (ret == OK)
5712 {
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01005713 if (((*arg)[2] == '{' && !in_vim9script()) || (*arg)[2] == '(')
5714 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005715 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005716 else
5717 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005718 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005719 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005720 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005721 // "." is ".name" lookup when we found a dict or when evaluating and
5722 // scriptversion is at least 2, where string concatenation is "..".
5723 else if (**arg == '['
5724 || (**arg == '.' && (rettv->v_type == VAR_DICT
5725 || (!evaluate
5726 && (*arg)[1] != '.'
5727 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005728 {
5729 dict_unref(selfdict);
5730 if (rettv->v_type == VAR_DICT)
5731 {
5732 selfdict = rettv->vval.v_dict;
5733 if (selfdict != NULL)
5734 ++selfdict->dv_refcount;
5735 }
5736 else
5737 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005738 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005739 {
5740 clear_tv(rettv);
5741 ret = FAIL;
5742 }
5743 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005744 else
5745 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005746 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005747
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005748 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5749 // Don't do this when "Func" is already a partial that was bound
5750 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005751 if (selfdict != NULL
5752 && (rettv->v_type == VAR_FUNC
5753 || (rettv->v_type == VAR_PARTIAL
5754 && (rettv->vval.v_partial->pt_auto
5755 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005756 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005757
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005758 dict_unref(selfdict);
5759 return ret;
5760}
5761
5762/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005763 * Make a copy of an item.
5764 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005765 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5766 * reference to an already copied list/dict can be used.
5767 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005768 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005769 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005770item_copy(
5771 typval_T *from,
5772 typval_T *to,
5773 int deep,
5774 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005775{
5776 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005777 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005778
Bram Moolenaar33570922005-01-25 22:26:29 +00005779 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005780 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005781 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005782 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005783 }
5784 ++recurse;
5785
5786 switch (from->v_type)
5787 {
5788 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005789 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005790 case VAR_STRING:
5791 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005792 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005793 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005794 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005795 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005796 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005797 copy_tv(from, to);
5798 break;
5799 case VAR_LIST:
5800 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005801 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005802 if (from->vval.v_list == NULL)
5803 to->vval.v_list = NULL;
5804 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5805 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005806 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005807 to->vval.v_list = from->vval.v_list->lv_copylist;
5808 ++to->vval.v_list->lv_refcount;
5809 }
5810 else
5811 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5812 if (to->vval.v_list == NULL)
5813 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005814 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005815 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005816 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005817 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005818 case VAR_DICT:
5819 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005820 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005821 if (from->vval.v_dict == NULL)
5822 to->vval.v_dict = NULL;
5823 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5824 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005825 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005826 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5827 ++to->vval.v_dict->dv_refcount;
5828 }
5829 else
5830 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5831 if (to->vval.v_dict == NULL)
5832 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005833 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005834 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005835 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005836 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005837 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005838 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005839 }
5840 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005841 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005842}
5843
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005844 void
5845echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5846{
5847 char_u *tofree;
5848 char_u numbuf[NUMBUFLEN];
5849 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5850
5851 if (*atstart)
5852 {
5853 *atstart = FALSE;
5854 // Call msg_start() after eval1(), evaluating the expression
5855 // may cause a message to appear.
5856 if (with_space)
5857 {
5858 // Mark the saved text as finishing the line, so that what
5859 // follows is displayed on a new line when scrolling back
5860 // at the more prompt.
5861 msg_sb_eol();
5862 msg_start();
5863 }
5864 }
5865 else if (with_space)
5866 msg_puts_attr(" ", echo_attr);
5867
5868 if (p != NULL)
5869 for ( ; *p != NUL && !got_int; ++p)
5870 {
5871 if (*p == '\n' || *p == '\r' || *p == TAB)
5872 {
5873 if (*p != TAB && *needclr)
5874 {
5875 // remove any text still there from the command
5876 msg_clr_eos();
5877 *needclr = FALSE;
5878 }
5879 msg_putchar_attr(*p, echo_attr);
5880 }
5881 else
5882 {
5883 if (has_mbyte)
5884 {
5885 int i = (*mb_ptr2len)(p);
5886
5887 (void)msg_outtrans_len_attr(p, i, echo_attr);
5888 p += i - 1;
5889 }
5890 else
5891 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5892 }
5893 }
5894 vim_free(tofree);
5895}
5896
Bram Moolenaare9a41262005-01-15 22:18:47 +00005897/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 * ":echo expr1 ..." print each argument separated with a space, add a
5899 * newline at the end.
5900 * ":echon expr1 ..." print each argument plain.
5901 */
5902 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005903ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904{
5905 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005906 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 char_u *p;
5908 int needclr = TRUE;
5909 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01005910 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005911 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005912 evalarg_T evalarg;
5913
Bram Moolenaare707c882020-07-01 13:07:37 +02005914 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915
5916 if (eap->skip)
5917 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005918 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005920 // If eval1() causes an error message the text from the command may
5921 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005922 need_clr_eos = needclr;
5923
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005925 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 {
5927 /*
5928 * Report the invalid expression unless the expression evaluation
5929 * has been cancelled due to an aborting error, an interrupt, or an
5930 * exception.
5931 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005932 if (!aborting() && did_emsg == did_emsg_before
5933 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005934 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005935 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 break;
5937 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005938 need_clr_eos = FALSE;
5939
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005941 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005942
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005943 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 arg = skipwhite(arg);
5945 }
5946 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02005947 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948
5949 if (eap->skip)
5950 --emsg_skip;
5951 else
5952 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005953 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 if (needclr)
5955 msg_clr_eos();
5956 if (eap->cmdidx == CMD_echo)
5957 msg_end();
5958 }
5959}
5960
5961/*
5962 * ":echohl {name}".
5963 */
5964 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005965ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005967 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968}
5969
5970/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005971 * Returns the :echo attribute
5972 */
5973 int
5974get_echo_attr(void)
5975{
5976 return echo_attr;
5977}
5978
5979/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 * ":execute expr1 ..." execute the result of an expression.
5981 * ":echomsg expr1 ..." Print a message
5982 * ":echoerr expr1 ..." Print an error
5983 * Each gets spaces around each argument and a newline at the end for
5984 * echo commands
5985 */
5986 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005987ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988{
5989 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005990 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 int ret = OK;
5992 char_u *p;
5993 garray_T ga;
5994 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995
5996 ga_init2(&ga, 1, 80);
5997
5998 if (eap->skip)
5999 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006000 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006002 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006003 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005
6006 if (!eap->skip)
6007 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006008 char_u buf[NUMBUFLEN];
6009
6010 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006011 {
6012 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6013 {
6014 emsg(_(e_inval_string));
6015 p = NULL;
6016 }
6017 else
6018 p = tv_get_string_buf(&rettv, buf);
6019 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006020 else
6021 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006022 if (p == NULL)
6023 {
6024 clear_tv(&rettv);
6025 ret = FAIL;
6026 break;
6027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 len = (int)STRLEN(p);
6029 if (ga_grow(&ga, len + 2) == FAIL)
6030 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006031 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 ret = FAIL;
6033 break;
6034 }
6035 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 ga.ga_len += len;
6039 }
6040
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006041 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 arg = skipwhite(arg);
6043 }
6044
6045 if (ret != FAIL && ga.ga_data != NULL)
6046 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006047 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6048 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006049 // Mark the already saved text as finishing the line, so that what
6050 // follows is displayed on a new line when scrolling back at the
6051 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006052 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006053 }
6054
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006056 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006057 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006058 out_flush();
6059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 else if (eap->cmdidx == CMD_echoerr)
6061 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006062 int save_did_emsg = did_emsg;
6063
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006064 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006065 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 if (!force_abort)
6067 did_emsg = save_did_emsg;
6068 }
6069 else if (eap->cmdidx == CMD_execute)
6070 do_cmdline((char_u *)ga.ga_data,
6071 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6072 }
6073
6074 ga_clear(&ga);
6075
6076 if (eap->skip)
6077 --emsg_skip;
6078
6079 eap->nextcmd = check_nextcmd(arg);
6080}
6081
6082/*
6083 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6084 * "arg" points to the "&" or '+' when called, to "option" when returning.
6085 * Returns NULL when no option name found. Otherwise pointer to the char
6086 * after the option name.
6087 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006088 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006089find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090{
6091 char_u *p = *arg;
6092
6093 ++p;
6094 if (*p == 'g' && p[1] == ':')
6095 {
6096 *opt_flags = OPT_GLOBAL;
6097 p += 2;
6098 }
6099 else if (*p == 'l' && p[1] == ':')
6100 {
6101 *opt_flags = OPT_LOCAL;
6102 p += 2;
6103 }
6104 else
6105 *opt_flags = 0;
6106
6107 if (!ASCII_ISALPHA(*p))
6108 return NULL;
6109 *arg = p;
6110
6111 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006112 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 else
6114 while (ASCII_ISALPHA(*p))
6115 ++p;
6116 return p;
6117}
6118
6119/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006120 * Display script name where an item was last set.
6121 * Should only be invoked when 'verbose' is non-zero.
6122 */
6123 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006124last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006125{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006126 char_u *p;
6127
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006128 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006129 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006130 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006131 if (p != NULL)
6132 {
6133 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006134 msg_puts(_("\n\tLast set from "));
6135 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006136 if (script_ctx.sc_lnum > 0)
6137 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006138 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006139 msg_outnum((long)script_ctx.sc_lnum);
6140 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006141 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006142 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006143 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006144 }
6145}
6146
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006147#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148
6149/*
6150 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006151 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 * "flags" can be "g" to do a global substitute.
6153 * Returns an allocated string, NULL for error.
6154 */
6155 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006156do_string_sub(
6157 char_u *str,
6158 char_u *pat,
6159 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006160 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006161 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162{
6163 int sublen;
6164 regmatch_T regmatch;
6165 int i;
6166 int do_all;
6167 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006168 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 garray_T ga;
6170 char_u *ret;
6171 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006172 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006174 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006176 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177
6178 ga_init2(&ga, 1, 200);
6179
6180 do_all = (flags[0] == 'g');
6181
6182 regmatch.rm_ic = p_ic;
6183 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6184 if (regmatch.regprog != NULL)
6185 {
6186 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006187 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6189 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006190 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006191 if (regmatch.startp[0] == regmatch.endp[0])
6192 {
6193 if (zero_width == regmatch.startp[0])
6194 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006195 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006196 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006197 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6198 (size_t)i);
6199 ga.ga_len += i;
6200 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006201 continue;
6202 }
6203 zero_width = regmatch.startp[0];
6204 }
6205
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 /*
6207 * Get some space for a temporary buffer to do the substitution
6208 * into. It will contain:
6209 * - The text up to where the match is.
6210 * - The substituted text.
6211 * - The text after the match.
6212 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006213 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006214 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6216 {
6217 ga_clear(&ga);
6218 break;
6219 }
6220
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006221 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 i = (int)(regmatch.startp[0] - tail);
6223 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006224 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006225 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 + ga.ga_len + i, TRUE, TRUE, FALSE);
6227 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006228 tail = regmatch.endp[0];
6229 if (*tail == NUL)
6230 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 if (!do_all)
6232 break;
6233 }
6234
6235 if (ga.ga_data != NULL)
6236 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6237
Bram Moolenaar473de612013-06-08 18:19:48 +02006238 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 }
6240
6241 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6242 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006243 if (p_cpo == empty_option)
6244 p_cpo = save_cpo;
6245 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006246 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006247 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006248 // If it's still empty it was changed and restored, need to restore in
6249 // the complicated way.
6250 if (*p_cpo == NUL)
6251 set_option_value((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006252 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01006253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254
6255 return ret;
6256}