blob: f84491e043a6eae1fc3e6afa295ca3a8d0143730 [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 Moolenaarb544f3c2017-02-23 19:03:28 +0100658 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000659 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200660 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
661 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000662 */
663 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100664call_func_retstr(
665 char_u *func,
666 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200667 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000668{
669 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000670 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000671
Bram Moolenaarded27a12018-08-01 19:06:03 +0200672 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000673 return NULL;
674
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100675 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000676 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 return retval;
678}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000679
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000680/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100681 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200682 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
683 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000684 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000685 */
686 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100687call_func_retlist(
688 char_u *func,
689 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200690 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000691{
692 typval_T rettv;
693
Bram Moolenaarded27a12018-08-01 19:06:03 +0200694 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000695 return NULL;
696
697 if (rettv.v_type != VAR_LIST)
698 {
699 clear_tv(&rettv);
700 return NULL;
701 }
702
703 return rettv.vval.v_list;
704}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706#ifdef FEAT_FOLDING
707/*
708 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
709 * it in "*cp". Doesn't give error messages.
710 */
711 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100712eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713{
Bram Moolenaar33570922005-01-25 22:26:29 +0000714 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200715 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000717 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
718 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719
720 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000721 if (use_sandbox)
722 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200723 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200725 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 retval = 0;
727 else
728 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100729 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000730 if (tv.v_type == VAR_NUMBER)
731 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000732 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 retval = 0;
734 else
735 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100736 // If the result is a string, check if there is a non-digit before
737 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000738 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 if (!VIM_ISDIGIT(*s) && *s != '-')
740 *cp = *s++;
741 retval = atol((char *)s);
742 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000743 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 }
745 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000746 if (use_sandbox)
747 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200748 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200749 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200751 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752}
753#endif
754
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000756 * Get an lval: variable, Dict item or List item that can be assigned a value
757 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
758 * "name.key", "name.key[expr]" etc.
759 * Indexing only works if "name" is an existing List or Dictionary.
760 * "name" points to the start of the name.
761 * If "rettv" is not NULL it points to the value to be assigned.
762 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
763 * wrong; must end in space or cmd separator.
764 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100765 * flags:
766 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100767 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100768 * GLV_NO_AUTOLOAD: do not use script autoloading
769 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000770 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000771 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000772 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000773 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200774 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100775get_lval(
776 char_u *name,
777 typval_T *rettv,
778 lval_T *lp,
779 int unlet,
780 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100781 int flags, // GLV_ values
782 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000783{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000784 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000785 char_u *expr_start, *expr_end;
786 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000787 dictitem_T *v;
788 typval_T var1;
789 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000790 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000791 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000792 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000793 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100794 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100795 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000796
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100797 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200798 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000799
800 if (skip)
801 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100802 // When skipping just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000803 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200804 lp->ll_name_end = find_name_end(name, NULL, NULL,
805 FNE_INCL_BR | fne_flags);
806 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000807 }
808
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100809 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000810 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100811 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000812 if (expr_start != NULL)
813 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100814 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100815 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000816 && *p != '[' && *p != '.')
817 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200818 semsg(_(e_trailing_arg), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000819 return NULL;
820 }
821
822 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
823 if (lp->ll_exp_name == NULL)
824 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100825 // Report an invalid expression in braces, unless the
826 // expression evaluation has been cancelled due to an
827 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000828 if (!aborting() && !quiet)
829 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000830 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100831 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000832 return NULL;
833 }
834 }
835 lp->ll_name = lp->ll_exp_name;
836 }
837 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100838 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000839 lp->ll_name = name;
840
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200841 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100842 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200843 // "a: type" is declaring variable "a" with a type, not "a:".
844 if (p == name + 2 && p[-1] == ':')
845 {
846 --p;
847 lp->ll_name_end = p;
848 }
849 if (*p == ':')
850 {
851 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
852 char_u *tp = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100853
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200854 // parse the type after the name
855 lp->ll_type = parse_type(&tp, &si->sn_type_list);
856 lp->ll_name_end = tp;
857 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100858 }
859 }
860
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100861 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000862 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
863 return p;
864
865 cc = *p;
866 *p = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100867 // Only pass &ht when we would write to the variable, it prevents autoload
868 // as well.
Bram Moolenaar6e65d592017-12-07 22:11:27 +0100869 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
870 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000871 if (v == NULL && !quiet)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200872 semsg(_(e_undefined_variable_str), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000873 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000874 if (v == NULL)
875 return NULL;
876
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +0100877 if (in_vim9script() && (flags & GLV_NO_DECL) == 0)
878 {
879 if (!quiet)
880 semsg(_(e_variable_already_declared), lp->ll_name);
881 return NULL;
882 }
883
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000884 /*
885 * Loop until no more [idx] or .key is following.
886 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000887 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100888 var1.v_type = VAR_UNKNOWN;
889 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000890 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000891 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000892 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
Bram Moolenaar81ed4962020-09-23 15:56:50 +0200893 && !(lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100894 && !(lp->ll_tv->v_type == VAR_BLOB
895 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000896 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000897 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100898 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000899 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000900 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000901 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000902 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000903 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100904 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000905 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000906 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000907
Bram Moolenaar10c65862020-10-08 21:16:42 +0200908 if (in_vim9script() && lp->ll_valtype == NULL
909 && lp->ll_tv == &v->di_tv
910 && ht != NULL && ht == get_script_local_ht())
911 {
912 svar_T *sv = find_typval_in_script(lp->ll_tv);
913
914 // Vim9 script local variable: get the type
915 if (sv != NULL)
916 lp->ll_valtype = sv->sv_type;
917 }
918
Bram Moolenaar8c711452005-01-14 21:53:12 +0000919 len = -1;
920 if (*p == '.')
921 {
922 key = p + 1;
923 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
924 ;
925 if (len == 0)
926 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000927 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100928 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000929 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000930 }
931 p = key + len;
932 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000933 else
934 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100935 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000936 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000937 if (*p == ':')
938 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000939 else
940 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000941 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200942 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000943 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100944 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000945 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100946 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000947 clear_tv(&var1);
948 return NULL;
949 }
Bram Moolenaar6a250262020-08-04 15:53:01 +0200950 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000951 }
952
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100953 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000954 if (*p == ':')
955 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000956 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000957 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000958 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +0200959 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100960 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000961 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000962 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100963 if (rettv != NULL
964 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100965 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100966 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100967 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +0000968 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000969 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100970 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100971 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000972 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000973 }
974 p = skipwhite(p + 1);
975 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000976 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000977 else
978 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000979 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200980 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200981 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000982 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100983 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000984 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000985 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100986 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000987 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100988 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100989 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000990 clear_tv(&var2);
991 return NULL;
992 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000993 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000994 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000995 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000996 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000997 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000998
Bram Moolenaar8c711452005-01-14 21:53:12 +0000999 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001000 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001001 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001002 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001003 clear_tv(&var1);
1004 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001005 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001006 }
1007
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001008 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001009 ++p;
1010 }
1011
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001012 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001013 {
1014 if (len == -1)
1015 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001016 // "[key]": get key from "var1"
1017 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001018 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001019 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001020 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001021 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001022 }
1023 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001024 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001025
1026 // a NULL dict is equivalent with an empty dict
1027 if (lp->ll_tv->vval.v_dict == NULL)
1028 {
1029 lp->ll_tv->vval.v_dict = dict_alloc();
1030 if (lp->ll_tv->vval.v_dict == NULL)
1031 {
1032 clear_tv(&var1);
1033 return NULL;
1034 }
1035 ++lp->ll_tv->vval.v_dict->dv_refcount;
1036 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001037 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001038
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001039 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001040
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001041 // When assigning to a scope dictionary check that a function and
1042 // variable name is valid (only variable name unless it is l: or
1043 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001044 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001045 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001046 int prevval;
1047 int wrong;
1048
1049 if (len != -1)
1050 {
1051 prevval = key[len];
1052 key[len] = NUL;
1053 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001054 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001055 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001056 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1057 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001058 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar03290b82020-12-19 16:30:44 +01001059 || !valid_varname(key, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001060 if (len != -1)
1061 key[len] = prevval;
1062 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001063 {
1064 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001065 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001066 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001067 }
1068
Bram Moolenaar10c65862020-10-08 21:16:42 +02001069 if (lp->ll_valtype != NULL)
1070 // use the type of the member
1071 lp->ll_valtype = lp->ll_valtype->tt_member;
1072
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001073 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001074 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001075 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001076 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001077 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001078 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001079 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001080 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001081 return NULL;
1082 }
1083
Bram Moolenaar31b81602019-02-10 22:14:27 +01001084 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001085 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001086 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001087 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001088 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001089 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001090 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001091 }
1092 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001093 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001094 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001095 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001096 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001097 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001098 p = NULL;
1099 break;
1100 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001101 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001102 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001103 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1104 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001105 {
1106 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001107 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001108 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001109
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001110 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001111 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001112 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001113 else if (lp->ll_tv->v_type == VAR_BLOB)
1114 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001115 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1116
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001117 /*
1118 * Get the number and item for the only or first index of the List.
1119 */
1120 if (empty1)
1121 lp->ll_n1 = 0;
1122 else
1123 // is number or string
1124 lp->ll_n1 = (long)tv_get_number(&var1);
1125 clear_tv(&var1);
1126
1127 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001128 || lp->ll_n1 > bloblen
1129 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001130 {
1131 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001132 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001133 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001134 return NULL;
1135 }
1136 if (lp->ll_range && !lp->ll_empty2)
1137 {
1138 lp->ll_n2 = (long)tv_get_number(&var2);
1139 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001140 if (lp->ll_n2 < 0
1141 || lp->ll_n2 >= bloblen
1142 || lp->ll_n2 < lp->ll_n1)
1143 {
1144 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001145 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001146 return NULL;
1147 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001148 }
1149 lp->ll_blob = lp->ll_tv->vval.v_blob;
1150 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001151 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001152 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001153 else
1154 {
1155 /*
1156 * Get the number and item for the only or first index of the List.
1157 */
1158 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001159 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001160 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001161 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001162 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001163 clear_tv(&var1);
1164
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001165 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001166 lp->ll_list = lp->ll_tv->vval.v_list;
1167 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1168 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001169 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001170 if (lp->ll_n1 < 0)
1171 {
1172 lp->ll_n1 = 0;
1173 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1174 }
1175 }
1176 if (lp->ll_li == NULL)
1177 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001178 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001179 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001180 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001181 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001182 }
1183
Bram Moolenaar10c65862020-10-08 21:16:42 +02001184 if (lp->ll_valtype != NULL)
1185 // use the type of the member
1186 lp->ll_valtype = lp->ll_valtype->tt_member;
1187
Bram Moolenaar8c711452005-01-14 21:53:12 +00001188 /*
1189 * May need to find the item or absolute index for the second
1190 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001191 * When no index given: "lp->ll_empty2" is TRUE.
1192 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001193 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001194 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001195 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001196 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001197 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001198 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001199 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001200 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001201 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001202 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001203 {
1204 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001205 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001206 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001207 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001208 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001209 }
1210
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001211 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001212 if (lp->ll_n1 < 0)
1213 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1214 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001215 {
1216 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001217 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001218 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001219 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001220 }
1221
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001222 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001223 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001224 }
1225
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001226 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001227 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001228 return p;
1229}
1230
1231/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001232 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001233 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001234 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001235clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001236{
1237 vim_free(lp->ll_exp_name);
1238 vim_free(lp->ll_newkey);
1239}
1240
1241/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001242 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001243 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001244 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1245 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001246 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001247 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001248set_var_lval(
1249 lval_T *lp,
1250 char_u *endp,
1251 typval_T *rettv,
1252 int copy,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001253 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar7454a062016-01-30 15:14:10 +01001254 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001255{
1256 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001257 listitem_T *ri;
1258 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001259
1260 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001261 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001262 cc = *endp;
1263 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001264 if (lp->ll_blob != NULL)
1265 {
1266 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001267
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001268 if (op != NULL && *op != '=')
1269 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001270 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001271 return;
1272 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001273 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001274 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001275
1276 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1277 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001278 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001279
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001280 if (lp->ll_empty2)
1281 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1282
1283 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001284 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001285 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001286 return;
1287 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001288 if (lp->ll_empty2)
1289 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001290
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001291 ir = 0;
1292 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1293 blob_set(lp->ll_blob, il,
1294 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001295 }
1296 else
1297 {
1298 val = (int)tv_get_number_chk(rettv, &error);
1299 if (!error)
1300 {
1301 garray_T *gap = &lp->ll_blob->bv_ga;
1302
1303 // Allow for appending a byte. Setting a byte beyond
1304 // the end is an error otherwise.
1305 if (lp->ll_n1 < gap->ga_len
1306 || (lp->ll_n1 == gap->ga_len
1307 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1308 {
1309 blob_set(lp->ll_blob, lp->ll_n1, val);
1310 if (lp->ll_n1 == gap->ga_len)
1311 ++gap->ga_len;
1312 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001313 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001314 }
1315 }
1316 }
1317 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001318 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001319 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001320
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001321 if (flags & ASSIGN_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001322 {
1323 emsg(_(e_cannot_mod));
1324 *endp = cc;
1325 return;
1326 }
1327
Bram Moolenaarff697e62019-02-12 22:28:33 +01001328 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001329 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001330 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar79518e22017-02-17 16:31:35 +01001331 &tv, &di, TRUE, FALSE) == OK)
1332 {
1333 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001334 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1335 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001336 && tv_op(&tv, rettv, op) == OK)
1337 set_var(lp->ll_name, &tv, FALSE);
1338 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001339 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001340 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001341 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001342 {
1343 if (lp->ll_type != NULL
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001344 && check_typval_type(lp->ll_type, rettv, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001345 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001346 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001347 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001348 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001349 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001350 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001351 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001352 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001353 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001354 else if (lp->ll_range)
1355 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001356 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001357 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001358
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001359 if (flags & ASSIGN_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001360 {
1361 emsg(_("E996: Cannot lock a range"));
1362 return;
1363 }
1364
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001365 /*
1366 * Check whether any of the list items is locked
1367 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001368 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001369 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001370 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001371 return;
1372 ri = ri->li_next;
1373 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1374 break;
1375 ll_li = ll_li->li_next;
1376 ++ll_n1;
1377 }
1378
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001379 /*
1380 * Assign the List values to the list items.
1381 */
1382 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001383 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001384 if (op != NULL && *op != '=')
1385 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1386 else
1387 {
1388 clear_tv(&lp->ll_li->li_tv);
1389 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1390 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001391 ri = ri->li_next;
1392 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1393 break;
1394 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001395 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001396 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001397 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001398 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001399 ri = NULL;
1400 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001401 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001402 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001403 lp->ll_li = lp->ll_li->li_next;
1404 ++lp->ll_n1;
1405 }
1406 if (ri != NULL)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001407 emsg(_(e_list_value_has_more_items_than_targets));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001408 else if (lp->ll_empty2
1409 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001410 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001411 emsg(_(e_list_value_does_not_have_enough_items));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001412 }
1413 else
1414 {
1415 /*
1416 * Assign to a List or Dictionary item.
1417 */
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001418 if (flags & ASSIGN_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001419 {
1420 emsg(_("E996: Cannot lock a list or dict"));
1421 return;
1422 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001423
1424 if (lp->ll_valtype != NULL
1425 && check_typval_type(lp->ll_valtype, rettv, 0) == FAIL)
1426 return;
1427
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001428 if (lp->ll_newkey != NULL)
1429 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001430 if (op != NULL && *op != '=')
1431 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001432 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001433 return;
1434 }
1435
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001436 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001437 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001438 if (di == NULL)
1439 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001440 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1441 {
1442 vim_free(di);
1443 return;
1444 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001445 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001446 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001447 else if (op != NULL && *op != '=')
1448 {
1449 tv_op(lp->ll_tv, rettv, op);
1450 return;
1451 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001452 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001453 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001454
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001455 /*
1456 * Assign the value to the variable or list item.
1457 */
1458 if (copy)
1459 copy_tv(rettv, lp->ll_tv);
1460 else
1461 {
1462 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001463 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001464 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001465 }
1466 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001467}
1468
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001469/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001470 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1471 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001472 * Returns OK or FAIL.
1473 */
1474 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001475tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001476{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001477 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001478 char_u numbuf[NUMBUFLEN];
1479 char_u *s;
1480
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001481 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001482 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001483 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001484 {
1485 switch (tv1->v_type)
1486 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001487 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001488 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001489 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001490 case VAR_DICT:
1491 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001492 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001493 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001494 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001495 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001496 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001497 break;
1498
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001499 case VAR_BLOB:
1500 if (*op != '+' || tv2->v_type != VAR_BLOB)
1501 break;
1502 // BLOB += BLOB
1503 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1504 {
1505 blob_T *b1 = tv1->vval.v_blob;
1506 blob_T *b2 = tv2->vval.v_blob;
1507 int i, len = blob_len(b2);
1508 for (i = 0; i < len; i++)
1509 ga_append(&b1->bv_ga, blob_get(b2, i));
1510 }
1511 return OK;
1512
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001513 case VAR_LIST:
1514 if (*op != '+' || tv2->v_type != VAR_LIST)
1515 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001516 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001517 if (tv2->vval.v_list != NULL)
1518 {
1519 if (tv1->vval.v_list == NULL)
1520 {
1521 tv1->vval.v_list = tv2->vval.v_list;
1522 ++tv1->vval.v_list->lv_refcount;
1523 }
1524 else
1525 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1526 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001527 return OK;
1528
1529 case VAR_NUMBER:
1530 case VAR_STRING:
1531 if (tv2->v_type == VAR_LIST)
1532 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001533 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001534 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001535 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001536 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001537#ifdef FEAT_FLOAT
1538 if (tv2->v_type == VAR_FLOAT)
1539 {
1540 float_T f = n;
1541
Bram Moolenaarff697e62019-02-12 22:28:33 +01001542 if (*op == '%')
1543 break;
1544 switch (*op)
1545 {
1546 case '+': f += tv2->vval.v_float; break;
1547 case '-': f -= tv2->vval.v_float; break;
1548 case '*': f *= tv2->vval.v_float; break;
1549 case '/': f /= tv2->vval.v_float; break;
1550 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001551 clear_tv(tv1);
1552 tv1->v_type = VAR_FLOAT;
1553 tv1->vval.v_float = f;
1554 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001555 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001556#endif
1557 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001558 switch (*op)
1559 {
1560 case '+': n += tv_get_number(tv2); break;
1561 case '-': n -= tv_get_number(tv2); break;
1562 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001563 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1564 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001565 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001566 clear_tv(tv1);
1567 tv1->v_type = VAR_NUMBER;
1568 tv1->vval.v_number = n;
1569 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001570 }
1571 else
1572 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001573 if (tv2->v_type == VAR_FLOAT)
1574 break;
1575
Bram Moolenaarff697e62019-02-12 22:28:33 +01001576 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001577 s = tv_get_string(tv1);
1578 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001579 clear_tv(tv1);
1580 tv1->v_type = VAR_STRING;
1581 tv1->vval.v_string = s;
1582 }
1583 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001584
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001585 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001586#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001587 {
1588 float_T f;
1589
Bram Moolenaarff697e62019-02-12 22:28:33 +01001590 if (*op == '%' || *op == '.'
1591 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001592 && tv2->v_type != VAR_NUMBER
1593 && tv2->v_type != VAR_STRING))
1594 break;
1595 if (tv2->v_type == VAR_FLOAT)
1596 f = tv2->vval.v_float;
1597 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001598 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001599 switch (*op)
1600 {
1601 case '+': tv1->vval.v_float += f; break;
1602 case '-': tv1->vval.v_float -= f; break;
1603 case '*': tv1->vval.v_float *= f; break;
1604 case '/': tv1->vval.v_float /= f; break;
1605 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001606 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001607#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001608 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001609 }
1610 }
1611
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001612 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001613 return FAIL;
1614}
1615
1616/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001617 * Evaluate the expression used in a ":for var in expr" command.
1618 * "arg" points to "var".
1619 * Set "*errp" to TRUE for an error, FALSE otherwise;
1620 * Return a pointer that holds the info. Null when there is an error.
1621 */
1622 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001623eval_for_line(
1624 char_u *arg,
1625 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001626 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001627 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001628{
Bram Moolenaar33570922005-01-25 22:26:29 +00001629 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001630 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001631 typval_T tv;
1632 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001633 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001634
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001635 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001636
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001637 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001638 if (fi == NULL)
1639 return NULL;
1640
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001641 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001642 if (expr == NULL)
1643 return fi;
1644
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001645 expr = skipwhite_and_linebreak(expr, evalarg);
1646 if (expr[0] != 'i' || expr[1] != 'n'
1647 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001648 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001649 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001650 return fi;
1651 }
1652
1653 if (skip)
1654 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001655 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1656 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001657 {
1658 *errp = FALSE;
1659 if (!skip)
1660 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001661 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001662 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001663 l = tv.vval.v_list;
1664 if (l == NULL)
1665 {
1666 // a null list is like an empty list: do nothing
1667 clear_tv(&tv);
1668 }
1669 else
1670 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001671 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001672 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001673
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001674 // No need to increment the refcount, it's already set for
1675 // the list being used in "tv".
1676 fi->fi_list = l;
1677 list_add_watch(l, &fi->fi_lw);
1678 fi->fi_lw.lw_item = l->lv_first;
1679 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001680 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001681 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001682 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001683 fi->fi_bi = 0;
1684 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001685 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001686 typval_T btv;
1687
1688 // Make a copy, so that the iteration still works when the
1689 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001690 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001691 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001692 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001693 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001694 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001695 else
1696 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001697 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001698 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001699 }
1700 }
1701 }
1702 if (skip)
1703 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001704 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001705
1706 return fi;
1707}
1708
1709/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001710 * Used when looping over a :for line, skip the "in expr" part.
1711 */
1712 void
1713skip_for_lines(void *fi_void, evalarg_T *evalarg)
1714{
1715 forinfo_T *fi = (forinfo_T *)fi_void;
1716 int i;
1717
1718 for (i = 0; i < fi->fi_break_count; ++i)
1719 eval_next_line(evalarg);
1720}
1721
1722/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001723 * Use the first item in a ":for" list. Advance to the next.
1724 * Assign the values to the variable (list). "arg" points to the first one.
1725 * Return TRUE when a valid item was found, FALSE when at end of list or
1726 * something wrong.
1727 */
1728 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001729next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001730{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001731 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001732 int result;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001733 int flag = in_vim9script() ? ASSIGN_NO_DECL : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001734 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001735
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001736 if (fi->fi_blob != NULL)
1737 {
1738 typval_T tv;
1739
1740 if (fi->fi_bi >= blob_len(fi->fi_blob))
1741 return FALSE;
1742 tv.v_type = VAR_NUMBER;
1743 tv.v_lock = VAR_FIXED;
1744 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1745 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001746 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001747 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001748 }
1749
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001750 item = fi->fi_lw.lw_item;
1751 if (item == NULL)
1752 result = FALSE;
1753 else
1754 {
1755 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001756 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001757 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001758 }
1759 return result;
1760}
1761
1762/*
1763 * Free the structure used to store info used by ":for".
1764 */
1765 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001766free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001767{
Bram Moolenaar33570922005-01-25 22:26:29 +00001768 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001769
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001770 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001771 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001772 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001773 list_unref(fi->fi_list);
1774 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001775 if (fi != NULL && fi->fi_blob != NULL)
1776 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001777 vim_free(fi);
1778}
1779
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001781set_context_for_expression(
1782 expand_T *xp,
1783 char_u *arg,
1784 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001786 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001788 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001790 if (cmdidx == CMD_let || cmdidx == CMD_var
1791 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001792 {
1793 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001794 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001795 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001796 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001797 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001798 {
1799 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001800 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001801 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001802 break;
1803 }
1804 return;
1805 }
1806 }
1807 else
1808 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1809 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 while ((xp->xp_pattern = vim_strpbrk(arg,
1811 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1812 {
1813 c = *xp->xp_pattern;
1814 if (c == '&')
1815 {
1816 c = xp->xp_pattern[1];
1817 if (c == '&')
1818 {
1819 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001820 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 }
1822 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001823 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001825 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1826 xp->xp_pattern += 2;
1827
1828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 }
1830 else if (c == '$')
1831 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001832 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 xp->xp_context = EXPAND_ENV_VARS;
1834 }
1835 else if (c == '=')
1836 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001837 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 xp->xp_context = EXPAND_EXPRESSION;
1839 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001840 else if (c == '#'
1841 && xp->xp_context == EXPAND_EXPRESSION)
1842 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001843 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001844 break;
1845 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001846 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 && xp->xp_context == EXPAND_FUNCTIONS
1848 && vim_strchr(xp->xp_pattern, '(') == NULL)
1849 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001850 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 break;
1852 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001853 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001855 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 {
1857 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1858 if (c == '\\' && xp->xp_pattern[1] != NUL)
1859 ++xp->xp_pattern;
1860 xp->xp_context = EXPAND_NOTHING;
1861 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001862 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001864 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1866 /* skip */ ;
1867 xp->xp_context = EXPAND_NOTHING;
1868 }
1869 else if (c == '|')
1870 {
1871 if (xp->xp_pattern[1] == '|')
1872 {
1873 ++xp->xp_pattern;
1874 xp->xp_context = EXPAND_EXPRESSION;
1875 }
1876 else
1877 xp->xp_context = EXPAND_COMMANDS;
1878 }
1879 else
1880 xp->xp_context = EXPAND_EXPRESSION;
1881 }
1882 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001883 // Doesn't look like something valid, expand as an expression
1884 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001885 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 arg = xp->xp_pattern;
1887 if (*arg != NUL)
1888 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1889 /* skip */ ;
1890 }
1891 xp->xp_pattern = arg;
1892}
1893
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001895 * Return TRUE if "pat" matches "text".
1896 * Does not use 'cpo' and always uses 'magic'.
1897 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001898 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001899pattern_match(char_u *pat, char_u *text, int ic)
1900{
1901 int matches = FALSE;
1902 char_u *save_cpo;
1903 regmatch_T regmatch;
1904
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001905 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001906 save_cpo = p_cpo;
1907 p_cpo = (char_u *)"";
1908 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1909 if (regmatch.regprog != NULL)
1910 {
1911 regmatch.rm_ic = ic;
1912 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1913 vim_regfree(regmatch.regprog);
1914 }
1915 p_cpo = save_cpo;
1916 return matches;
1917}
1918
1919/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001920 * Handle a name followed by "(". Both for just "name(arg)" and for
1921 * "expr->name(arg)".
1922 * Returns OK or FAIL.
1923 */
1924 static int
1925eval_func(
1926 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02001927 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001928 char_u *name,
1929 int name_len,
1930 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001931 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001932 typval_T *basetv) // "expr" for "expr->name(arg)"
1933{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001934 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001935 char_u *s = name;
1936 int len = name_len;
1937 partial_T *partial;
1938 int ret = OK;
1939
1940 if (!evaluate)
1941 check_vars(s, len);
1942
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001943 // If "s" is the name of a variable of type VAR_FUNC
1944 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001945 s = deref_func_name(s, &len, &partial, !evaluate);
1946
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001947 // Need to make a copy, in case evaluating the arguments makes
1948 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001949 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001950 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001951 ret = FAIL;
1952 else
1953 {
1954 funcexe_T funcexe;
1955
1956 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001957 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001958 funcexe.firstline = curwin->w_cursor.lnum;
1959 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001960 funcexe.evaluate = evaluate;
1961 funcexe.partial = partial;
1962 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02001963 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001964 }
1965 vim_free(s);
1966
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001967 // If evaluate is FALSE rettv->v_type was not set in
1968 // get_func_tv, but it's needed in handle_subscript() to parse
1969 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001970 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
1971 {
1972 rettv->vval.v_string = NULL;
1973 rettv->v_type = VAR_FUNC;
1974 }
1975
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001976 // Stop the expression evaluation when immediately
1977 // aborting on error, or when an interrupt occurred or
1978 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001979 if (evaluate && aborting())
1980 {
1981 if (ret == OK)
1982 clear_tv(rettv);
1983 ret = FAIL;
1984 }
1985 return ret;
1986}
1987
1988/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02001989 * Get the next line source line without advancing. But do skip over comment
1990 * lines.
1991 */
1992 static char_u *
1993getline_peek_skip_comments(evalarg_T *evalarg)
1994{
1995 for (;;)
1996 {
1997 char_u *next = getline_peek(evalarg->eval_getline,
1998 evalarg->eval_cookie);
1999 char_u *p;
2000
2001 if (next == NULL)
2002 break;
2003 p = skipwhite(next);
2004 if (*p != NUL && !vim9_comment_start(p))
2005 return next;
2006 (void)eval_next_line(evalarg);
2007 }
2008 return NULL;
2009}
2010
2011/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002012 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2013 * comment) and there is a next line, return the next line (skipping blanks)
2014 * and set "getnext".
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002015 * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
2016 * "arg" must point somewhere inside a line, not at the start.
2017 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002018 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002019eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2020{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002021 char_u *p = skipwhite(arg);
2022
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002023 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002024 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002025 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002026 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaar9d489562020-07-30 20:08:50 +02002027 && (*p == NUL || (VIM_ISWHITE(p[-1]) && vim9_comment_start(p))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002028 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002029 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002030
2031 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002032 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002033 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002034 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002035
Bram Moolenaar9d489562020-07-30 20:08:50 +02002036 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002037 {
2038 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002039 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002040 }
2041 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002042 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002043}
2044
2045/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002046 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002047 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002048 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002049eval_next_line(evalarg_T *evalarg)
2050{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002051 garray_T *gap = &evalarg->eval_ga;
2052 char_u *line;
2053
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002054 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002055 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2056 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002057 else
2058 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002059 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002060 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2061 {
2062 // Going to concatenate the lines after parsing.
2063 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2064 ++gap->ga_len;
2065 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002066 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002067 {
2068 vim_free(evalarg->eval_tofree);
2069 evalarg->eval_tofree = line;
2070 }
2071 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002072}
2073
Bram Moolenaare6b53242020-07-01 17:28:33 +02002074/*
2075 * Call eval_next_non_blank() and get the next line if needed.
2076 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002077 char_u *
2078skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2079{
2080 int getnext;
2081 char_u *p = skipwhite(arg);
2082
Bram Moolenaar962d7212020-07-04 14:15:00 +02002083 if (evalarg == NULL)
2084 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002085 eval_next_non_blank(p, evalarg, &getnext);
2086 if (getnext)
2087 return eval_next_line(evalarg);
2088 return p;
2089}
2090
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002091/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002092 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002093 */
2094 void
2095clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2096{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002097 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002098 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002099 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002100 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002101 if (eap != NULL)
2102 {
2103 // We may need to keep the original command line, e.g. for
2104 // ":let" it has the variable names. But we may also need the
2105 // new one, "nextcmd" points into it. Keep both.
2106 vim_free(eap->cmdline_tofree);
2107 eap->cmdline_tofree = *eap->cmdlinep;
2108 *eap->cmdlinep = evalarg->eval_tofree;
2109 }
2110 else
2111 vim_free(evalarg->eval_tofree);
2112 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002113 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002114
2115 vim_free(evalarg->eval_tofree_lambda);
2116 evalarg->eval_tofree_lambda = NULL;
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002117 }
2118}
2119
2120/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002122 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2124 */
2125
2126/*
2127 * Handle zero level expression.
2128 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002129 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002130 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002131 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 * Return OK or FAIL.
2133 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002134 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002135eval0(
2136 char_u *arg,
2137 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002138 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002139 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140{
2141 int ret;
2142 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002143 int did_emsg_before = did_emsg;
2144 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002145 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146
2147 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002148 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002149 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002150
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002151 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 {
2153 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002154 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 /*
2156 * Report the invalid expression unless the expression evaluation has
2157 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002158 * exception, or we already gave a more specific error.
2159 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002161 if (!aborting()
2162 && did_emsg == did_emsg_before
2163 && called_emsg == called_emsg_before
2164 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002165 semsg(_(e_invexpr2), arg);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002166
2167 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002168 // a next command to avoid more errors, unless "|" is following, which
2169 // could only be a command separator.
2170 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2171 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002172 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002174
2175 if (eap != NULL)
2176 eap->nextcmd = check_nextcmd(p);
2177
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 return ret;
2179}
2180
2181/*
2182 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002183 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002184 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 *
2186 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002187 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002189 * Note: "rettv.v_lock" is not set.
2190 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 * Return OK or FAIL.
2192 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002193 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002194eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002196 char_u *p;
2197 int getnext;
2198
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002199 CLEAR_POINTER(rettv);
2200
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 /*
2202 * Get the first variable.
2203 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002204 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 return FAIL;
2206
Bram Moolenaar793648f2020-06-26 21:28:25 +02002207 p = eval_next_non_blank(*arg, evalarg, &getnext);
2208 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002210 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002211 int result;
2212 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002213 evalarg_T *evalarg_used = evalarg;
2214 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002215 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002216 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002217 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002218
2219 if (evalarg == NULL)
2220 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002221 CLEAR_FIELD(local_evalarg);
2222 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002223 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002224 orig_flags = evalarg_used->eval_flags;
2225 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002226
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002227 if (getnext)
2228 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002229 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002230 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002231 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002232 {
2233 error_white_both(p, 1);
2234 clear_tv(rettv);
2235 return FAIL;
2236 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002237 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002238 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002239
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002241 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002243 int error = FALSE;
2244
Bram Moolenaar13106602020-10-04 16:06:05 +02002245 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002246 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002247 else if (vim9script)
2248 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002249 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002251 if (error || !op_falsy || !result)
2252 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002253 if (error)
2254 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 }
2256
2257 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002258 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002260 if (op_falsy)
2261 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002262 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002263 {
2264 error_white_both(p, 1);
2265 clear_tv(rettv);
2266 return FAIL;
2267 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002268 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002269 evalarg_used->eval_flags = (op_falsy ? !result : result)
2270 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2271 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002272 {
2273 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002275 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002276 if (!op_falsy || !result)
2277 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002279 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002281 /*
2282 * Check for the ":".
2283 */
2284 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2285 if (*p != ':')
2286 {
2287 emsg(_(e_missing_colon));
2288 if (evaluate && result)
2289 clear_tv(rettv);
2290 evalarg_used->eval_flags = orig_flags;
2291 return FAIL;
2292 }
2293 if (getnext)
2294 *arg = eval_next_line(evalarg_used);
2295 else
2296 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002297 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002298 {
2299 error_white_both(p, 1);
2300 clear_tv(rettv);
2301 evalarg_used->eval_flags = orig_flags;
2302 return FAIL;
2303 }
2304 *arg = p;
2305 }
2306
2307 /*
2308 * Get the third variable. Recursive!
2309 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002310 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002311 {
2312 error_white_both(p, 1);
2313 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002314 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002315 return FAIL;
2316 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002317 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2318 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002319 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002320 if (eval1(arg, &var2, evalarg_used) == FAIL)
2321 {
2322 if (evaluate && result)
2323 clear_tv(rettv);
2324 evalarg_used->eval_flags = orig_flags;
2325 return FAIL;
2326 }
2327 if (evaluate && !result)
2328 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002330
2331 if (evalarg == NULL)
2332 clear_evalarg(&local_evalarg, NULL);
2333 else
2334 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 }
2336
2337 return OK;
2338}
2339
2340/*
2341 * Handle first level expression:
2342 * expr2 || expr2 || expr2 logical OR
2343 *
2344 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002345 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 *
2347 * Return OK or FAIL.
2348 */
2349 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002350eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002352 char_u *p;
2353 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354
2355 /*
2356 * Get the first variable.
2357 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002358 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 return FAIL;
2360
2361 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002362 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002364 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002365 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002367 evalarg_T *evalarg_used = evalarg;
2368 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002369 int evaluate;
2370 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002371 long result = FALSE;
2372 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002373 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002374 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002375
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002376 if (evalarg == NULL)
2377 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002378 CLEAR_FIELD(local_evalarg);
2379 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002380 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002381 orig_flags = evalarg_used->eval_flags;
2382 evaluate = orig_flags & EVAL_EVALUATE;
2383 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002384 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002385 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002386 result = tv_get_bool_chk(rettv, &error);
2387 else if (tv_get_number_chk(rettv, &error) != 0)
2388 result = TRUE;
2389 clear_tv(rettv);
2390 if (error)
2391 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 }
2393
2394 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002395 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002397 while (p[0] == '|' && p[1] == '|')
2398 {
2399 if (getnext)
2400 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002401 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002402 {
2403 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2404 {
2405 error_white_both(p, 2);
2406 clear_tv(rettv);
2407 return FAIL;
2408 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002409 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002410 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002411
2412 /*
2413 * Get the second variable.
2414 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002415 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2416 {
2417 error_white_both(p, 2);
2418 clear_tv(rettv);
2419 return FAIL;
2420 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002421 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2422 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002423 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002424 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002425 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002426
2427 /*
2428 * Compute the result.
2429 */
2430 if (evaluate && !result)
2431 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002432 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002433 result = tv_get_bool_chk(&var2, &error);
2434 else if (tv_get_number_chk(&var2, &error) != 0)
2435 result = TRUE;
2436 clear_tv(&var2);
2437 if (error)
2438 return FAIL;
2439 }
2440 if (evaluate)
2441 {
2442 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002443 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002444 rettv->v_type = VAR_BOOL;
2445 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002446 }
2447 else
2448 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002449 rettv->v_type = VAR_NUMBER;
2450 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002451 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002452 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002453
2454 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002456
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002457 if (evalarg == NULL)
2458 clear_evalarg(&local_evalarg, NULL);
2459 else
2460 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 }
2462
2463 return OK;
2464}
2465
2466/*
2467 * Handle second level expression:
2468 * expr3 && expr3 && expr3 logical AND
2469 *
2470 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002471 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 *
2473 * Return OK or FAIL.
2474 */
2475 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002476eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002478 char_u *p;
2479 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480
2481 /*
2482 * Get the first variable.
2483 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002484 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 return FAIL;
2486
2487 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002488 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002490 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002491 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002493 evalarg_T *evalarg_used = evalarg;
2494 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002495 int orig_flags;
2496 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002497 long result = TRUE;
2498 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002499 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002500 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002501
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002502 if (evalarg == NULL)
2503 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002504 CLEAR_FIELD(local_evalarg);
2505 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002506 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002507 orig_flags = evalarg_used->eval_flags;
2508 evaluate = orig_flags & EVAL_EVALUATE;
2509 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002510 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002511 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002512 result = tv_get_bool_chk(rettv, &error);
2513 else if (tv_get_number_chk(rettv, &error) == 0)
2514 result = FALSE;
2515 clear_tv(rettv);
2516 if (error)
2517 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 }
2519
2520 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002521 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002523 while (p[0] == '&' && p[1] == '&')
2524 {
2525 if (getnext)
2526 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002527 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002528 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002529 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002530 {
2531 error_white_both(p, 2);
2532 clear_tv(rettv);
2533 return FAIL;
2534 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002535 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002536 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002537
2538 /*
2539 * Get the second variable.
2540 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002541 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2542 {
2543 error_white_both(p, 2);
2544 clear_tv(rettv);
2545 return FAIL;
2546 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002547 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2548 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002549 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002550 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002551 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002552 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002553
2554 /*
2555 * Compute the result.
2556 */
2557 if (evaluate && result)
2558 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002559 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002560 result = tv_get_bool_chk(&var2, &error);
2561 else if (tv_get_number_chk(&var2, &error) == 0)
2562 result = FALSE;
2563 clear_tv(&var2);
2564 if (error)
2565 return FAIL;
2566 }
2567 if (evaluate)
2568 {
2569 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002570 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002571 rettv->v_type = VAR_BOOL;
2572 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002573 }
2574 else
2575 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002576 rettv->v_type = VAR_NUMBER;
2577 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002578 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002579 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002580
2581 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002583
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002584 if (evalarg == NULL)
2585 clear_evalarg(&local_evalarg, NULL);
2586 else
2587 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 }
2589
2590 return OK;
2591}
2592
2593/*
2594 * Handle third level expression:
2595 * var1 == var2
2596 * var1 =~ var2
2597 * var1 != var2
2598 * var1 !~ var2
2599 * var1 > var2
2600 * var1 >= var2
2601 * var1 < var2
2602 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002603 * var1 is var2
2604 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 *
2606 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002607 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 *
2609 * Return OK or FAIL.
2610 */
2611 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002612eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002615 int getnext;
Bram Moolenaar87396072019-12-31 22:36:18 +01002616 exptype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002618 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619
2620 /*
2621 * Get the first variable.
2622 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002623 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 return FAIL;
2625
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002626 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002627 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628
2629 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002630 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002632 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002634 typval_T var2;
2635 int ic;
2636 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002637 int evaluate = evalarg == NULL
2638 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002639
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002640 if (getnext)
2641 *arg = eval_next_line(evalarg);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002642 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2643 {
2644 error_white_both(p, len);
2645 clear_tv(rettv);
2646 return FAIL;
2647 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002648
Bram Moolenaar696ba232020-07-29 21:20:41 +02002649 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2650 {
2651 semsg(_(e_invexpr2), p);
2652 clear_tv(rettv);
2653 return FAIL;
2654 }
2655
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002656 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 if (p[len] == '?')
2658 {
2659 ic = TRUE;
2660 ++len;
2661 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002662 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 else if (p[len] == '#')
2664 {
2665 ic = FALSE;
2666 ++len;
2667 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002668 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002670 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671
2672 /*
2673 * Get the second variable.
2674 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002675 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2676 {
2677 error_white_both(p, 1);
2678 clear_tv(rettv);
2679 return FAIL;
2680 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002681 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002682 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002684 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 return FAIL;
2686 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002687 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002688 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002689 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002690
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002691 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002692 {
2693 ret = FAIL;
2694 clear_tv(rettv);
2695 }
2696 else
2697 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002698 clear_tv(&var2);
2699 return ret;
2700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 }
2702
2703 return OK;
2704}
2705
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002706/*
2707 * Make a copy of blob "tv1" and append blob "tv2".
2708 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002709 void
2710eval_addblob(typval_T *tv1, typval_T *tv2)
2711{
2712 blob_T *b1 = tv1->vval.v_blob;
2713 blob_T *b2 = tv2->vval.v_blob;
2714 blob_T *b = blob_alloc();
2715 int i;
2716
2717 if (b != NULL)
2718 {
2719 for (i = 0; i < blob_len(b1); i++)
2720 ga_append(&b->bv_ga, blob_get(b1, i));
2721 for (i = 0; i < blob_len(b2); i++)
2722 ga_append(&b->bv_ga, blob_get(b2, i));
2723
2724 clear_tv(tv1);
2725 rettv_blob_set(tv1, b);
2726 }
2727}
2728
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002729/*
2730 * Make a copy of list "tv1" and append list "tv2".
2731 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002732 int
2733eval_addlist(typval_T *tv1, typval_T *tv2)
2734{
2735 typval_T var3;
2736
2737 // concatenate Lists
2738 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2739 {
2740 clear_tv(tv1);
2741 clear_tv(tv2);
2742 return FAIL;
2743 }
2744 clear_tv(tv1);
2745 *tv1 = var3;
2746 return OK;
2747}
2748
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749/*
2750 * Handle fourth level expression:
2751 * + number addition
2752 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002753 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002754 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 *
2756 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002757 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 *
2759 * Return OK or FAIL.
2760 */
2761 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002762eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 /*
2765 * Get the first variable.
2766 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002767 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 return FAIL;
2769
2770 /*
2771 * Repeat computing, until no '+', '-' or '.' is following.
2772 */
2773 for (;;)
2774 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002775 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002776 int getnext;
2777 char_u *p;
2778 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002779 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002780 int concat;
2781 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002782 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002783
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002784 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarce2c5442020-11-28 21:21:17 +01002785 // "+=" and "-=" are assignment
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002786 p = eval_next_non_blank(*arg, evalarg, &getnext);
2787 op = *p;
2788 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaarce2c5442020-11-28 21:21:17 +01002789 if ((op != '+' && op != '-' && !concat) || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002791
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002792 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002793 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002794 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002795 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002796 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002797 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002798 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002799 {
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002800 error_white_both(p, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002801 clear_tv(rettv);
2802 return FAIL;
2803 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002804 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002805 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002806 if ((op != '+' || (rettv->v_type != VAR_LIST
2807 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002808#ifdef FEAT_FLOAT
2809 && (op == '.' || rettv->v_type != VAR_FLOAT)
2810#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002811 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002812 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002813 int error = FALSE;
2814
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002815 // For "list + ...", an illegal use of the first operand as
2816 // a number cannot be determined before evaluating the 2nd
2817 // operand: if this is also a list, all is ok.
2818 // For "something . ...", "something - ..." or "non-list + ...",
2819 // we know that the first operand needs to be a string or number
2820 // without evaluating the 2nd operand. So check before to avoid
2821 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002822 if (op != '.')
2823 tv_get_number_chk(rettv, &error);
2824 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002825 {
2826 clear_tv(rettv);
2827 return FAIL;
2828 }
2829 }
2830
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 /*
2832 * Get the second variable.
2833 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002834 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002835 {
2836 error_white_both(p, oplen);
2837 clear_tv(rettv);
2838 return FAIL;
2839 }
2840 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002841 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002843 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 return FAIL;
2845 }
2846
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002847 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 {
2849 /*
2850 * Compute the result.
2851 */
2852 if (op == '.')
2853 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002854 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2855 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002856 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002857
Bram Moolenaar2e086612020-08-25 22:37:48 +02002858 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002859 || var2.v_type == VAR_CHANNEL
2860 || var2.v_type == VAR_JOB))
2861 emsg(_(e_inval_string));
2862#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02002863 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002864 {
2865 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2866 var2.vval.v_float);
2867 s2 = buf2;
2868 }
2869#endif
2870 else
2871 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002872 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002873 {
2874 clear_tv(rettv);
2875 clear_tv(&var2);
2876 return FAIL;
2877 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002878 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002879 clear_tv(rettv);
2880 rettv->v_type = VAR_STRING;
2881 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002883 else if (op == '+' && rettv->v_type == VAR_BLOB
2884 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002885 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002886 else if (op == '+' && rettv->v_type == VAR_LIST
2887 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002888 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002889 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002890 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 else
2893 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002894 int error = FALSE;
2895 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002896#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002897 float_T f1 = 0, f2 = 0;
2898
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002899 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002900 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002901 f1 = rettv->vval.v_float;
2902 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002903 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002904 else
2905#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002906 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002907 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002908 if (error)
2909 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002910 // This can only happen for "list + non-list". For
2911 // "non-list + ..." or "something - ...", we returned
2912 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002913 clear_tv(rettv);
2914 return FAIL;
2915 }
2916#ifdef FEAT_FLOAT
2917 if (var2.v_type == VAR_FLOAT)
2918 f1 = n1;
2919#endif
2920 }
2921#ifdef FEAT_FLOAT
2922 if (var2.v_type == VAR_FLOAT)
2923 {
2924 f2 = var2.vval.v_float;
2925 n2 = 0;
2926 }
2927 else
2928#endif
2929 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002930 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002931 if (error)
2932 {
2933 clear_tv(rettv);
2934 clear_tv(&var2);
2935 return FAIL;
2936 }
2937#ifdef FEAT_FLOAT
2938 if (rettv->v_type == VAR_FLOAT)
2939 f2 = n2;
2940#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002941 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002942 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002943
2944#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002945 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002946 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2947 {
2948 if (op == '+')
2949 f1 = f1 + f2;
2950 else
2951 f1 = f1 - f2;
2952 rettv->v_type = VAR_FLOAT;
2953 rettv->vval.v_float = f1;
2954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002956#endif
2957 {
2958 if (op == '+')
2959 n1 = n1 + n2;
2960 else
2961 n1 = n1 - n2;
2962 rettv->v_type = VAR_NUMBER;
2963 rettv->vval.v_number = n1;
2964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002966 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 }
2968 }
2969 return OK;
2970}
2971
2972/*
2973 * Handle fifth level expression:
2974 * * number multiplication
2975 * / number division
2976 * % number modulo
2977 *
2978 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002979 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 *
2981 * Return OK or FAIL.
2982 */
2983 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002984eval6(
2985 char_u **arg,
2986 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002987 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002988 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002990#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002991 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002992#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993
2994 /*
2995 * Get the first variable.
2996 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002997 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 return FAIL;
2999
3000 /*
3001 * Repeat computing, until no '*', '/' or '%' is following.
3002 */
3003 for (;;)
3004 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003005 int evaluate;
3006 int getnext;
3007 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003008 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003009 int op;
3010 varnumber_T n1, n2;
3011#ifdef FEAT_FLOAT
3012 float_T f1, f2;
3013#endif
3014 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003015
Bram Moolenaar9d489562020-07-30 20:08:50 +02003016 p = eval_next_non_blank(*arg, evalarg, &getnext);
3017 op = *p;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02003018 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003020
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003021 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003022 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003023 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003024 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003025 {
3026 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3027 {
3028 error_white_both(p, 1);
3029 clear_tv(rettv);
3030 return FAIL;
3031 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003032 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003035#ifdef FEAT_FLOAT
3036 f1 = 0;
3037 f2 = 0;
3038#endif
3039 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003040 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003042#ifdef FEAT_FLOAT
3043 if (rettv->v_type == VAR_FLOAT)
3044 {
3045 f1 = rettv->vval.v_float;
3046 use_float = TRUE;
3047 n1 = 0;
3048 }
3049 else
3050#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003051 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003052 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003053 if (error)
3054 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 }
3056 else
3057 n1 = 0;
3058
3059 /*
3060 * Get the second variable.
3061 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003062 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3063 {
3064 error_white_both(p, 1);
3065 clear_tv(rettv);
3066 return FAIL;
3067 }
3068 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003069 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 return FAIL;
3071
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003072 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003074#ifdef FEAT_FLOAT
3075 if (var2.v_type == VAR_FLOAT)
3076 {
3077 if (!use_float)
3078 {
3079 f1 = n1;
3080 use_float = TRUE;
3081 }
3082 f2 = var2.vval.v_float;
3083 n2 = 0;
3084 }
3085 else
3086#endif
3087 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003088 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003089 clear_tv(&var2);
3090 if (error)
3091 return FAIL;
3092#ifdef FEAT_FLOAT
3093 if (use_float)
3094 f2 = n2;
3095#endif
3096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097
3098 /*
3099 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003100 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003102#ifdef FEAT_FLOAT
3103 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003105 if (op == '*')
3106 f1 = f1 * f2;
3107 else if (op == '/')
3108 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003109# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003110 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003111 if (f2 == 0.0)
3112 {
3113 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003114 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003115 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003116 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003117 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003118 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003119 }
3120 else
3121 f1 = f1 / f2;
3122# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003123 // We rely on the floating point library to handle divide
3124 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003125 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003126# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003129 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003130 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003131 return FAIL;
3132 }
3133 rettv->v_type = VAR_FLOAT;
3134 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 }
3136 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003139 if (op == '*')
3140 n1 = n1 * n2;
3141 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01003142 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01003144 n1 = num_modulus(n1, n2);
3145
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003146 rettv->v_type = VAR_NUMBER;
3147 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 }
3150 }
3151
3152 return OK;
3153}
3154
3155/*
3156 * Handle sixth level expression:
3157 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003158 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003159 * "string" string constant
3160 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 * &option-name option value
3162 * @r register contents
3163 * identifier variable value
3164 * function() function call
3165 * $VAR environment variable
3166 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003167 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003168 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003169 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003170 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 *
3172 * Also handle:
3173 * ! in front logical NOT
3174 * - in front unary minus
3175 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003176 * trailing [] subscript in String or List
3177 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003178 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 *
3180 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003181 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 *
3183 * Return OK or FAIL.
3184 */
3185 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003186eval7(
3187 char_u **arg,
3188 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003189 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003190 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003192 int evaluate = evalarg != NULL
3193 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 int len;
3195 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 char_u *start_leader, *end_leader;
3197 int ret = OK;
3198 char_u *alias;
3199
3200 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003201 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003202 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003204 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205
3206 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003207 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 */
3209 start_leader = *arg;
3210 while (**arg == '!' || **arg == '-' || **arg == '+')
3211 *arg = skipwhite(*arg + 1);
3212 end_leader = *arg;
3213
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003214 if (**arg == '.' && (!isdigit(*(*arg + 1))
3215#ifdef FEAT_FLOAT
3216 || current_sctx.sc_version < 2
3217#endif
3218 ))
3219 {
3220 semsg(_(e_invexpr2), *arg);
3221 ++*arg;
3222 return FAIL;
3223 }
3224
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 switch (**arg)
3226 {
3227 /*
3228 * Number constant.
3229 */
3230 case '0':
3231 case '1':
3232 case '2':
3233 case '3':
3234 case '4':
3235 case '5':
3236 case '6':
3237 case '7':
3238 case '8':
3239 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003240 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003241
3242 // Apply prefixed "-" and "+" now. Matters especially when
3243 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003244 if (ret == OK && evaluate && end_leader > start_leader
3245 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003246 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 break;
3248
3249 /*
3250 * String constant: "string".
3251 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003252 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 break;
3254
3255 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003256 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003258 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003259 break;
3260
3261 /*
3262 * List: [expr, expr]
3263 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003264 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 break;
3266
3267 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003268 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003269 */
Bram Moolenaare0de1712020-12-02 17:36:54 +01003270 case '#': if (!in_vim9script() && (*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003271 {
3272 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003273 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003274 }
3275 else
3276 ret = NOTDONE;
3277 break;
3278
3279 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003280 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003281 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003282 */
Bram Moolenaarc2ca9352020-11-25 21:30:11 +01003283 case '{': ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003284 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003285 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003286 break;
3287
3288 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003289 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003291 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 break;
3293
3294 /*
3295 * Environment variable: $VAR.
3296 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003297 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 break;
3299
3300 /*
3301 * Register contents: @r.
3302 */
3303 case '@': ++*arg;
3304 if (evaluate)
3305 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003306 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02003307 rettv->vval.v_string = get_reg_contents(**arg,
3308 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 }
3310 if (**arg != NUL)
3311 ++*arg;
3312 break;
3313
3314 /*
3315 * nested expression: (expression).
3316 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003317 case '(': {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003318 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003319 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003320
Bram Moolenaar9215f012020-06-27 21:18:00 +02003321 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003322 if (**arg == ')')
3323 ++*arg;
3324 else if (ret == OK)
3325 {
3326 emsg(_(e_missing_close));
3327 clear_tv(rettv);
3328 ret = FAIL;
3329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 }
3331 break;
3332
Bram Moolenaar8c711452005-01-14 21:53:12 +00003333 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 break;
3335 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003336
3337 if (ret == NOTDONE)
3338 {
3339 /*
3340 * Must be a variable or function name.
3341 * Can also be a curly-braces kind of name: {expr}.
3342 */
3343 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003344 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003345 if (alias != NULL)
3346 s = alias;
3347
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003348 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003349 ret = FAIL;
3350 else
3351 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003352 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3353
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003354 if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
3355 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003356 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003357 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003358 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003359 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003360 else if (flags & EVAL_CONSTANT)
3361 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003362 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003363 {
3364 // get the value of "true", "false" or a variable
3365 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3366 {
3367 rettv->v_type = VAR_BOOL;
3368 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003369 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003370 }
3371 else if (len == 5 && in_vim9script()
3372 && STRNCMP(s, "false", 4) == 0)
3373 {
3374 rettv->v_type = VAR_BOOL;
3375 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003376 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003377 }
3378 else
3379 ret = eval_variable(s, len, rettv, NULL, TRUE, FALSE);
3380 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003381 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003382 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003383 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003384 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003385 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003386 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003387 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003388 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003389 }
3390
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003391 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3392 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003393 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003394 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395
3396 /*
3397 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3398 */
3399 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003400 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003401 return ret;
3402}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003403
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003404/*
3405 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003406 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003407 * Adjusts "end_leaderp" until it is at "start_leader".
3408 */
3409 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003410eval7_leader(
3411 typval_T *rettv,
3412 int numeric_only,
3413 char_u *start_leader,
3414 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003415{
3416 char_u *end_leader = *end_leaderp;
3417 int ret = OK;
3418 int error = FALSE;
3419 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003420 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003421#ifdef FEAT_FLOAT
3422 float_T f = 0.0;
3423
3424 if (rettv->v_type == VAR_FLOAT)
3425 f = rettv->vval.v_float;
3426 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003427#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003428 {
3429 while (VIM_ISWHITE(end_leader[-1]))
3430 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003431 if (in_vim9script() && end_leader[-1] == '!')
3432 val = tv2bool(rettv);
3433 else
3434 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003435 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003436 if (error)
3437 {
3438 clear_tv(rettv);
3439 ret = FAIL;
3440 }
3441 else
3442 {
3443 while (end_leader > start_leader)
3444 {
3445 --end_leader;
3446 if (*end_leader == '!')
3447 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003448 if (numeric_only)
3449 {
3450 ++end_leader;
3451 break;
3452 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003453#ifdef FEAT_FLOAT
3454 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003455 {
3456 if (in_vim9script())
3457 {
3458 rettv->v_type = VAR_BOOL;
3459 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3460 }
3461 else
3462 f = !f;
3463 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003464 else
3465#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003466 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003467 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003468 type = VAR_BOOL;
3469 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003470 }
3471 else if (*end_leader == '-')
3472 {
3473#ifdef FEAT_FLOAT
3474 if (rettv->v_type == VAR_FLOAT)
3475 f = -f;
3476 else
3477#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003478 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003479 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003480 type = VAR_NUMBER;
3481 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003482 }
3483 }
3484#ifdef FEAT_FLOAT
3485 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003487 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003488 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003490 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003491#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003492 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003493 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003494 if (in_vim9script())
3495 rettv->v_type = type;
3496 else
3497 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003498 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003501 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 return ret;
3503}
3504
3505/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003506 * Call the function referred to in "rettv".
3507 */
3508 static int
3509call_func_rettv(
3510 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003511 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003512 typval_T *rettv,
3513 int evaluate,
3514 dict_T *selfdict,
3515 typval_T *basetv)
3516{
3517 partial_T *pt = NULL;
3518 funcexe_T funcexe;
3519 typval_T functv;
3520 char_u *s;
3521 int ret;
3522
3523 // need to copy the funcref so that we can clear rettv
3524 if (evaluate)
3525 {
3526 functv = *rettv;
3527 rettv->v_type = VAR_UNKNOWN;
3528
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003529 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003530 if (functv.v_type == VAR_PARTIAL)
3531 {
3532 pt = functv.vval.v_partial;
3533 s = partial_name(pt);
3534 }
3535 else
3536 s = functv.vval.v_string;
3537 }
3538 else
3539 s = (char_u *)"";
3540
Bram Moolenaara80faa82020-04-12 19:37:17 +02003541 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003542 funcexe.firstline = curwin->w_cursor.lnum;
3543 funcexe.lastline = curwin->w_cursor.lnum;
3544 funcexe.evaluate = evaluate;
3545 funcexe.partial = pt;
3546 funcexe.selfdict = selfdict;
3547 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003548 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003549
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003550 // Clear the funcref afterwards, so that deleting it while
3551 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003552 if (evaluate)
3553 clear_tv(&functv);
3554
3555 return ret;
3556}
3557
3558/*
3559 * Evaluate "->method()".
3560 * "*arg" points to the '-'.
3561 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3562 */
3563 static int
3564eval_lambda(
3565 char_u **arg,
3566 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003567 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003568 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003569{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003570 int evaluate = evalarg != NULL
3571 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003572 typval_T base = *rettv;
3573 int ret;
3574
3575 // Skip over the ->.
3576 *arg += 2;
3577 rettv->v_type = VAR_UNKNOWN;
3578
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003579 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003580 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003581 return FAIL;
3582 else if (**arg != '(')
3583 {
3584 if (verbose)
3585 {
3586 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003587 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003588 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003589 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003590 }
3591 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003592 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003593 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003594 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003595 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003596
3597 // Clear the funcref afterwards, so that deleting it while
3598 // evaluating the arguments is possible (see test55).
3599 if (evaluate)
3600 clear_tv(&base);
3601
3602 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003603}
3604
3605/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003606 * Evaluate "->method()".
3607 * "*arg" points to the '-'.
3608 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3609 */
3610 static int
3611eval_method(
3612 char_u **arg,
3613 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003614 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003615 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003616{
3617 char_u *name;
3618 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003619 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003620 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003621 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003622 int evaluate = evalarg != NULL
3623 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003624
3625 // Skip over the ->.
3626 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003627 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003628
Bram Moolenaarac92e252019-08-03 21:58:38 +02003629 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003630 len = get_name_len(arg, &alias, evaluate, TRUE);
3631 if (alias != NULL)
3632 name = alias;
3633
3634 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003635 {
3636 if (verbose)
3637 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003638 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003639 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003640 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003641 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003642 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003643 if (**arg != '(')
3644 {
3645 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003646 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003647 ret = FAIL;
3648 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003649 else if (VIM_ISWHITE((*arg)[-1]))
3650 {
3651 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003652 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003653 ret = FAIL;
3654 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003655 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003656 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003657 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003658 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003659
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003660 // Clear the funcref afterwards, so that deleting it while
3661 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003662 if (evaluate)
3663 clear_tv(&base);
3664
Bram Moolenaarac92e252019-08-03 21:58:38 +02003665 return ret;
3666}
3667
3668/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003669 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3670 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003671 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3672 */
3673 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003674eval_index(
3675 char_u **arg,
3676 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003677 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003678 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003679{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003680 int evaluate = evalarg != NULL
3681 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003682 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003683 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003684 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003685 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003686 int keylen = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003687
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003688 if (check_can_index(rettv, evaluate, verbose) == FAIL)
3689 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003690
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003691 init_tv(&var1);
3692 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003693 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003694 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003695 /*
3696 * dict.name
3697 */
3698 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003699 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003700 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003701 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003702 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02003703 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003704 }
3705 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003706 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003707 /*
3708 * something[idx]
3709 *
3710 * Get the (first) variable from inside the [].
3711 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003712 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003713 if (**arg == ':')
3714 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003715 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003716 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003717 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003718 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003719 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003720 clear_tv(&var1);
3721 return FAIL;
3722 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003723
3724 /*
3725 * Get the second variable from inside the [:].
3726 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003727 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003728 if (**arg == ':')
3729 {
3730 range = TRUE;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003731 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003732 if (**arg == ']')
3733 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003734 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003735 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003736 if (!empty1)
3737 clear_tv(&var1);
3738 return FAIL;
3739 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003740 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003741 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003742 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003743 if (!empty1)
3744 clear_tv(&var1);
3745 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003746 return FAIL;
3747 }
3748 }
3749
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003750 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003751 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003752 if (**arg != ']')
3753 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003754 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003755 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003756 clear_tv(&var1);
3757 if (range)
3758 clear_tv(&var2);
3759 return FAIL;
3760 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02003761 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003762 }
3763
3764 if (evaluate)
3765 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003766 int res = eval_index_inner(rettv, range,
3767 empty1 ? NULL : &var1, empty2 ? NULL : &var2,
3768 key, keylen, verbose);
3769 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003770 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003771 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003772 clear_tv(&var2);
3773 return res;
3774 }
3775 return OK;
3776}
3777
3778/*
3779 * Check if "rettv" can have an [index] or [sli:ce]
3780 */
3781 int
3782check_can_index(typval_T *rettv, int evaluate, int verbose)
3783{
3784 switch (rettv->v_type)
3785 {
3786 case VAR_FUNC:
3787 case VAR_PARTIAL:
3788 if (verbose)
3789 emsg(_("E695: Cannot index a Funcref"));
3790 return FAIL;
3791 case VAR_FLOAT:
3792#ifdef FEAT_FLOAT
3793 if (verbose)
3794 emsg(_(e_float_as_string));
3795 return FAIL;
3796#endif
3797 case VAR_BOOL:
3798 case VAR_SPECIAL:
3799 case VAR_JOB:
3800 case VAR_CHANNEL:
3801 if (verbose)
3802 emsg(_(e_cannot_index_special_variable));
3803 return FAIL;
3804 case VAR_UNKNOWN:
3805 case VAR_ANY:
3806 case VAR_VOID:
3807 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003808 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003809 emsg(_(e_cannot_index_special_variable));
3810 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003811 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003812 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003813
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003814 case VAR_STRING:
3815 case VAR_LIST:
3816 case VAR_DICT:
3817 case VAR_BLOB:
3818 break;
3819 case VAR_NUMBER:
3820 if (in_vim9script())
3821 emsg(_(e_cannot_index_number));
3822 break;
3823 }
3824 return OK;
3825}
3826
3827/*
3828 * Apply index or range to "rettv".
3829 * "var1" is the first index, NULL for [:expr].
3830 * "var2" is the second index, NULL for [expr] and [expr: ]
3831 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
3832 */
3833 int
3834eval_index_inner(
3835 typval_T *rettv,
3836 int is_range,
3837 typval_T *var1,
3838 typval_T *var2,
3839 char_u *key,
3840 int keylen,
3841 int verbose)
3842{
3843 long n1, n2 = 0;
3844 long len;
3845
3846 n1 = 0;
3847 if (var1 != NULL && rettv->v_type != VAR_DICT)
3848 n1 = tv_get_number(var1);
3849
3850 if (is_range)
3851 {
3852 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003853 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003854 if (verbose)
3855 emsg(_(e_cannot_slice_dictionary));
3856 return FAIL;
3857 }
3858 if (var2 == NULL)
3859 n2 = -1;
3860 else
3861 n2 = tv_get_number(var2);
3862 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01003863
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003864 switch (rettv->v_type)
3865 {
3866 case VAR_UNKNOWN:
3867 case VAR_ANY:
3868 case VAR_VOID:
3869 case VAR_FUNC:
3870 case VAR_PARTIAL:
3871 case VAR_FLOAT:
3872 case VAR_BOOL:
3873 case VAR_SPECIAL:
3874 case VAR_JOB:
3875 case VAR_CHANNEL:
3876 break; // not evaluating, skipping over subscript
3877
3878 case VAR_NUMBER:
3879 case VAR_STRING:
3880 {
3881 char_u *s = tv_get_string(rettv);
3882
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003883 len = (long)STRLEN(s);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003884 if (in_vim9script())
3885 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003886 if (is_range)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003887 s = string_slice(s, n1, n2);
3888 else
3889 s = char_from_string(s, n1);
3890 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003891 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003892 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003893 // The resulting variable is a substring. If the indexes
3894 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003895 if (n1 < 0)
3896 {
3897 n1 = len + n1;
3898 if (n1 < 0)
3899 n1 = 0;
3900 }
3901 if (n2 < 0)
3902 n2 = len + n2;
3903 else if (n2 >= len)
3904 n2 = len;
3905 if (n1 >= len || n2 < 0 || n1 > n2)
3906 s = NULL;
3907 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02003908 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003909 }
3910 else
3911 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003912 // The resulting variable is a string of a single
3913 // character. If the index is too big or negative the
3914 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003915 if (n1 >= len || n1 < 0)
3916 s = NULL;
3917 else
3918 s = vim_strnsave(s + n1, 1);
3919 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003920 clear_tv(rettv);
3921 rettv->v_type = VAR_STRING;
3922 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003923 }
3924 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003925
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003926 case VAR_BLOB:
3927 len = blob_len(rettv->vval.v_blob);
3928 if (is_range)
3929 {
3930 // The resulting variable is a sub-blob. If the indexes
3931 // are out of range the result is empty.
3932 if (n1 < 0)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003933 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003934 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003935 if (n1 < 0)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003936 n1 = 0;
3937 }
3938 if (n2 < 0)
3939 n2 = len + n2;
3940 else if (n2 >= len)
3941 n2 = len - 1;
3942 if (n1 >= len || n2 < 0 || n1 > n2)
3943 {
3944 clear_tv(rettv);
3945 rettv->v_type = VAR_BLOB;
3946 rettv->vval.v_blob = NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003947 }
3948 else
3949 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003950 blob_T *blob = blob_alloc();
3951 long i;
3952
3953 if (blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003954 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003955 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003956 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003957 blob_free(blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003958 return FAIL;
3959 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003960 blob->bv_ga.ga_len = n2 - n1 + 1;
3961 for (i = n1; i <= n2; i++)
3962 blob_set(blob, i - n1,
3963 blob_get(rettv->vval.v_blob, i));
3964
3965 clear_tv(rettv);
3966 rettv_blob_set(rettv, blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003967 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003968 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003969 }
3970 else
3971 {
3972 // The resulting variable is a byte value.
3973 // If the index is too big or negative that is an error.
3974 if (n1 < 0)
3975 n1 = len + n1;
3976 if (n1 < len && n1 >= 0)
3977 {
3978 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003979
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003980 clear_tv(rettv);
3981 rettv->v_type = VAR_NUMBER;
3982 rettv->vval.v_number = v;
3983 }
3984 else
3985 semsg(_(e_blobidx), n1);
3986 }
3987 break;
3988
3989 case VAR_LIST:
3990 if (var1 == NULL)
3991 n1 = 0;
3992 if (var2 == NULL)
3993 n2 = -1;
3994 if (list_slice_or_index(rettv->vval.v_list,
3995 is_range, n1, n2, rettv, verbose) == FAIL)
3996 return FAIL;
3997 break;
3998
3999 case VAR_DICT:
4000 {
4001 dictitem_T *item;
4002 typval_T tmp;
4003
4004 if (key == NULL)
4005 {
4006 key = tv_get_string_chk(var1);
4007 if (key == NULL)
4008 return FAIL;
4009 }
4010
4011 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
4012
4013 if (item == NULL && verbose)
4014 semsg(_(e_dictkey), key);
4015 if (item == NULL)
4016 return FAIL;
4017
4018 copy_tv(&item->di_tv, &tmp);
4019 clear_tv(rettv);
4020 *rettv = tmp;
4021 }
4022 break;
4023 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004024 return OK;
4025}
4026
4027/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004028 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004029 */
4030 char_u *
4031partial_name(partial_T *pt)
4032{
4033 if (pt->pt_name != NULL)
4034 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004035 if (pt->pt_func != NULL)
4036 return pt->pt_func->uf_name;
4037 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004038}
4039
Bram Moolenaarddecc252016-04-06 22:59:37 +02004040 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004041partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004042{
4043 int i;
4044
4045 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004046 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004047 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004048 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004049 if (pt->pt_name != NULL)
4050 {
4051 func_unref(pt->pt_name);
4052 vim_free(pt->pt_name);
4053 }
4054 else
4055 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004056
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004057 // Decrease the reference count for the context of a closure. If down
4058 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004059 if (pt->pt_funcstack != NULL)
4060 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004061 --pt->pt_funcstack->fs_refcount;
4062 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004063 }
4064
Bram Moolenaarddecc252016-04-06 22:59:37 +02004065 vim_free(pt);
4066}
4067
4068/*
4069 * Unreference a closure: decrement the reference count and free it when it
4070 * becomes zero.
4071 */
4072 void
4073partial_unref(partial_T *pt)
4074{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004075 if (pt != NULL)
4076 {
4077 if (--pt->pt_refcount <= 0)
4078 partial_free(pt);
4079
4080 // If the reference count goes down to one, the funcstack may be the
4081 // only reference and can be freed if no other partials reference it.
4082 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4083 funcstack_check_refcount(pt->pt_funcstack);
4084 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004085}
4086
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004087/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004088 * Return the next (unique) copy ID.
4089 * Used for serializing nested structures.
4090 */
4091 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004092get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004093{
4094 current_copyID += COPYID_INC;
4095 return current_copyID;
4096}
4097
4098/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004099 * Garbage collection for lists and dictionaries.
4100 *
4101 * We use reference counts to be able to free most items right away when they
4102 * are no longer used. But for composite items it's possible that it becomes
4103 * unused while the reference count is > 0: When there is a recursive
4104 * reference. Example:
4105 * :let l = [1, 2, 3]
4106 * :let d = {9: l}
4107 * :let l[1] = d
4108 *
4109 * Since this is quite unusual we handle this with garbage collection: every
4110 * once in a while find out which lists and dicts are not referenced from any
4111 * variable.
4112 *
4113 * Here is a good reference text about garbage collection (refers to Python
4114 * but it applies to all reference-counting mechanisms):
4115 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004116 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004117
4118/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004119 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004120 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004121 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004122 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004123 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004124garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004125{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004126 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004127 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004128 buf_T *buf;
4129 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004130 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004131 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004132
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004133 if (!testing)
4134 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004135 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004136 want_garbage_collect = FALSE;
4137 may_garbage_collect = FALSE;
4138 garbage_collect_at_exit = FALSE;
4139 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004140
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004141 // The execution stack can grow big, limit the size.
4142 if (exestack.ga_maxlen - exestack.ga_len > 500)
4143 {
4144 size_t new_len;
4145 char_u *pp;
4146 int n;
4147
4148 // Keep 150% of the current size, with a minimum of the growth size.
4149 n = exestack.ga_len / 2;
4150 if (n < exestack.ga_growsize)
4151 n = exestack.ga_growsize;
4152
4153 // Don't make it bigger though.
4154 if (exestack.ga_len + n < exestack.ga_maxlen)
4155 {
4156 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4157 pp = vim_realloc(exestack.ga_data, new_len);
4158 if (pp == NULL)
4159 return FAIL;
4160 exestack.ga_maxlen = exestack.ga_len + n;
4161 exestack.ga_data = pp;
4162 }
4163 }
4164
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004165 // We advance by two because we add one for items referenced through
4166 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004167 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004168
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004169 /*
4170 * 1. Go through all accessible variables and mark all lists and dicts
4171 * with copyID.
4172 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004173
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004174 // Don't free variables in the previous_funccal list unless they are only
4175 // referenced through previous_funccal. This must be first, because if
4176 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004177 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004178
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004179 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004180 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004181
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004182 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004183 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004184 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4185 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004186
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004187 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004188 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004189 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4190 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004191 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004192 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4193 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004194#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004195 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004196 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4197 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004198 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004199 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004200 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4201 NULL, NULL);
4202#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004203
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004204 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004205 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004206 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4207 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004208 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004209 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004210
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004211 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004212 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004213
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004214 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004215 abort = abort || set_ref_in_functions(copyID);
4216
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004217 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004218 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004219
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004220 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004221 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004222
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004223 // callbacks in buffers
4224 abort = abort || set_ref_in_buffers(copyID);
4225
Bram Moolenaar1dced572012-04-05 16:54:08 +02004226#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004227 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004228#endif
4229
Bram Moolenaardb913952012-06-29 12:54:53 +02004230#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004231 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004232#endif
4233
4234#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004235 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004236#endif
4237
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004238#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004239 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004240 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004241#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004242#ifdef FEAT_NETBEANS_INTG
4243 abort = abort || set_ref_in_nb_channel(copyID);
4244#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004245
Bram Moolenaare3188e22016-05-31 21:13:04 +02004246#ifdef FEAT_TIMERS
4247 abort = abort || set_ref_in_timer(copyID);
4248#endif
4249
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004250#ifdef FEAT_QUICKFIX
4251 abort = abort || set_ref_in_quickfix(copyID);
4252#endif
4253
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004254#ifdef FEAT_TERMINAL
4255 abort = abort || set_ref_in_term(copyID);
4256#endif
4257
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004258#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004259 abort = abort || set_ref_in_popups(copyID);
4260#endif
4261
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004262 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004263 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004264 /*
4265 * 2. Free lists and dictionaries that are not referenced.
4266 */
4267 did_free = free_unref_items(copyID);
4268
4269 /*
4270 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004271 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004272 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004273 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004274 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004275 else if (p_verbose > 0)
4276 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004277 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004278 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004279
4280 return did_free;
4281}
4282
4283/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004284 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004285 */
4286 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004287free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004288{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004289 int did_free = FALSE;
4290
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004291 // Let all "free" functions know that we are here. This means no
4292 // dictionaries, lists, channels or jobs are to be freed, because we will
4293 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004294 in_free_unref_items = TRUE;
4295
4296 /*
4297 * PASS 1: free the contents of the items. We don't free the items
4298 * themselves yet, so that it is possible to decrement refcount counters
4299 */
4300
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004301 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004302 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004303
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004304 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004305 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004306
4307#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004308 // Go through the list of jobs and free items without the copyID. This
4309 // must happen before doing channels, because jobs refer to channels, but
4310 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004311 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4312
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004313 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004314 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4315#endif
4316
4317 /*
4318 * PASS 2: free the items themselves.
4319 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004320 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004321 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004322
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004323#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004324 // Go through the list of jobs and free items without the copyID. This
4325 // must happen before doing channels, because jobs refer to channels, but
4326 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004327 free_unused_jobs(copyID, COPYID_MASK);
4328
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004329 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004330 free_unused_channels(copyID, COPYID_MASK);
4331#endif
4332
4333 in_free_unref_items = FALSE;
4334
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004335 return did_free;
4336}
4337
4338/*
4339 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004340 * "list_stack" is used to add lists to be marked. Can be NULL.
4341 *
4342 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004343 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004344 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004345set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004346{
4347 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004348 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004349 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004350 hashtab_T *cur_ht;
4351 ht_stack_T *ht_stack = NULL;
4352 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004353
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004354 cur_ht = ht;
4355 for (;;)
4356 {
4357 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004358 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004359 // Mark each item in the hashtab. If the item contains a hashtab
4360 // it is added to ht_stack, if it contains a list it is added to
4361 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004362 todo = (int)cur_ht->ht_used;
4363 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4364 if (!HASHITEM_EMPTY(hi))
4365 {
4366 --todo;
4367 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4368 &ht_stack, list_stack);
4369 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004370 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004371
4372 if (ht_stack == NULL)
4373 break;
4374
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004375 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004376 cur_ht = ht_stack->ht;
4377 tempitem = ht_stack;
4378 ht_stack = ht_stack->prev;
4379 free(tempitem);
4380 }
4381
4382 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004383}
4384
4385/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004386 * Mark a dict and its items with "copyID".
4387 * Returns TRUE if setting references failed somehow.
4388 */
4389 int
4390set_ref_in_dict(dict_T *d, int copyID)
4391{
4392 if (d != NULL && d->dv_copyID != copyID)
4393 {
4394 d->dv_copyID = copyID;
4395 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4396 }
4397 return FALSE;
4398}
4399
4400/*
4401 * Mark a list and its items with "copyID".
4402 * Returns TRUE if setting references failed somehow.
4403 */
4404 int
4405set_ref_in_list(list_T *ll, int copyID)
4406{
4407 if (ll != NULL && ll->lv_copyID != copyID)
4408 {
4409 ll->lv_copyID = copyID;
4410 return set_ref_in_list_items(ll, copyID, NULL);
4411 }
4412 return FALSE;
4413}
4414
4415/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004416 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004417 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4418 *
4419 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004420 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004421 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004422set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004423{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004424 listitem_T *li;
4425 int abort = FALSE;
4426 list_T *cur_l;
4427 list_stack_T *list_stack = NULL;
4428 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004429
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004430 cur_l = l;
4431 for (;;)
4432 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004433 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004434 // Mark each item in the list. If the item contains a hashtab
4435 // it is added to ht_stack, if it contains a list it is added to
4436 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004437 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4438 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4439 ht_stack, &list_stack);
4440 if (list_stack == NULL)
4441 break;
4442
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004443 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004444 cur_l = list_stack->list;
4445 tempitem = list_stack;
4446 list_stack = list_stack->prev;
4447 free(tempitem);
4448 }
4449
4450 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004451}
4452
4453/*
4454 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004455 * "list_stack" is used to add lists to be marked. Can be NULL.
4456 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4457 *
4458 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004459 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004460 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004461set_ref_in_item(
4462 typval_T *tv,
4463 int copyID,
4464 ht_stack_T **ht_stack,
4465 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004466{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004467 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004468
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004469 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004470 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004471 dict_T *dd = tv->vval.v_dict;
4472
Bram Moolenaara03f2332016-02-06 18:09:59 +01004473 if (dd != NULL && dd->dv_copyID != copyID)
4474 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004475 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004476 dd->dv_copyID = copyID;
4477 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004478 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004479 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4480 }
4481 else
4482 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004483 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4484
Bram Moolenaara03f2332016-02-06 18:09:59 +01004485 if (newitem == NULL)
4486 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004487 else
4488 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004489 newitem->ht = &dd->dv_hashtab;
4490 newitem->prev = *ht_stack;
4491 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004492 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004493 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004494 }
4495 }
4496 else if (tv->v_type == VAR_LIST)
4497 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004498 list_T *ll = tv->vval.v_list;
4499
Bram Moolenaara03f2332016-02-06 18:09:59 +01004500 if (ll != NULL && ll->lv_copyID != copyID)
4501 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004502 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004503 ll->lv_copyID = copyID;
4504 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004505 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004506 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004507 }
4508 else
4509 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004510 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4511
Bram Moolenaara03f2332016-02-06 18:09:59 +01004512 if (newitem == NULL)
4513 abort = TRUE;
4514 else
4515 {
4516 newitem->list = ll;
4517 newitem->prev = *list_stack;
4518 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004519 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004520 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004521 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004522 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004523 else if (tv->v_type == VAR_FUNC)
4524 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004525 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004526 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004527 else if (tv->v_type == VAR_PARTIAL)
4528 {
4529 partial_T *pt = tv->vval.v_partial;
4530 int i;
4531
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004532 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004533 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004534 // Didn't see this partial yet.
4535 pt->pt_copyID = copyID;
4536
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004537 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004538
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004539 if (pt->pt_dict != NULL)
4540 {
4541 typval_T dtv;
4542
4543 dtv.v_type = VAR_DICT;
4544 dtv.vval.v_dict = pt->pt_dict;
4545 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4546 }
4547
4548 for (i = 0; i < pt->pt_argc; ++i)
4549 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4550 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004551 if (pt->pt_funcstack != NULL)
4552 {
4553 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4554
4555 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4556 abort = abort || set_ref_in_item(stack + i, copyID,
4557 ht_stack, list_stack);
4558 }
4559
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004560 }
4561 }
4562#ifdef FEAT_JOB_CHANNEL
4563 else if (tv->v_type == VAR_JOB)
4564 {
4565 job_T *job = tv->vval.v_job;
4566 typval_T dtv;
4567
4568 if (job != NULL && job->jv_copyID != copyID)
4569 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004570 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004571 if (job->jv_channel != NULL)
4572 {
4573 dtv.v_type = VAR_CHANNEL;
4574 dtv.vval.v_channel = job->jv_channel;
4575 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4576 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004577 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004578 {
4579 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004580 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004581 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4582 }
4583 }
4584 }
4585 else if (tv->v_type == VAR_CHANNEL)
4586 {
4587 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004588 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004589 typval_T dtv;
4590 jsonq_T *jq;
4591 cbq_T *cq;
4592
4593 if (ch != NULL && ch->ch_copyID != copyID)
4594 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004595 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004596 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004597 {
4598 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4599 jq = jq->jq_next)
4600 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4601 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4602 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004603 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004604 {
4605 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004606 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004607 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4608 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004609 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004610 {
4611 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004612 dtv.vval.v_partial =
4613 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004614 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4615 }
4616 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004617 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004618 {
4619 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004620 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004621 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4622 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004623 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004624 {
4625 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004626 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004627 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4628 }
4629 }
4630 }
4631#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004632 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004633}
4634
Bram Moolenaar8c711452005-01-14 21:53:12 +00004635/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004636 * Return a string with the string representation of a variable.
4637 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004638 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004639 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004640 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004641 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02004642 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004643 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4644 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004645 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004646 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004647 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004648echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004649 typval_T *tv,
4650 char_u **tofree,
4651 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004652 int copyID,
4653 int echo_style,
4654 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004655 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004656{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004657 static int recurse = 0;
4658 char_u *r = NULL;
4659
Bram Moolenaar33570922005-01-25 22:26:29 +00004660 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004661 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004662 if (!did_echo_string_emsg)
4663 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004664 // Only give this message once for a recursive call to avoid
4665 // flooding the user with errors. And stop iterating over lists
4666 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004667 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004668 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004669 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004670 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004671 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004672 }
4673 ++recurse;
4674
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004675 switch (tv->v_type)
4676 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004677 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004678 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004679 {
4680 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004681 r = tv->vval.v_string;
4682 if (r == NULL)
4683 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004684 }
4685 else
4686 {
4687 *tofree = string_quote(tv->vval.v_string, FALSE);
4688 r = *tofree;
4689 }
4690 break;
4691
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004692 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004693 if (echo_style)
4694 {
4695 *tofree = NULL;
4696 r = tv->vval.v_string;
4697 }
4698 else
4699 {
4700 *tofree = string_quote(tv->vval.v_string, TRUE);
4701 r = *tofree;
4702 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004703 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004704
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004705 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004706 {
4707 partial_T *pt = tv->vval.v_partial;
4708 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004709 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004710 garray_T ga;
4711 int i;
4712 char_u *tf;
4713
4714 ga_init2(&ga, 1, 100);
4715 ga_concat(&ga, (char_u *)"function(");
4716 if (fname != NULL)
4717 {
4718 ga_concat(&ga, fname);
4719 vim_free(fname);
4720 }
4721 if (pt != NULL && pt->pt_argc > 0)
4722 {
4723 ga_concat(&ga, (char_u *)", [");
4724 for (i = 0; i < pt->pt_argc; ++i)
4725 {
4726 if (i > 0)
4727 ga_concat(&ga, (char_u *)", ");
4728 ga_concat(&ga,
4729 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4730 vim_free(tf);
4731 }
4732 ga_concat(&ga, (char_u *)"]");
4733 }
4734 if (pt != NULL && pt->pt_dict != NULL)
4735 {
4736 typval_T dtv;
4737
4738 ga_concat(&ga, (char_u *)", ");
4739 dtv.v_type = VAR_DICT;
4740 dtv.vval.v_dict = pt->pt_dict;
4741 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4742 vim_free(tf);
4743 }
4744 ga_concat(&ga, (char_u *)")");
4745
4746 *tofree = ga.ga_data;
4747 r = *tofree;
4748 break;
4749 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004750
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004751 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004752 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004753 break;
4754
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004755 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004756 if (tv->vval.v_list == NULL)
4757 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004758 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004759 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004760 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004761 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004762 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4763 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004764 {
4765 *tofree = NULL;
4766 r = (char_u *)"[...]";
4767 }
4768 else
4769 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004770 int old_copyID = tv->vval.v_list->lv_copyID;
4771
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004772 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004773 *tofree = list2string(tv, copyID, restore_copyID);
4774 if (restore_copyID)
4775 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004776 r = *tofree;
4777 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004778 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004779
Bram Moolenaar8c711452005-01-14 21:53:12 +00004780 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004781 if (tv->vval.v_dict == NULL)
4782 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004783 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004784 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004785 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004786 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004787 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4788 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004789 {
4790 *tofree = NULL;
4791 r = (char_u *)"{...}";
4792 }
4793 else
4794 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004795 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004796
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004797 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004798 *tofree = dict2string(tv, copyID, restore_copyID);
4799 if (restore_copyID)
4800 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004801 r = *tofree;
4802 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004803 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004804
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004805 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004806 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004807 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004808 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004809 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004810 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004811 break;
4812
Bram Moolenaar835dc632016-02-07 14:27:38 +01004813 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004814 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004815 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004816 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004817 if (composite_val)
4818 {
4819 *tofree = string_quote(r, FALSE);
4820 r = *tofree;
4821 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004822 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004823
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004824 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004825#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004826 *tofree = NULL;
4827 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4828 r = numbuf;
4829 break;
4830#endif
4831
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004832 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004833 case VAR_SPECIAL:
4834 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004835 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004836 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004837 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004838
Bram Moolenaar8502c702014-06-17 12:51:16 +02004839 if (--recurse == 0)
4840 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004841 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004842}
4843
4844/*
4845 * Return a string with the string representation of a variable.
4846 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4847 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004848 * Does not put quotes around strings, as ":echo" displays values.
4849 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4850 * May return NULL.
4851 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004852 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004853echo_string(
4854 typval_T *tv,
4855 char_u **tofree,
4856 char_u *numbuf,
4857 int copyID)
4858{
4859 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4860}
4861
4862/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004863 * Return string "str" in ' quotes, doubling ' characters.
4864 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004865 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004866 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004867 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004868string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004869{
Bram Moolenaar33570922005-01-25 22:26:29 +00004870 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004871 char_u *p, *r, *s;
4872
Bram Moolenaar33570922005-01-25 22:26:29 +00004873 len = (function ? 13 : 3);
4874 if (str != NULL)
4875 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004876 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004877 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004878 if (*p == '\'')
4879 ++len;
4880 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004881 s = r = alloc(len);
4882 if (r != NULL)
4883 {
4884 if (function)
4885 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004886 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004887 r += 10;
4888 }
4889 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004890 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004891 if (str != NULL)
4892 for (p = str; *p != NUL; )
4893 {
4894 if (*p == '\'')
4895 *r++ = '\'';
4896 MB_COPY_CHAR(p, r);
4897 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004898 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004899 if (function)
4900 *r++ = ')';
4901 *r++ = NUL;
4902 }
4903 return s;
4904}
4905
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004906#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004907/*
4908 * Convert the string "text" to a floating point number.
4909 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4910 * this always uses a decimal point.
4911 * Returns the length of the text that was consumed.
4912 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004913 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004914string2float(
4915 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004916 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004917{
4918 char *s = (char *)text;
4919 float_T f;
4920
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004921 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004922 if (STRNICMP(text, "inf", 3) == 0)
4923 {
4924 *value = INFINITY;
4925 return 3;
4926 }
4927 if (STRNICMP(text, "-inf", 3) == 0)
4928 {
4929 *value = -INFINITY;
4930 return 4;
4931 }
4932 if (STRNICMP(text, "nan", 3) == 0)
4933 {
4934 *value = NAN;
4935 return 3;
4936 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004937 f = strtod(s, &s);
4938 *value = f;
4939 return (int)((char_u *)s - text);
4940}
4941#endif
4942
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004943/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004945 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004947 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004948var2fpos(
4949 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004950 int dollar_lnum, // TRUE when $ is last line
4951 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004953 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004955 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004957 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004958 if (varp->v_type == VAR_LIST)
4959 {
4960 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004961 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004962 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004963 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004964
4965 l = varp->vval.v_list;
4966 if (l == NULL)
4967 return NULL;
4968
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004969 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004970 pos.lnum = list_find_nr(l, 0L, &error);
4971 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004972 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004973 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004974
Bram Moolenaarec65d772020-08-20 22:29:12 +02004975 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004976 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004977 li = list_find(l, 1L);
4978 if (li != NULL && li->li_tv.v_type == VAR_STRING
4979 && li->li_tv.vval.v_string != NULL
4980 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02004981 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004982 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02004983 }
4984 else
4985 {
4986 pos.col = list_find_nr(l, 1L, &error);
4987 if (error)
4988 return NULL;
4989 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00004990
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004991 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004992 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004993 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004994 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004995
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004996 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004997 pos.coladd = list_find_nr(l, 2L, &error);
4998 if (error)
4999 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005000
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005001 return &pos;
5002 }
5003
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005004 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005005 if (name == NULL)
5006 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005007 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005009 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005010 {
5011 if (VIsual_active)
5012 return &VIsual;
5013 return &curwin->w_cursor;
5014 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005015 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005017 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5019 return NULL;
5020 return pp;
5021 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005022
Bram Moolenaara5525202006-03-02 22:52:09 +00005023 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005024
Bram Moolenaar477933c2007-07-17 14:32:23 +00005025 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005026 {
5027 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005028 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005029 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005030 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005031 // In silent Ex mode topline is zero, but that's not a valid line
5032 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005033 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005034 return &pos;
5035 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005036 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005037 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005038 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005039 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005040 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005041 return &pos;
5042 }
5043 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005044 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005046 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 {
5048 pos.lnum = curbuf->b_ml.ml_line_count;
5049 pos.col = 0;
5050 }
5051 else
5052 {
5053 pos.lnum = curwin->w_cursor.lnum;
5054 pos.col = (colnr_T)STRLEN(ml_get_curline());
5055 }
5056 return &pos;
5057 }
5058 return NULL;
5059}
5060
5061/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005062 * Convert list in "arg" into a position and optional file number.
5063 * When "fnump" is NULL there is no file number, only 3 items.
5064 * Note that the column is passed on as-is, the caller may want to decrement
5065 * it to use 1 for the first column.
5066 * Return FAIL when conversion is not possible, doesn't check the position for
5067 * validity.
5068 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005069 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005070list2fpos(
5071 typval_T *arg,
5072 pos_T *posp,
5073 int *fnump,
5074 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005075{
5076 list_T *l = arg->vval.v_list;
5077 long i = 0;
5078 long n;
5079
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005080 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5081 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005082 if (arg->v_type != VAR_LIST
5083 || l == NULL
5084 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005085 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005086 return FAIL;
5087
5088 if (fnump != NULL)
5089 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005090 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005091 if (n < 0)
5092 return FAIL;
5093 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005094 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005095 *fnump = n;
5096 }
5097
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005098 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005099 if (n < 0)
5100 return FAIL;
5101 posp->lnum = n;
5102
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005103 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005104 if (n < 0)
5105 return FAIL;
5106 posp->col = n;
5107
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005108 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005109 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005110 posp->coladd = 0;
5111 else
5112 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005113
Bram Moolenaar493c1782014-05-28 14:34:46 +02005114 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005115 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005116
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005117 return OK;
5118}
5119
5120/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 * Get the length of an environment variable name.
5122 * Advance "arg" to the first character after the name.
5123 * Return 0 for error.
5124 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005125 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005126get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127{
5128 char_u *p;
5129 int len;
5130
5131 for (p = *arg; vim_isIDc(*p); ++p)
5132 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005133 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 return 0;
5135
5136 len = (int)(p - *arg);
5137 *arg = p;
5138 return len;
5139}
5140
5141/*
5142 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005143 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 * Return 0 if something is wrong.
5145 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005146 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005147get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148{
5149 char_u *p;
5150 int len;
5151
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005152 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005154 {
5155 if (*p == ':')
5156 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005157 // "s:" is start of "s:var", but "n:" is not and can be used in
5158 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005159 len = (int)(p - *arg);
5160 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5161 || len > 1)
5162 break;
5163 }
5164 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005165 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 return 0;
5167
5168 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005169 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170
5171 return len;
5172}
5173
5174/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005175 * Get the length of the name of a variable or function.
5176 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005178 * Return -1 if curly braces expansion failed.
5179 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 * If the name contains 'magic' {}'s, expand them and return the
5181 * expanded name in an allocated string via 'alias' - caller must free.
5182 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005183 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005184get_name_len(
5185 char_u **arg,
5186 char_u **alias,
5187 int evaluate,
5188 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189{
5190 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 char_u *p;
5192 char_u *expr_start;
5193 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005195 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196
5197 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5198 && (*arg)[2] == (int)KE_SNR)
5199 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005200 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 *arg += 3;
5202 return get_id_len(arg) + 3;
5203 }
5204 len = eval_fname_script(*arg);
5205 if (len > 0)
5206 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005207 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 *arg += len;
5209 }
5210
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005212 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005214 p = find_name_end(*arg, &expr_start, &expr_end,
5215 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 if (expr_start != NULL)
5217 {
5218 char_u *temp_string;
5219
5220 if (!evaluate)
5221 {
5222 len += (int)(p - *arg);
5223 *arg = skipwhite(p);
5224 return len;
5225 }
5226
5227 /*
5228 * Include any <SID> etc in the expanded string:
5229 * Thus the -len here.
5230 */
5231 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5232 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005233 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 *alias = temp_string;
5235 *arg = skipwhite(p);
5236 return (int)STRLEN(temp_string);
5237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238
5239 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005240 // Only give an error when there is something, otherwise it will be
5241 // reported at a higher level.
5242 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005243 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244
5245 return len;
5246}
5247
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005248/*
5249 * Find the end of a variable or function name, taking care of magic braces.
5250 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5251 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005252 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005253 * Return a pointer to just after the name. Equal to "arg" if there is no
5254 * valid name.
5255 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005256 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005257find_name_end(
5258 char_u *arg,
5259 char_u **expr_start,
5260 char_u **expr_end,
5261 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005263 int mb_nest = 0;
5264 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005266 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005267 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005269 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005271 *expr_start = NULL;
5272 *expr_end = NULL;
5273 }
5274
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005275 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005276 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5277 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005278 return arg;
5279
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005280 for (p = arg; *p != NUL
5281 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005282 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005283 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005284 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005285 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005286 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005287 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005288 if (*p == '\'')
5289 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005290 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005291 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005292 ;
5293 if (*p == NUL)
5294 break;
5295 }
5296 else if (*p == '"')
5297 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005298 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005299 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005300 if (*p == '\\' && p[1] != NUL)
5301 ++p;
5302 if (*p == NUL)
5303 break;
5304 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005305 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5306 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005307 // "s:" is start of "s:var", but "n:" is not and can be used in
5308 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005309 len = (int)(p - arg);
5310 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005311 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005312 break;
5313 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005314
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005315 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005317 if (*p == '[')
5318 ++br_nest;
5319 else if (*p == ']')
5320 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005322
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005323 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005325 if (*p == '{')
5326 {
5327 mb_nest++;
5328 if (expr_start != NULL && *expr_start == NULL)
5329 *expr_start = p;
5330 }
5331 else if (*p == '}')
5332 {
5333 mb_nest--;
5334 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5335 *expr_end = p;
5336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 }
5339
5340 return p;
5341}
5342
5343/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005344 * Expands out the 'magic' {}'s in a variable/function name.
5345 * Note that this can call itself recursively, to deal with
5346 * constructs like foo{bar}{baz}{bam}
5347 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5348 * "in_start" ^
5349 * "expr_start" ^
5350 * "expr_end" ^
5351 * "in_end" ^
5352 *
5353 * Returns a new allocated string, which the caller must free.
5354 * Returns NULL for failure.
5355 */
5356 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005357make_expanded_name(
5358 char_u *in_start,
5359 char_u *expr_start,
5360 char_u *expr_end,
5361 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005362{
5363 char_u c1;
5364 char_u *retval = NULL;
5365 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005366
5367 if (expr_end == NULL || in_end == NULL)
5368 return NULL;
5369 *expr_start = NUL;
5370 *expr_end = NUL;
5371 c1 = *in_end;
5372 *in_end = NUL;
5373
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005374 temp_result = eval_to_string(expr_start + 1, FALSE);
5375 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005376 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005377 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5378 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005379 if (retval != NULL)
5380 {
5381 STRCPY(retval, in_start);
5382 STRCAT(retval, temp_result);
5383 STRCAT(retval, expr_end + 1);
5384 }
5385 }
5386 vim_free(temp_result);
5387
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005388 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005389 *expr_start = '{';
5390 *expr_end = '}';
5391
5392 if (retval != NULL)
5393 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005394 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005395 if (expr_start != NULL)
5396 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005397 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005398 temp_result = make_expanded_name(retval, expr_start,
5399 expr_end, temp_result);
5400 vim_free(retval);
5401 retval = temp_result;
5402 }
5403 }
5404
5405 return retval;
5406}
5407
5408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005410 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005412 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005413eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005415 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005416}
5417
5418/*
5419 * Return TRUE if character "c" can be used as the first character in a
5420 * variable or function name (excluding '{' and '}').
5421 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005422 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005423eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005424{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005425 return ASCII_ISALPHA(c) || c == '_';
5426}
5427
5428/*
5429 * Return TRUE if character "c" can be used as the first character of a
5430 * dictionary key.
5431 */
5432 int
5433eval_isdictc(int c)
5434{
5435 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436}
5437
5438/*
Bram Moolenaare3c37d82020-08-15 18:39:05 +02005439 * Return the character "str[index]" where "index" is the character index. If
5440 * "index" is out of range NULL is returned.
5441 */
5442 char_u *
5443char_from_string(char_u *str, varnumber_T index)
5444{
5445 size_t nbyte = 0;
5446 varnumber_T nchar = index;
5447 size_t slen;
5448
5449 if (str == NULL || index < 0)
5450 return NULL;
5451 slen = STRLEN(str);
5452 while (nchar > 0 && nbyte < slen)
5453 {
5454 nbyte += MB_CPTR2LEN(str + nbyte);
5455 --nchar;
5456 }
5457 if (nbyte >= slen)
5458 return NULL;
5459 return vim_strnsave(str + nbyte, MB_CPTR2LEN(str + nbyte));
5460}
5461
5462/*
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005463 * Get the byte index for character index "idx" in string "str" with length
5464 * "str_len".
5465 * If going over the end return "str_len".
5466 * If "idx" is negative count from the end, -1 is the last character.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005467 * When going over the start return -1.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005468 */
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005469 static long
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005470char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
5471{
5472 varnumber_T nchar = idx;
5473 size_t nbyte = 0;
5474
5475 if (nchar >= 0)
5476 {
5477 while (nchar > 0 && nbyte < str_len)
5478 {
5479 nbyte += MB_CPTR2LEN(str + nbyte);
5480 --nchar;
5481 }
5482 }
5483 else
5484 {
5485 nbyte = str_len;
5486 while (nchar < 0 && nbyte > 0)
5487 {
5488 --nbyte;
5489 nbyte -= mb_head_off(str, str + nbyte);
5490 ++nchar;
5491 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005492 if (nchar < 0)
5493 return -1;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005494 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005495 return (long)nbyte;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005496}
5497
5498/*
5499 * Return the slice "str[first:last]" using character indexes.
5500 * Return NULL when the result is empty.
5501 */
5502 char_u *
5503string_slice(char_u *str, varnumber_T first, varnumber_T last)
5504{
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005505 long start_byte, end_byte;
5506 size_t slen;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005507
5508 if (str == NULL)
5509 return NULL;
5510 slen = STRLEN(str);
5511 start_byte = char_idx2byte(str, slen, first);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005512 if (start_byte < 0)
5513 start_byte = 0; // first index very negative: use zero
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005514 if (last == -1)
Bram Moolenaar25660542020-08-28 16:38:11 +02005515 end_byte = (long)slen;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005516 else
5517 {
5518 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005519 if (end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005520 // end index is inclusive
5521 end_byte += MB_CPTR2LEN(str + end_byte);
5522 }
5523
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005524 if (start_byte >= (long)slen || end_byte <= start_byte)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005525 return NULL;
5526 return vim_strnsave(str + start_byte, end_byte - start_byte);
5527}
5528
5529/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005530 * Handle:
5531 * - expr[expr], expr[expr:expr] subscript
5532 * - ".name" lookup
5533 * - function call with Funcref variable: func(expr)
5534 * - method call: var->method()
5535 *
5536 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005537 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005538 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005539handle_subscript(
5540 char_u **arg,
5541 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005542 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005543 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005544{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005545 int evaluate = evalarg != NULL
5546 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005547 int ret = OK;
5548 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005549 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005550 int getnext;
5551 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005552
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005553 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005554 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005555 // When at the end of the line and ".name" or "->{" or "->X" follows in
5556 // the next line then consume the line break.
5557 p = eval_next_non_blank(*arg, evalarg, &getnext);
5558 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005559 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaar9d489562020-07-30 20:08:50 +02005560 || (p[0] == '-' && p[1] == '>'
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005561 && (p[2] == '{' || ASCII_ISALPHA(p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005562 {
5563 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005564 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005565 check_white = FALSE;
5566 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005567
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005568 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5569 || rettv->v_type == VAR_PARTIAL))
5570 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005571 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005572 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5573 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005574
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005575 // Stop the expression evaluation when immediately aborting on
5576 // error, or when an interrupt occurred or an exception was thrown
5577 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005578 if (aborting())
5579 {
5580 if (ret == OK)
5581 clear_tv(rettv);
5582 ret = FAIL;
5583 }
5584 dict_unref(selfdict);
5585 selfdict = NULL;
5586 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005587 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005588 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02005589 *arg = p;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005590 if (ret == OK)
5591 {
5592 if ((*arg)[2] == '{')
5593 // expr->{lambda}()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005594 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005595 else
5596 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005597 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005598 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005599 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005600 // "." is ".name" lookup when we found a dict or when evaluating and
5601 // scriptversion is at least 2, where string concatenation is "..".
5602 else if (**arg == '['
5603 || (**arg == '.' && (rettv->v_type == VAR_DICT
5604 || (!evaluate
5605 && (*arg)[1] != '.'
5606 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005607 {
5608 dict_unref(selfdict);
5609 if (rettv->v_type == VAR_DICT)
5610 {
5611 selfdict = rettv->vval.v_dict;
5612 if (selfdict != NULL)
5613 ++selfdict->dv_refcount;
5614 }
5615 else
5616 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005617 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005618 {
5619 clear_tv(rettv);
5620 ret = FAIL;
5621 }
5622 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005623 else
5624 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005625 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005626
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005627 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5628 // Don't do this when "Func" is already a partial that was bound
5629 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005630 if (selfdict != NULL
5631 && (rettv->v_type == VAR_FUNC
5632 || (rettv->v_type == VAR_PARTIAL
5633 && (rettv->vval.v_partial->pt_auto
5634 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005635 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005636
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005637 dict_unref(selfdict);
5638 return ret;
5639}
5640
5641/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005642 * Make a copy of an item.
5643 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005644 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5645 * reference to an already copied list/dict can be used.
5646 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005647 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005648 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005649item_copy(
5650 typval_T *from,
5651 typval_T *to,
5652 int deep,
5653 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005654{
5655 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005656 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005657
Bram Moolenaar33570922005-01-25 22:26:29 +00005658 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005659 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005660 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005661 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005662 }
5663 ++recurse;
5664
5665 switch (from->v_type)
5666 {
5667 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005668 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005669 case VAR_STRING:
5670 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005671 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005672 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005673 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005674 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005675 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005676 copy_tv(from, to);
5677 break;
5678 case VAR_LIST:
5679 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005680 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005681 if (from->vval.v_list == NULL)
5682 to->vval.v_list = NULL;
5683 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5684 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005685 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005686 to->vval.v_list = from->vval.v_list->lv_copylist;
5687 ++to->vval.v_list->lv_refcount;
5688 }
5689 else
5690 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5691 if (to->vval.v_list == NULL)
5692 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005693 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005694 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005695 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005696 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005697 case VAR_DICT:
5698 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005699 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005700 if (from->vval.v_dict == NULL)
5701 to->vval.v_dict = NULL;
5702 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5703 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005704 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005705 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5706 ++to->vval.v_dict->dv_refcount;
5707 }
5708 else
5709 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5710 if (to->vval.v_dict == NULL)
5711 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005712 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005713 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005714 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005715 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005716 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005717 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005718 }
5719 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005720 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005721}
5722
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005723 void
5724echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5725{
5726 char_u *tofree;
5727 char_u numbuf[NUMBUFLEN];
5728 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5729
5730 if (*atstart)
5731 {
5732 *atstart = FALSE;
5733 // Call msg_start() after eval1(), evaluating the expression
5734 // may cause a message to appear.
5735 if (with_space)
5736 {
5737 // Mark the saved text as finishing the line, so that what
5738 // follows is displayed on a new line when scrolling back
5739 // at the more prompt.
5740 msg_sb_eol();
5741 msg_start();
5742 }
5743 }
5744 else if (with_space)
5745 msg_puts_attr(" ", echo_attr);
5746
5747 if (p != NULL)
5748 for ( ; *p != NUL && !got_int; ++p)
5749 {
5750 if (*p == '\n' || *p == '\r' || *p == TAB)
5751 {
5752 if (*p != TAB && *needclr)
5753 {
5754 // remove any text still there from the command
5755 msg_clr_eos();
5756 *needclr = FALSE;
5757 }
5758 msg_putchar_attr(*p, echo_attr);
5759 }
5760 else
5761 {
5762 if (has_mbyte)
5763 {
5764 int i = (*mb_ptr2len)(p);
5765
5766 (void)msg_outtrans_len_attr(p, i, echo_attr);
5767 p += i - 1;
5768 }
5769 else
5770 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5771 }
5772 }
5773 vim_free(tofree);
5774}
5775
Bram Moolenaare9a41262005-01-15 22:18:47 +00005776/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 * ":echo expr1 ..." print each argument separated with a space, add a
5778 * newline at the end.
5779 * ":echon expr1 ..." print each argument plain.
5780 */
5781 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005782ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783{
5784 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005785 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 char_u *p;
5787 int needclr = TRUE;
5788 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01005789 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005790 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005791 evalarg_T evalarg;
5792
Bram Moolenaare707c882020-07-01 13:07:37 +02005793 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794
5795 if (eap->skip)
5796 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005797 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005799 // If eval1() causes an error message the text from the command may
5800 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005801 need_clr_eos = needclr;
5802
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005804 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 {
5806 /*
5807 * Report the invalid expression unless the expression evaluation
5808 * has been cancelled due to an aborting error, an interrupt, or an
5809 * exception.
5810 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005811 if (!aborting() && did_emsg == did_emsg_before
5812 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005813 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005814 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 break;
5816 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005817 need_clr_eos = FALSE;
5818
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005820 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005821
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005822 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 arg = skipwhite(arg);
5824 }
5825 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02005826 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827
5828 if (eap->skip)
5829 --emsg_skip;
5830 else
5831 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005832 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 if (needclr)
5834 msg_clr_eos();
5835 if (eap->cmdidx == CMD_echo)
5836 msg_end();
5837 }
5838}
5839
5840/*
5841 * ":echohl {name}".
5842 */
5843 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005844ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005846 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847}
5848
5849/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005850 * Returns the :echo attribute
5851 */
5852 int
5853get_echo_attr(void)
5854{
5855 return echo_attr;
5856}
5857
5858/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 * ":execute expr1 ..." execute the result of an expression.
5860 * ":echomsg expr1 ..." Print a message
5861 * ":echoerr expr1 ..." Print an error
5862 * Each gets spaces around each argument and a newline at the end for
5863 * echo commands
5864 */
5865 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005866ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867{
5868 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005869 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 int ret = OK;
5871 char_u *p;
5872 garray_T ga;
5873 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874
5875 ga_init2(&ga, 1, 80);
5876
5877 if (eap->skip)
5878 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02005879 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02005881 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01005882 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884
5885 if (!eap->skip)
5886 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005887 char_u buf[NUMBUFLEN];
5888
5889 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01005890 {
5891 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
5892 {
5893 emsg(_(e_inval_string));
5894 p = NULL;
5895 }
5896 else
5897 p = tv_get_string_buf(&rettv, buf);
5898 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005899 else
5900 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005901 if (p == NULL)
5902 {
5903 clear_tv(&rettv);
5904 ret = FAIL;
5905 break;
5906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 len = (int)STRLEN(p);
5908 if (ga_grow(&ga, len + 2) == FAIL)
5909 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005910 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 ret = FAIL;
5912 break;
5913 }
5914 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 ga.ga_len += len;
5918 }
5919
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005920 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 arg = skipwhite(arg);
5922 }
5923
5924 if (ret != FAIL && ga.ga_data != NULL)
5925 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005926 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
5927 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005928 // Mark the already saved text as finishing the line, so that what
5929 // follows is displayed on a new line when scrolling back at the
5930 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005931 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005932 }
5933
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005935 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005936 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005937 out_flush();
5938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 else if (eap->cmdidx == CMD_echoerr)
5940 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005941 int save_did_emsg = did_emsg;
5942
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005943 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005944 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 if (!force_abort)
5946 did_emsg = save_did_emsg;
5947 }
5948 else if (eap->cmdidx == CMD_execute)
5949 do_cmdline((char_u *)ga.ga_data,
5950 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
5951 }
5952
5953 ga_clear(&ga);
5954
5955 if (eap->skip)
5956 --emsg_skip;
5957
5958 eap->nextcmd = check_nextcmd(arg);
5959}
5960
5961/*
5962 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
5963 * "arg" points to the "&" or '+' when called, to "option" when returning.
5964 * Returns NULL when no option name found. Otherwise pointer to the char
5965 * after the option name.
5966 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005967 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005968find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969{
5970 char_u *p = *arg;
5971
5972 ++p;
5973 if (*p == 'g' && p[1] == ':')
5974 {
5975 *opt_flags = OPT_GLOBAL;
5976 p += 2;
5977 }
5978 else if (*p == 'l' && p[1] == ':')
5979 {
5980 *opt_flags = OPT_LOCAL;
5981 p += 2;
5982 }
5983 else
5984 *opt_flags = 0;
5985
5986 if (!ASCII_ISALPHA(*p))
5987 return NULL;
5988 *arg = p;
5989
5990 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005991 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 else
5993 while (ASCII_ISALPHA(*p))
5994 ++p;
5995 return p;
5996}
5997
5998/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00005999 * Display script name where an item was last set.
6000 * Should only be invoked when 'verbose' is non-zero.
6001 */
6002 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006003last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006004{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006005 char_u *p;
6006
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006007 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006008 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006009 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006010 if (p != NULL)
6011 {
6012 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006013 msg_puts(_("\n\tLast set from "));
6014 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006015 if (script_ctx.sc_lnum > 0)
6016 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006017 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006018 msg_outnum((long)script_ctx.sc_lnum);
6019 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006020 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006021 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006022 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006023 }
6024}
6025
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006026#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027
6028/*
6029 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006030 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 * "flags" can be "g" to do a global substitute.
6032 * Returns an allocated string, NULL for error.
6033 */
6034 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006035do_string_sub(
6036 char_u *str,
6037 char_u *pat,
6038 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006039 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006040 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041{
6042 int sublen;
6043 regmatch_T regmatch;
6044 int i;
6045 int do_all;
6046 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006047 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 garray_T ga;
6049 char_u *ret;
6050 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006051 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006053 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006055 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056
6057 ga_init2(&ga, 1, 200);
6058
6059 do_all = (flags[0] == 'g');
6060
6061 regmatch.rm_ic = p_ic;
6062 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6063 if (regmatch.regprog != NULL)
6064 {
6065 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006066 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6068 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006069 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006070 if (regmatch.startp[0] == regmatch.endp[0])
6071 {
6072 if (zero_width == regmatch.startp[0])
6073 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006074 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006075 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006076 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6077 (size_t)i);
6078 ga.ga_len += i;
6079 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006080 continue;
6081 }
6082 zero_width = regmatch.startp[0];
6083 }
6084
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 /*
6086 * Get some space for a temporary buffer to do the substitution
6087 * into. It will contain:
6088 * - The text up to where the match is.
6089 * - The substituted text.
6090 * - The text after the match.
6091 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006092 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006093 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6095 {
6096 ga_clear(&ga);
6097 break;
6098 }
6099
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006100 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 i = (int)(regmatch.startp[0] - tail);
6102 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006103 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006104 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 + ga.ga_len + i, TRUE, TRUE, FALSE);
6106 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006107 tail = regmatch.endp[0];
6108 if (*tail == NUL)
6109 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 if (!do_all)
6111 break;
6112 }
6113
6114 if (ga.ga_data != NULL)
6115 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6116
Bram Moolenaar473de612013-06-08 18:19:48 +02006117 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 }
6119
6120 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6121 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006122 if (p_cpo == empty_option)
6123 p_cpo = save_cpo;
6124 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006125 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006126 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127
6128 return ret;
6129}