blob: e3a3592c8cb3cbf54b20d13d4986d6d6307f4c94 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010023#define NAMESPACE_CHAR (char_u *)"abglstvw"
24
Bram Moolenaar532c7802005-01-27 14:44:31 +000025/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000026 * When recursively copying lists and dicts we need to remember which ones we
27 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000028 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000029 */
30static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020031
Bram Moolenaar071d4272004-06-13 20:20:40 +000032/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000033 * Info used by a ":for" loop.
34 */
Bram Moolenaar33570922005-01-25 22:26:29 +000035typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000036{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010037 int fi_semicolon; // TRUE if ending in '; var]'
38 int fi_varcount; // nr of variables in the list
Bram Moolenaarb7a78f72020-06-28 18:43:40 +020039 int fi_break_count; // nr of line breaks encountered
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010040 listwatch_T fi_lw; // keep an eye on the item used.
41 list_T *fi_list; // list being used
42 int fi_bi; // index of blob
43 blob_T *fi_blob; // blob being used
Bram Moolenaar33570922005-01-25 22:26:29 +000044} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000045
Bram Moolenaar48e697e2016-01-23 22:17:30 +010046static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020047static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
48static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
49static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
50static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
51static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
52static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +020053static int eval7_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000054
Bram Moolenaar48e697e2016-01-23 22:17:30 +010055static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020056static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020057
Bram Moolenaare21c1582019-03-02 11:57:09 +010058/*
59 * Return "n1" divided by "n2", taking care of dividing by zero.
60 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020061 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010062num_divide(varnumber_T n1, varnumber_T n2)
63{
64 varnumber_T result;
65
66 if (n2 == 0) // give an error message?
67 {
68 if (n1 == 0)
69 result = VARNUM_MIN; // similar to NaN
70 else if (n1 < 0)
71 result = -VARNUM_MAX;
72 else
73 result = VARNUM_MAX;
74 }
75 else
76 result = n1 / n2;
77
78 return result;
79}
80
81/*
82 * Return "n1" modulus "n2", taking care of dividing by zero.
83 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020084 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010085num_modulus(varnumber_T n1, varnumber_T n2)
86{
87 // Give an error when n2 is 0?
88 return (n2 == 0) ? 0 : (n1 % n2);
89}
90
Bram Moolenaara1fa8922017-01-12 20:06:33 +010091#if defined(EBCDIC) || defined(PROTO)
92/*
93 * Compare struct fst by function name.
94 */
95 static int
96compare_func_name(const void *s1, const void *s2)
97{
98 struct fst *p1 = (struct fst *)s1;
99 struct fst *p2 = (struct fst *)s2;
100
101 return STRCMP(p1->f_name, p2->f_name);
102}
103
104/*
105 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100106 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100107 * On machines using EBCDIC we have to sort it.
108 */
109 static void
110sortFunctions(void)
111{
112 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
113
114 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
115}
116#endif
117
Bram Moolenaar33570922005-01-25 22:26:29 +0000118/*
119 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000120 */
121 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100122eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000123{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200124 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200125 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000126
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200127#ifdef EBCDIC
128 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100129 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200130 */
131 sortFunctions();
132#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000133}
134
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000135#if defined(EXITFREE) || defined(PROTO)
136 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100137eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000138{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200139 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100140 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200141 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000142
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200143 // autoloaded script names
144 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000145
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200146 // unreferenced lists and dicts
147 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200148
149 // functions not garbage collected
150 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000151}
152#endif
153
Bram Moolenaare6b53242020-07-01 17:28:33 +0200154 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200155fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
156{
157 CLEAR_FIELD(*evalarg);
158 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
159 if (eap != NULL && getline_equal(eap->getline, eap->cookie, getsourceline))
160 {
161 evalarg->eval_getline = eap->getline;
162 evalarg->eval_cookie = eap->cookie;
163 }
164}
165
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166/*
167 * Top level evaluation function, returning a boolean.
168 * Sets "error" to TRUE if there was an error.
169 * Return TRUE or FALSE.
170 */
171 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100172eval_to_bool(
173 char_u *arg,
174 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200175 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100176 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177{
Bram Moolenaar33570922005-01-25 22:26:29 +0000178 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200179 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200180 evalarg_T evalarg;
181
Bram Moolenaar37c83712020-06-30 21:18:36 +0200182 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183
184 if (skip)
185 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200186 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 else
189 {
190 *error = FALSE;
191 if (!skip)
192 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200193 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200194 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200195 else
196 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000197 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 }
199 }
200 if (skip)
201 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200202 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200204 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205}
206
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100207/*
208 * Call eval1() and give an error message if not done at a lower level.
209 */
210 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200211eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100212{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100213 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100214 int ret;
215 int did_emsg_before = did_emsg;
216 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200217 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100218
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200219 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
220
221 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100222 if (ret == FAIL)
223 {
224 // Report the invalid expression unless the expression evaluation has
225 // been cancelled due to an aborting error, an interrupt, or an
226 // exception, or we already gave a more specific error.
227 // Also check called_emsg for when using assert_fails().
228 if (!aborting() && did_emsg == did_emsg_before
229 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100230 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100231 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200232 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100233 return ret;
234}
235
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100236/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200237 * Return whether a typval is a valid expression to pass to eval_expr_typval()
238 * or eval_expr_to_bool(). An empty string returns FALSE;
239 */
240 int
241eval_expr_valid_arg(typval_T *tv)
242{
243 return tv->v_type != VAR_UNKNOWN
244 && (tv->v_type != VAR_STRING
245 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
246}
247
248/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100249 * Evaluate an expression, which can be a function, partial or string.
250 * Pass arguments "argv[argc]".
251 * Return the result in "rettv" and OK or FAIL.
252 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200253 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100254eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
255{
256 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100257 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200258 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100259
260 if (expr->v_type == VAR_FUNC)
261 {
262 s = expr->vval.v_string;
263 if (s == NULL || *s == NUL)
264 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200265 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200266 funcexe.evaluate = TRUE;
267 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100268 return FAIL;
269 }
270 else if (expr->v_type == VAR_PARTIAL)
271 {
272 partial_T *partial = expr->vval.v_partial;
273
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200274 if (partial == NULL)
275 return FAIL;
276
Bram Moolenaar822ba242020-05-24 23:00:18 +0200277 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200278 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100279 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200280 if (call_def_function(partial->pt_func, argc, argv,
281 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100282 return FAIL;
283 }
284 else
285 {
286 s = partial_name(partial);
287 if (s == NULL || *s == NUL)
288 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200289 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100290 funcexe.evaluate = TRUE;
291 funcexe.partial = partial;
292 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
293 return FAIL;
294 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100295 }
296 else
297 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100298 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100299 if (s == NULL)
300 return FAIL;
301 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200302 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100303 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200304 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100305 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200306 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100307 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100308 return FAIL;
309 }
310 }
311 return OK;
312}
313
314/*
315 * Like eval_to_bool() but using a typval_T instead of a string.
316 * Works for string, funcref and partial.
317 */
318 int
319eval_expr_to_bool(typval_T *expr, int *error)
320{
321 typval_T rettv;
322 int res;
323
324 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
325 {
326 *error = TRUE;
327 return FALSE;
328 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200329 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100330 clear_tv(&rettv);
331 return res;
332}
333
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334/*
335 * Top level evaluation function, returning a string. If "skip" is TRUE,
336 * only parsing to "nextcmd" is done, without reporting errors. Return
337 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
338 */
339 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100340eval_to_string_skip(
341 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200342 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100343 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344{
Bram Moolenaar33570922005-01-25 22:26:29 +0000345 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200347 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348
Bram Moolenaar37c83712020-06-30 21:18:36 +0200349 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 if (skip)
351 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200352 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 retval = NULL;
354 else
355 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100356 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000357 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 }
359 if (skip)
360 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200361 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362
363 return retval;
364}
365
366/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000367 * Skip over an expression at "*pp".
368 * Return FAIL for an error, OK otherwise.
369 */
370 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200371skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000372{
Bram Moolenaar33570922005-01-25 22:26:29 +0000373 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000374
375 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200376 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000377}
378
379/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200380 * Skip over an expression at "*pp".
381 * If in Vim9 script and line breaks are encountered, the lines are
382 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200383 * "arg" is advanced to just after the expression.
384 * "start" is set to the start of the expression, "end" to just after the end.
385 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200386 * Return FAIL for an error, OK otherwise.
387 */
388 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200389skip_expr_concatenate(
390 char_u **arg,
391 char_u **start,
392 char_u **end,
393 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200394{
395 typval_T rettv;
396 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200397 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200398 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200399 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200400 int evaluate = evalarg == NULL
401 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200402
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200403 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200404 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200405 {
406 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200407 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200408 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200409 ++gap->ga_len;
410 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200411 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200412
413 // Don't evaluate the expression.
414 if (evalarg != NULL)
415 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200416 *arg = skipwhite(*arg);
417 res = eval1(arg, &rettv, evalarg);
418 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200419 if (evalarg != NULL)
420 evalarg->eval_flags = save_flags;
421
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200422 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200423 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200424 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200425 if (evalarg->eval_ga.ga_len == 1)
426 {
427 // just one line, no need to concatenate
428 ga_clear(gap);
429 gap->ga_itemsize = 0;
430 }
431 else
432 {
433 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200434 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200435
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200436 // Line breaks encountered, concatenate all the lines.
437 *((char_u **)gap->ga_data) = *start;
438 p = ga_concat_strings(gap, "");
439
440 // free the lines only when using getsourceline()
441 if (evalarg->eval_cookie != NULL)
442 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200443 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200444 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200445 // Do not free the last line, "arg" points into it, free it
446 // later.
447 vim_free(evalarg->eval_tofree);
448 evalarg->eval_tofree =
449 ((char_u **)gap->ga_data)[gap->ga_len - 1];
450 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200451 ga_clear_strings(gap);
452 }
453 else
454 ga_clear(gap);
455 gap->ga_itemsize = 0;
456 if (p == NULL)
457 return FAIL;
458 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200459 vim_free(evalarg->eval_tofree_lambda);
460 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200461 // Compute "end" relative to the end.
462 *end = *start + STRLEN(*start) - endoff;
463 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200464 }
465
466 return res;
467}
468
469/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200470 * Top level evaluation function, returning a string. Does not handle line
471 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000472 * When "convert" is TRUE convert a List into a sequence of lines and convert
473 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 * Return pointer to allocated memory, or NULL for failure.
475 */
476 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100477eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100478 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100479 int convert,
480 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481{
Bram Moolenaar33570922005-01-25 22:26:29 +0000482 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000484 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000485#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000486 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000487#endif
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100488 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100490 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
491 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 retval = NULL;
493 else
494 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000495 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000496 {
497 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000498 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200499 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200500 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200501 if (tv.vval.v_list->lv_len > 0)
502 ga_append(&ga, NL);
503 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000504 ga_append(&ga, NUL);
505 retval = (char_u *)ga.ga_data;
506 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000507#ifdef FEAT_FLOAT
508 else if (convert && tv.v_type == VAR_FLOAT)
509 {
510 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
511 retval = vim_strsave(numbuf);
512 }
513#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000514 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100515 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000516 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100518 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519
520 return retval;
521}
522
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100523 char_u *
524eval_to_string(
525 char_u *arg,
526 int convert)
527{
528 return eval_to_string_eap(arg, convert, NULL);
529}
530
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000532 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200533 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200534 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 */
536 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100537eval_to_string_safe(
538 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100539 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540{
541 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200542 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200543 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200545 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200546 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000547 if (use_sandbox)
548 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200549 ++textwinlock;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200550 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000551 if (use_sandbox)
552 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200553 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200554 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200555 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 return retval;
557}
558
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559/*
560 * Top level evaluation function, returning a number.
561 * Evaluates "expr" silently.
562 * Returns -1 for an error.
563 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200564 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100565eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566{
Bram Moolenaar33570922005-01-25 22:26:29 +0000567 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200568 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000569 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570
571 ++emsg_off;
572
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200573 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 retval = -1;
575 else
576 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100577 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000578 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 }
580 --emsg_off;
581
582 return retval;
583}
584
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000585/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000586 * Top level evaluation function.
587 * Returns an allocated typval_T with the result.
588 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000589 */
590 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200591eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000592{
593 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200594 evalarg_T evalarg;
595
596 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000597
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200598 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200599 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100600 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000601
Bram Moolenaar37c83712020-06-30 21:18:36 +0200602 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000603 return tv;
604}
605
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100607 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200608 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
609 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000610 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200612 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100613call_vim_function(
614 char_u *func,
615 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200616 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200617 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000619 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200620 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100622 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200623 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200624 funcexe.firstline = curwin->w_cursor.lnum;
625 funcexe.lastline = curwin->w_cursor.lnum;
626 funcexe.evaluate = TRUE;
627 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000628 if (ret == FAIL)
629 clear_tv(rettv);
630
631 return ret;
632}
633
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100634/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100635 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100636 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200637 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
638 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100639 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200640 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100641call_func_retnr(
642 char_u *func,
643 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200644 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100645{
646 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200647 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100648
Bram Moolenaarded27a12018-08-01 19:06:03 +0200649 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100650 return -1;
651
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100652 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100653 clear_tv(&rettv);
654 return retval;
655}
656
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000657/*
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100658 * Call Vim script function like call_func_retnr() and drop the result.
659 * Returns FAIL when calling the function fails.
660 */
661 int
662call_func_noret(
663 char_u *func,
664 int argc,
665 typval_T *argv)
666{
667 typval_T rettv;
668
669 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
670 return FAIL;
671 clear_tv(&rettv);
672 return OK;
673}
674
675/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100676 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100677 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000678 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000679 */
680 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100681call_func_retstr(
682 char_u *func,
683 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200684 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000685{
686 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000687 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000688
Bram Moolenaarded27a12018-08-01 19:06:03 +0200689 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000690 return NULL;
691
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100692 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000693 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 return retval;
695}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000696
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000697/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100698 * Call Vim script function "func" and return the result as a List.
Bram Moolenaar5b3d1bb2020-12-22 12:20:08 +0100699 * Uses "argv" and "argc" as call_func_retnr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000700 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000701 */
702 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100703call_func_retlist(
704 char_u *func,
705 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200706 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000707{
708 typval_T rettv;
709
Bram Moolenaarded27a12018-08-01 19:06:03 +0200710 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000711 return NULL;
712
713 if (rettv.v_type != VAR_LIST)
714 {
715 clear_tv(&rettv);
716 return NULL;
717 }
718
719 return rettv.vval.v_list;
720}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722#ifdef FEAT_FOLDING
723/*
724 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
725 * it in "*cp". Doesn't give error messages.
726 */
727 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100728eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729{
Bram Moolenaar33570922005-01-25 22:26:29 +0000730 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200731 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000733 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
734 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735
736 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000737 if (use_sandbox)
738 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200739 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200741 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 retval = 0;
743 else
744 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100745 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000746 if (tv.v_type == VAR_NUMBER)
747 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000748 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 retval = 0;
750 else
751 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100752 // If the result is a string, check if there is a non-digit before
753 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000754 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 if (!VIM_ISDIGIT(*s) && *s != '-')
756 *cp = *s++;
757 retval = atol((char *)s);
758 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000759 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 }
761 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000762 if (use_sandbox)
763 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200764 --textwinlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200765 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200767 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768}
769#endif
770
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000772 * Get an lval: variable, Dict item or List item that can be assigned a value
773 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
774 * "name.key", "name.key[expr]" etc.
775 * Indexing only works if "name" is an existing List or Dictionary.
776 * "name" points to the start of the name.
777 * If "rettv" is not NULL it points to the value to be assigned.
778 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
779 * wrong; must end in space or cmd separator.
780 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100781 * flags:
782 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100783 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100784 * GLV_NO_AUTOLOAD: do not use script autoloading
785 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000786 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000787 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000788 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000789 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200790 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100791get_lval(
792 char_u *name,
793 typval_T *rettv,
794 lval_T *lp,
795 int unlet,
796 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100797 int flags, // GLV_ values
798 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000799{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000800 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000801 char_u *expr_start, *expr_end;
802 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000803 dictitem_T *v;
804 typval_T var1;
805 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000806 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000807 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000808 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000809 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100810 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100811 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000812
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100813 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200814 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000815
816 if (skip)
817 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100818 // When skipping just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000819 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200820 lp->ll_name_end = find_name_end(name, NULL, NULL,
821 FNE_INCL_BR | fne_flags);
822 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000823 }
824
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100825 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000826 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100827 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000828 if (expr_start != NULL)
829 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100830 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100831 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000832 && *p != '[' && *p != '.')
833 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200834 semsg(_(e_trailing_arg), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000835 return NULL;
836 }
837
838 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
839 if (lp->ll_exp_name == NULL)
840 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100841 // Report an invalid expression in braces, unless the
842 // expression evaluation has been cancelled due to an
843 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000844 if (!aborting() && !quiet)
845 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000846 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100847 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000848 return NULL;
849 }
850 }
851 lp->ll_name = lp->ll_exp_name;
852 }
853 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100854 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000855 lp->ll_name = name;
856
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200857 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100858 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200859 // "a: type" is declaring variable "a" with a type, not "a:".
860 if (p == name + 2 && p[-1] == ':')
861 {
862 --p;
863 lp->ll_name_end = p;
864 }
865 if (*p == ':')
866 {
867 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
868 char_u *tp = skipwhite(p + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100869
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200870 // parse the type after the name
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100871 lp->ll_type = parse_type(&tp, &si->sn_type_list, !quiet);
872 if (lp->ll_type == NULL && !quiet)
873 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +0200874 lp->ll_name_end = tp;
875 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100876 }
877 }
878
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100879 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000880 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
881 return p;
882
883 cc = *p;
884 *p = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100885 // Only pass &ht when we would write to the variable, it prevents autoload
886 // as well.
Bram Moolenaar6e65d592017-12-07 22:11:27 +0100887 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
888 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000889 if (v == NULL && !quiet)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200890 semsg(_(e_undefined_variable_str), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000891 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000892 if (v == NULL)
893 return NULL;
894
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +0100895 if (in_vim9script() && (flags & GLV_NO_DECL) == 0)
896 {
897 if (!quiet)
898 semsg(_(e_variable_already_declared), lp->ll_name);
899 return NULL;
900 }
901
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000902 /*
903 * Loop until no more [idx] or .key is following.
904 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000905 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100906 var1.v_type = VAR_UNKNOWN;
907 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000908 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000909 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000910 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
Bram Moolenaar81ed4962020-09-23 15:56:50 +0200911 && !(lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100912 && !(lp->ll_tv->v_type == VAR_BLOB
913 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000914 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000915 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100916 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000917 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000918 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000919 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000920 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000921 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100922 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000923 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000924 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000925
Bram Moolenaar10c65862020-10-08 21:16:42 +0200926 if (in_vim9script() && lp->ll_valtype == NULL
927 && lp->ll_tv == &v->di_tv
928 && ht != NULL && ht == get_script_local_ht())
929 {
930 svar_T *sv = find_typval_in_script(lp->ll_tv);
931
932 // Vim9 script local variable: get the type
933 if (sv != NULL)
934 lp->ll_valtype = sv->sv_type;
935 }
936
Bram Moolenaar8c711452005-01-14 21:53:12 +0000937 len = -1;
938 if (*p == '.')
939 {
940 key = p + 1;
941 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
942 ;
943 if (len == 0)
944 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000945 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100946 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000947 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000948 }
949 p = key + len;
950 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000951 else
952 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100953 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000954 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000955 if (*p == ':')
956 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000957 else
958 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000959 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200960 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000961 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100962 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000963 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100964 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000965 clear_tv(&var1);
966 return NULL;
967 }
Bram Moolenaar6a250262020-08-04 15:53:01 +0200968 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000969 }
970
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100971 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000972 if (*p == ':')
973 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000974 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000975 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000976 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +0200977 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100978 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000979 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000980 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100981 if (rettv != NULL
982 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100983 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100984 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100985 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +0000986 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000987 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100988 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100989 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000990 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000991 }
992 p = skipwhite(p + 1);
993 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000994 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000995 else
996 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000997 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200998 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200999 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001000 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001001 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001002 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001003 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001004 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001005 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001006 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001007 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001008 clear_tv(&var2);
1009 return NULL;
1010 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001011 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001012 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001013 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001014 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001015 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001016
Bram Moolenaar8c711452005-01-14 21:53:12 +00001017 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001018 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001019 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001020 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001021 clear_tv(&var1);
1022 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001023 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001024 }
1025
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001026 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001027 ++p;
1028 }
1029
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001030 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001031 {
1032 if (len == -1)
1033 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001034 // "[key]": get key from "var1"
1035 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001036 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001037 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001038 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001039 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001040 }
1041 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001042 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001043
1044 // a NULL dict is equivalent with an empty dict
1045 if (lp->ll_tv->vval.v_dict == NULL)
1046 {
1047 lp->ll_tv->vval.v_dict = dict_alloc();
1048 if (lp->ll_tv->vval.v_dict == NULL)
1049 {
1050 clear_tv(&var1);
1051 return NULL;
1052 }
1053 ++lp->ll_tv->vval.v_dict->dv_refcount;
1054 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001055 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001056
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001057 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001058
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001059 // When assigning to a scope dictionary check that a function and
1060 // variable name is valid (only variable name unless it is l: or
1061 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001062 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001063 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001064 int prevval;
1065 int wrong;
1066
1067 if (len != -1)
1068 {
1069 prevval = key[len];
1070 key[len] = NUL;
1071 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001072 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001073 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001074 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1075 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001076 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar03290b82020-12-19 16:30:44 +01001077 || !valid_varname(key, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001078 if (len != -1)
1079 key[len] = prevval;
1080 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001081 {
1082 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001083 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001084 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001085 }
1086
Bram Moolenaar10c65862020-10-08 21:16:42 +02001087 if (lp->ll_valtype != NULL)
1088 // use the type of the member
1089 lp->ll_valtype = lp->ll_valtype->tt_member;
1090
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001091 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001092 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001093 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001094 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001095 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001096 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001097 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001098 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001099 return NULL;
1100 }
1101
Bram Moolenaar31b81602019-02-10 22:14:27 +01001102 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001103 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001104 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001105 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001106 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001107 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001108 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001109 }
1110 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001111 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001112 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001113 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001114 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001115 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001116 p = NULL;
1117 break;
1118 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001119 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001120 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001121 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1122 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001123 {
1124 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001125 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001126 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001127
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001128 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001129 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001130 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001131 else if (lp->ll_tv->v_type == VAR_BLOB)
1132 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001133 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1134
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001135 /*
1136 * Get the number and item for the only or first index of the List.
1137 */
1138 if (empty1)
1139 lp->ll_n1 = 0;
1140 else
1141 // is number or string
1142 lp->ll_n1 = (long)tv_get_number(&var1);
1143 clear_tv(&var1);
1144
1145 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001146 || lp->ll_n1 > bloblen
1147 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001148 {
1149 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001150 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001151 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001152 return NULL;
1153 }
1154 if (lp->ll_range && !lp->ll_empty2)
1155 {
1156 lp->ll_n2 = (long)tv_get_number(&var2);
1157 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001158 if (lp->ll_n2 < 0
1159 || lp->ll_n2 >= bloblen
1160 || lp->ll_n2 < lp->ll_n1)
1161 {
1162 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001163 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001164 return NULL;
1165 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001166 }
1167 lp->ll_blob = lp->ll_tv->vval.v_blob;
1168 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001169 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001170 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001171 else
1172 {
1173 /*
1174 * Get the number and item for the only or first index of the List.
1175 */
1176 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001177 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001178 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001179 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001180 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001181 clear_tv(&var1);
1182
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001183 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001184 lp->ll_list = lp->ll_tv->vval.v_list;
1185 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1186 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001187 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001188 if (lp->ll_n1 < 0)
1189 {
1190 lp->ll_n1 = 0;
1191 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1192 }
1193 }
1194 if (lp->ll_li == NULL)
1195 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001196 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001197 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001198 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001199 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001200 }
1201
Bram Moolenaar10c65862020-10-08 21:16:42 +02001202 if (lp->ll_valtype != NULL)
1203 // use the type of the member
1204 lp->ll_valtype = lp->ll_valtype->tt_member;
1205
Bram Moolenaar8c711452005-01-14 21:53:12 +00001206 /*
1207 * May need to find the item or absolute index for the second
1208 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001209 * When no index given: "lp->ll_empty2" is TRUE.
1210 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001211 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001212 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001213 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001214 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001215 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001216 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001217 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001218 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001219 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001220 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001221 {
1222 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001223 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001224 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001225 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001226 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001227 }
1228
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001229 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001230 if (lp->ll_n1 < 0)
1231 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1232 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001233 {
1234 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001235 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001236 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001237 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001238 }
1239
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001240 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001241 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001242 }
1243
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001244 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001245 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001246 return p;
1247}
1248
1249/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001250 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001251 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001252 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001253clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001254{
1255 vim_free(lp->ll_exp_name);
1256 vim_free(lp->ll_newkey);
1257}
1258
1259/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001260 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001261 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001262 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1263 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001264 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001265 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001266set_var_lval(
1267 lval_T *lp,
1268 char_u *endp,
1269 typval_T *rettv,
1270 int copy,
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001271 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar7454a062016-01-30 15:14:10 +01001272 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001273{
1274 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001275 listitem_T *ri;
1276 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001277
1278 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001279 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001280 cc = *endp;
1281 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001282 if (lp->ll_blob != NULL)
1283 {
1284 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001285
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001286 if (op != NULL && *op != '=')
1287 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001288 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001289 return;
1290 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001291 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001292 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001293
1294 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1295 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001296 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001297
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001298 if (lp->ll_empty2)
1299 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1300
1301 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001302 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001303 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001304 return;
1305 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001306 if (lp->ll_empty2)
1307 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001308
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001309 ir = 0;
1310 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1311 blob_set(lp->ll_blob, il,
1312 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001313 }
1314 else
1315 {
1316 val = (int)tv_get_number_chk(rettv, &error);
1317 if (!error)
1318 {
1319 garray_T *gap = &lp->ll_blob->bv_ga;
1320
1321 // Allow for appending a byte. Setting a byte beyond
1322 // the end is an error otherwise.
1323 if (lp->ll_n1 < gap->ga_len
1324 || (lp->ll_n1 == gap->ga_len
1325 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1326 {
1327 blob_set(lp->ll_blob, lp->ll_n1, val);
1328 if (lp->ll_n1 == gap->ga_len)
1329 ++gap->ga_len;
1330 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001331 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001332 }
1333 }
1334 }
1335 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001336 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001337 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001338
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001339 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar9937a052019-06-15 15:45:06 +02001340 {
1341 emsg(_(e_cannot_mod));
1342 *endp = cc;
1343 return;
1344 }
1345
Bram Moolenaarff697e62019-02-12 22:28:33 +01001346 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001347 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001348 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar79518e22017-02-17 16:31:35 +01001349 &tv, &di, TRUE, FALSE) == OK)
1350 {
1351 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001352 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1353 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001354 && tv_op(&tv, rettv, op) == OK)
1355 set_var(lp->ll_name, &tv, FALSE);
1356 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001357 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001358 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001359 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001360 {
1361 if (lp->ll_type != NULL
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001362 && check_typval_type(lp->ll_type, rettv, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001363 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001364 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001365 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001366 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001367 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001368 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001369 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001370 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001371 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001372 else if (lp->ll_range)
1373 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001374 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001375 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001376
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001377 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar9937a052019-06-15 15:45:06 +02001378 {
1379 emsg(_("E996: Cannot lock a range"));
1380 return;
1381 }
1382
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001383 /*
1384 * Check whether any of the list items is locked
1385 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001386 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001387 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001388 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001389 return;
1390 ri = ri->li_next;
1391 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1392 break;
1393 ll_li = ll_li->li_next;
1394 ++ll_n1;
1395 }
1396
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001397 /*
1398 * Assign the List values to the list items.
1399 */
1400 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001401 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001402 if (op != NULL && *op != '=')
1403 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1404 else
1405 {
1406 clear_tv(&lp->ll_li->li_tv);
1407 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1408 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001409 ri = ri->li_next;
1410 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1411 break;
1412 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001413 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001414 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001415 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001416 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001417 ri = NULL;
1418 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001419 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001420 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001421 lp->ll_li = lp->ll_li->li_next;
1422 ++lp->ll_n1;
1423 }
1424 if (ri != NULL)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001425 emsg(_(e_list_value_has_more_items_than_targets));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001426 else if (lp->ll_empty2
1427 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001428 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaar792f7862020-11-23 08:31:18 +01001429 emsg(_(e_list_value_does_not_have_enough_items));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001430 }
1431 else
1432 {
1433 /*
1434 * Assign to a List or Dictionary item.
1435 */
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01001436 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar9937a052019-06-15 15:45:06 +02001437 {
1438 emsg(_("E996: Cannot lock a list or dict"));
1439 return;
1440 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001441
1442 if (lp->ll_valtype != NULL
1443 && check_typval_type(lp->ll_valtype, rettv, 0) == FAIL)
1444 return;
1445
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001446 if (lp->ll_newkey != NULL)
1447 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001448 if (op != NULL && *op != '=')
1449 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001450 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001451 return;
1452 }
1453
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001454 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001455 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001456 if (di == NULL)
1457 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001458 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1459 {
1460 vim_free(di);
1461 return;
1462 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001463 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001464 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001465 else if (op != NULL && *op != '=')
1466 {
1467 tv_op(lp->ll_tv, rettv, op);
1468 return;
1469 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001470 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001471 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001472
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001473 /*
1474 * Assign the value to the variable or list item.
1475 */
1476 if (copy)
1477 copy_tv(rettv, lp->ll_tv);
1478 else
1479 {
1480 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001481 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001482 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001483 }
1484 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001485}
1486
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001487/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001488 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1489 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001490 * Returns OK or FAIL.
1491 */
1492 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001493tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001494{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001495 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001496 char_u numbuf[NUMBUFLEN];
1497 char_u *s;
1498
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001499 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001500 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001501 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001502 {
1503 switch (tv1->v_type)
1504 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001505 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001506 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001507 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001508 case VAR_DICT:
1509 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001510 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001511 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001512 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001513 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001514 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001515 break;
1516
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001517 case VAR_BLOB:
1518 if (*op != '+' || tv2->v_type != VAR_BLOB)
1519 break;
1520 // BLOB += BLOB
1521 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1522 {
1523 blob_T *b1 = tv1->vval.v_blob;
1524 blob_T *b2 = tv2->vval.v_blob;
1525 int i, len = blob_len(b2);
1526 for (i = 0; i < len; i++)
1527 ga_append(&b1->bv_ga, blob_get(b2, i));
1528 }
1529 return OK;
1530
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001531 case VAR_LIST:
1532 if (*op != '+' || tv2->v_type != VAR_LIST)
1533 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001534 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001535 if (tv2->vval.v_list != NULL)
1536 {
1537 if (tv1->vval.v_list == NULL)
1538 {
1539 tv1->vval.v_list = tv2->vval.v_list;
1540 ++tv1->vval.v_list->lv_refcount;
1541 }
1542 else
1543 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1544 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001545 return OK;
1546
1547 case VAR_NUMBER:
1548 case VAR_STRING:
1549 if (tv2->v_type == VAR_LIST)
1550 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001551 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001552 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001553 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001554 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001555#ifdef FEAT_FLOAT
1556 if (tv2->v_type == VAR_FLOAT)
1557 {
1558 float_T f = n;
1559
Bram Moolenaarff697e62019-02-12 22:28:33 +01001560 if (*op == '%')
1561 break;
1562 switch (*op)
1563 {
1564 case '+': f += tv2->vval.v_float; break;
1565 case '-': f -= tv2->vval.v_float; break;
1566 case '*': f *= tv2->vval.v_float; break;
1567 case '/': f /= tv2->vval.v_float; break;
1568 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001569 clear_tv(tv1);
1570 tv1->v_type = VAR_FLOAT;
1571 tv1->vval.v_float = f;
1572 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001573 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001574#endif
1575 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001576 switch (*op)
1577 {
1578 case '+': n += tv_get_number(tv2); break;
1579 case '-': n -= tv_get_number(tv2); break;
1580 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001581 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1582 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001583 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001584 clear_tv(tv1);
1585 tv1->v_type = VAR_NUMBER;
1586 tv1->vval.v_number = n;
1587 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001588 }
1589 else
1590 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001591 if (tv2->v_type == VAR_FLOAT)
1592 break;
1593
Bram Moolenaarff697e62019-02-12 22:28:33 +01001594 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001595 s = tv_get_string(tv1);
1596 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001597 clear_tv(tv1);
1598 tv1->v_type = VAR_STRING;
1599 tv1->vval.v_string = s;
1600 }
1601 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001602
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001603 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001604#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001605 {
1606 float_T f;
1607
Bram Moolenaarff697e62019-02-12 22:28:33 +01001608 if (*op == '%' || *op == '.'
1609 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001610 && tv2->v_type != VAR_NUMBER
1611 && tv2->v_type != VAR_STRING))
1612 break;
1613 if (tv2->v_type == VAR_FLOAT)
1614 f = tv2->vval.v_float;
1615 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001616 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001617 switch (*op)
1618 {
1619 case '+': tv1->vval.v_float += f; break;
1620 case '-': tv1->vval.v_float -= f; break;
1621 case '*': tv1->vval.v_float *= f; break;
1622 case '/': tv1->vval.v_float /= f; break;
1623 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001624 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001625#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001626 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001627 }
1628 }
1629
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001630 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001631 return FAIL;
1632}
1633
1634/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001635 * Evaluate the expression used in a ":for var in expr" command.
1636 * "arg" points to "var".
1637 * Set "*errp" to TRUE for an error, FALSE otherwise;
1638 * Return a pointer that holds the info. Null when there is an error.
1639 */
1640 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001641eval_for_line(
1642 char_u *arg,
1643 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001644 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001645 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001646{
Bram Moolenaar33570922005-01-25 22:26:29 +00001647 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001648 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001649 typval_T tv;
1650 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001651 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001652
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001653 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001654
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001655 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001656 if (fi == NULL)
1657 return NULL;
1658
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001659 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001660 if (expr == NULL)
1661 return fi;
1662
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001663 expr = skipwhite_and_linebreak(expr, evalarg);
1664 if (expr[0] != 'i' || expr[1] != 'n'
1665 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001666 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001667 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001668 return fi;
1669 }
1670
1671 if (skip)
1672 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001673 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1674 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001675 {
1676 *errp = FALSE;
1677 if (!skip)
1678 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001679 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001680 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001681 l = tv.vval.v_list;
1682 if (l == NULL)
1683 {
1684 // a null list is like an empty list: do nothing
1685 clear_tv(&tv);
1686 }
1687 else
1688 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001689 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001690 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001691
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001692 // No need to increment the refcount, it's already set for
1693 // the list being used in "tv".
1694 fi->fi_list = l;
1695 list_add_watch(l, &fi->fi_lw);
1696 fi->fi_lw.lw_item = l->lv_first;
1697 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001698 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001699 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001700 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001701 fi->fi_bi = 0;
1702 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001703 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001704 typval_T btv;
1705
1706 // Make a copy, so that the iteration still works when the
1707 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001708 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001709 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001710 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001711 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001712 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001713 else
1714 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001715 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001716 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001717 }
1718 }
1719 }
1720 if (skip)
1721 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001722 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001723
1724 return fi;
1725}
1726
1727/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001728 * Used when looping over a :for line, skip the "in expr" part.
1729 */
1730 void
1731skip_for_lines(void *fi_void, evalarg_T *evalarg)
1732{
1733 forinfo_T *fi = (forinfo_T *)fi_void;
1734 int i;
1735
1736 for (i = 0; i < fi->fi_break_count; ++i)
1737 eval_next_line(evalarg);
1738}
1739
1740/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001741 * Use the first item in a ":for" list. Advance to the next.
1742 * Assign the values to the variable (list). "arg" points to the first one.
1743 * Return TRUE when a valid item was found, FALSE when at end of list or
1744 * something wrong.
1745 */
1746 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001747next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001748{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001749 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001750 int result;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001751 int flag = in_vim9script() ? ASSIGN_NO_DECL : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001752 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001753
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001754 if (fi->fi_blob != NULL)
1755 {
1756 typval_T tv;
1757
1758 if (fi->fi_bi >= blob_len(fi->fi_blob))
1759 return FALSE;
1760 tv.v_type = VAR_NUMBER;
1761 tv.v_lock = VAR_FIXED;
1762 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1763 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001764 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001765 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001766 }
1767
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001768 item = fi->fi_lw.lw_item;
1769 if (item == NULL)
1770 result = FALSE;
1771 else
1772 {
1773 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001774 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001775 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001776 }
1777 return result;
1778}
1779
1780/*
1781 * Free the structure used to store info used by ":for".
1782 */
1783 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001784free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001785{
Bram Moolenaar33570922005-01-25 22:26:29 +00001786 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001787
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001788 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001789 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001790 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001791 list_unref(fi->fi_list);
1792 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001793 if (fi != NULL && fi->fi_blob != NULL)
1794 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001795 vim_free(fi);
1796}
1797
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001799set_context_for_expression(
1800 expand_T *xp,
1801 char_u *arg,
1802 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001804 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001806 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001808 if (cmdidx == CMD_let || cmdidx == CMD_var
1809 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001810 {
1811 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001812 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001813 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001814 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001815 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001816 {
1817 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001818 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001819 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001820 break;
1821 }
1822 return;
1823 }
1824 }
1825 else
1826 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1827 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 while ((xp->xp_pattern = vim_strpbrk(arg,
1829 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1830 {
1831 c = *xp->xp_pattern;
1832 if (c == '&')
1833 {
1834 c = xp->xp_pattern[1];
1835 if (c == '&')
1836 {
1837 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001838 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 }
1840 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001841 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001843 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1844 xp->xp_pattern += 2;
1845
1846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 }
1848 else if (c == '$')
1849 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001850 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 xp->xp_context = EXPAND_ENV_VARS;
1852 }
1853 else if (c == '=')
1854 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001855 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 xp->xp_context = EXPAND_EXPRESSION;
1857 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001858 else if (c == '#'
1859 && xp->xp_context == EXPAND_EXPRESSION)
1860 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001861 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001862 break;
1863 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001864 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 && xp->xp_context == EXPAND_FUNCTIONS
1866 && vim_strchr(xp->xp_pattern, '(') == NULL)
1867 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001868 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 break;
1870 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001871 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001873 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 {
1875 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1876 if (c == '\\' && xp->xp_pattern[1] != NUL)
1877 ++xp->xp_pattern;
1878 xp->xp_context = EXPAND_NOTHING;
1879 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001880 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001882 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1884 /* skip */ ;
1885 xp->xp_context = EXPAND_NOTHING;
1886 }
1887 else if (c == '|')
1888 {
1889 if (xp->xp_pattern[1] == '|')
1890 {
1891 ++xp->xp_pattern;
1892 xp->xp_context = EXPAND_EXPRESSION;
1893 }
1894 else
1895 xp->xp_context = EXPAND_COMMANDS;
1896 }
1897 else
1898 xp->xp_context = EXPAND_EXPRESSION;
1899 }
1900 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001901 // Doesn't look like something valid, expand as an expression
1902 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001903 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 arg = xp->xp_pattern;
1905 if (*arg != NUL)
1906 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1907 /* skip */ ;
1908 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01001909
1910 // ":exe one two" completes "two"
1911 if ((cmdidx == CMD_execute
1912 || cmdidx == CMD_echo
1913 || cmdidx == CMD_echon
1914 || cmdidx == CMD_echomsg)
1915 && xp->xp_context == EXPAND_EXPRESSION)
1916 {
1917 for (;;)
1918 {
1919 char_u *n = skiptowhite(arg);
1920
1921 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
1922 break;
1923 arg = skipwhite(n);
1924 }
1925 }
1926
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 xp->xp_pattern = arg;
1928}
1929
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001931 * Return TRUE if "pat" matches "text".
1932 * Does not use 'cpo' and always uses 'magic'.
1933 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001934 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001935pattern_match(char_u *pat, char_u *text, int ic)
1936{
1937 int matches = FALSE;
1938 char_u *save_cpo;
1939 regmatch_T regmatch;
1940
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001941 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001942 save_cpo = p_cpo;
1943 p_cpo = (char_u *)"";
1944 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1945 if (regmatch.regprog != NULL)
1946 {
1947 regmatch.rm_ic = ic;
1948 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1949 vim_regfree(regmatch.regprog);
1950 }
1951 p_cpo = save_cpo;
1952 return matches;
1953}
1954
1955/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001956 * Handle a name followed by "(". Both for just "name(arg)" and for
1957 * "expr->name(arg)".
1958 * Returns OK or FAIL.
1959 */
1960 static int
1961eval_func(
1962 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02001963 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001964 char_u *name,
1965 int name_len,
1966 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001967 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001968 typval_T *basetv) // "expr" for "expr->name(arg)"
1969{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001970 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001971 char_u *s = name;
1972 int len = name_len;
1973 partial_T *partial;
1974 int ret = OK;
1975
1976 if (!evaluate)
1977 check_vars(s, len);
1978
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001979 // If "s" is the name of a variable of type VAR_FUNC
1980 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001981 s = deref_func_name(s, &len, &partial, !evaluate);
1982
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001983 // Need to make a copy, in case evaluating the arguments makes
1984 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001985 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001986 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001987 ret = FAIL;
1988 else
1989 {
1990 funcexe_T funcexe;
1991
1992 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001993 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001994 funcexe.firstline = curwin->w_cursor.lnum;
1995 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001996 funcexe.evaluate = evaluate;
1997 funcexe.partial = partial;
1998 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02001999 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002000 }
2001 vim_free(s);
2002
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002003 // If evaluate is FALSE rettv->v_type was not set in
2004 // get_func_tv, but it's needed in handle_subscript() to parse
2005 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002006 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2007 {
2008 rettv->vval.v_string = NULL;
2009 rettv->v_type = VAR_FUNC;
2010 }
2011
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002012 // Stop the expression evaluation when immediately
2013 // aborting on error, or when an interrupt occurred or
2014 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002015 if (evaluate && aborting())
2016 {
2017 if (ret == OK)
2018 clear_tv(rettv);
2019 ret = FAIL;
2020 }
2021 return ret;
2022}
2023
2024/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002025 * Get the next line source line without advancing. But do skip over comment
2026 * lines.
2027 */
2028 static char_u *
2029getline_peek_skip_comments(evalarg_T *evalarg)
2030{
2031 for (;;)
2032 {
2033 char_u *next = getline_peek(evalarg->eval_getline,
2034 evalarg->eval_cookie);
2035 char_u *p;
2036
2037 if (next == NULL)
2038 break;
2039 p = skipwhite(next);
2040 if (*p != NUL && !vim9_comment_start(p))
2041 return next;
2042 (void)eval_next_line(evalarg);
2043 }
2044 return NULL;
2045}
2046
2047/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002048 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2049 * comment) and there is a next line, return the next line (skipping blanks)
2050 * and set "getnext".
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002051 * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
2052 * "arg" must point somewhere inside a line, not at the start.
2053 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002054 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002055eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2056{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002057 char_u *p = skipwhite(arg);
2058
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002059 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002060 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002061 && evalarg != NULL
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002062 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL)
Bram Moolenaar9d489562020-07-30 20:08:50 +02002063 && (*p == NUL || (VIM_ISWHITE(p[-1]) && vim9_comment_start(p))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002064 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002065 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002066
2067 if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002068 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002069 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002070 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002071
Bram Moolenaar9d489562020-07-30 20:08:50 +02002072 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002073 {
2074 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002075 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002076 }
2077 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002078 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002079}
2080
2081/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002082 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002083 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002084 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002085eval_next_line(evalarg_T *evalarg)
2086{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002087 garray_T *gap = &evalarg->eval_ga;
2088 char_u *line;
2089
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002090 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002091 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2092 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002093 else
2094 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002095 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002096 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2097 {
2098 // Going to concatenate the lines after parsing.
2099 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2100 ++gap->ga_len;
2101 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002102 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002103 {
2104 vim_free(evalarg->eval_tofree);
2105 evalarg->eval_tofree = line;
2106 }
2107 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002108}
2109
Bram Moolenaare6b53242020-07-01 17:28:33 +02002110/*
2111 * Call eval_next_non_blank() and get the next line if needed.
2112 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002113 char_u *
2114skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2115{
2116 int getnext;
2117 char_u *p = skipwhite(arg);
2118
Bram Moolenaar962d7212020-07-04 14:15:00 +02002119 if (evalarg == NULL)
2120 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002121 eval_next_non_blank(p, evalarg, &getnext);
2122 if (getnext)
2123 return eval_next_line(evalarg);
2124 return p;
2125}
2126
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002127/*
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002128 * After using "evalarg" filled from "eap": free the memory.
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002129 */
2130 void
2131clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
2132{
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002133 if (evalarg != NULL)
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002134 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002135 if (evalarg->eval_tofree != NULL)
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002136 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002137 if (eap != NULL)
2138 {
2139 // We may need to keep the original command line, e.g. for
2140 // ":let" it has the variable names. But we may also need the
2141 // new one, "nextcmd" points into it. Keep both.
2142 vim_free(eap->cmdline_tofree);
2143 eap->cmdline_tofree = *eap->cmdlinep;
2144 *eap->cmdlinep = evalarg->eval_tofree;
2145 }
2146 else
2147 vim_free(evalarg->eval_tofree);
2148 evalarg->eval_tofree = NULL;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002149 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002150
2151 vim_free(evalarg->eval_tofree_lambda);
2152 evalarg->eval_tofree_lambda = NULL;
Bram Moolenaarfaf86262020-06-27 23:07:36 +02002153 }
2154}
2155
2156/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002158 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2160 */
2161
2162/*
2163 * Handle zero level expression.
2164 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002165 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002166 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002167 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 * Return OK or FAIL.
2169 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002170 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002171eval0(
2172 char_u *arg,
2173 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002174 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002175 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176{
2177 int ret;
2178 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002179 int did_emsg_before = did_emsg;
2180 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002181 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182
2183 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002184 ret = eval1(&p, rettv, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002185 p = skipwhite(p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002186
Bram Moolenaarfaac4102020-04-20 17:46:14 +02002187 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 {
2189 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002190 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 /*
2192 * Report the invalid expression unless the expression evaluation has
2193 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002194 * exception, or we already gave a more specific error.
2195 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002197 if (!aborting()
2198 && did_emsg == did_emsg_before
2199 && called_emsg == called_emsg_before
2200 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002201 semsg(_(e_invexpr2), arg);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002202
2203 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002204 // a next command to avoid more errors, unless "|" is following, which
2205 // could only be a command separator.
2206 if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2207 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002208 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002210
2211 if (eap != NULL)
2212 eap->nextcmd = check_nextcmd(p);
2213
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 return ret;
2215}
2216
2217/*
2218 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002219 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002220 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 *
2222 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002223 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002225 * Note: "rettv.v_lock" is not set.
2226 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 * Return OK or FAIL.
2228 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002229 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002230eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002232 char_u *p;
2233 int getnext;
2234
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002235 CLEAR_POINTER(rettv);
2236
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 /*
2238 * Get the first variable.
2239 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002240 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 return FAIL;
2242
Bram Moolenaar793648f2020-06-26 21:28:25 +02002243 p = eval_next_non_blank(*arg, evalarg, &getnext);
2244 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002246 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002247 int result;
2248 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002249 evalarg_T *evalarg_used = evalarg;
2250 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002251 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002252 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002253 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002254
2255 if (evalarg == NULL)
2256 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002257 CLEAR_FIELD(local_evalarg);
2258 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002259 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002260 orig_flags = evalarg_used->eval_flags;
2261 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002262
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002263 if (getnext)
2264 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002265 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002266 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002267 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002268 {
2269 error_white_both(p, 1);
2270 clear_tv(rettv);
2271 return FAIL;
2272 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002273 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002274 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002275
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002277 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002279 int error = FALSE;
2280
Bram Moolenaar13106602020-10-04 16:06:05 +02002281 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002282 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002283 else if (vim9script)
2284 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002285 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002287 if (error || !op_falsy || !result)
2288 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002289 if (error)
2290 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 }
2292
2293 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002294 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002296 if (op_falsy)
2297 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002298 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002299 {
2300 error_white_both(p, 1);
2301 clear_tv(rettv);
2302 return FAIL;
2303 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002304 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002305 evalarg_used->eval_flags = (op_falsy ? !result : result)
2306 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2307 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002308 {
2309 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002311 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002312 if (!op_falsy || !result)
2313 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002315 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002317 /*
2318 * Check for the ":".
2319 */
2320 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2321 if (*p != ':')
2322 {
2323 emsg(_(e_missing_colon));
2324 if (evaluate && result)
2325 clear_tv(rettv);
2326 evalarg_used->eval_flags = orig_flags;
2327 return FAIL;
2328 }
2329 if (getnext)
2330 *arg = eval_next_line(evalarg_used);
2331 else
2332 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002333 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002334 {
2335 error_white_both(p, 1);
2336 clear_tv(rettv);
2337 evalarg_used->eval_flags = orig_flags;
2338 return FAIL;
2339 }
2340 *arg = p;
2341 }
2342
2343 /*
2344 * Get the third variable. Recursive!
2345 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002346 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002347 {
2348 error_white_both(p, 1);
2349 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002350 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002351 return FAIL;
2352 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002353 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2354 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002355 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002356 if (eval1(arg, &var2, evalarg_used) == FAIL)
2357 {
2358 if (evaluate && result)
2359 clear_tv(rettv);
2360 evalarg_used->eval_flags = orig_flags;
2361 return FAIL;
2362 }
2363 if (evaluate && !result)
2364 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002366
2367 if (evalarg == NULL)
2368 clear_evalarg(&local_evalarg, NULL);
2369 else
2370 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 }
2372
2373 return OK;
2374}
2375
2376/*
2377 * Handle first level expression:
2378 * expr2 || expr2 || expr2 logical OR
2379 *
2380 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002381 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 *
2383 * Return OK or FAIL.
2384 */
2385 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002386eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002388 char_u *p;
2389 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390
2391 /*
2392 * Get the first variable.
2393 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002394 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 return FAIL;
2396
2397 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002398 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002400 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002401 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002403 evalarg_T *evalarg_used = evalarg;
2404 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002405 int evaluate;
2406 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002407 long result = FALSE;
2408 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002409 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002410 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002411
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002412 if (evalarg == NULL)
2413 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002414 CLEAR_FIELD(local_evalarg);
2415 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002416 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002417 orig_flags = evalarg_used->eval_flags;
2418 evaluate = orig_flags & EVAL_EVALUATE;
2419 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002420 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002421 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002422 result = tv_get_bool_chk(rettv, &error);
2423 else if (tv_get_number_chk(rettv, &error) != 0)
2424 result = TRUE;
2425 clear_tv(rettv);
2426 if (error)
2427 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 }
2429
2430 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002431 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002433 while (p[0] == '|' && p[1] == '|')
2434 {
2435 if (getnext)
2436 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002437 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002438 {
2439 if (evaluate && in_vim9script() && !VIM_ISWHITE(p[-1]))
2440 {
2441 error_white_both(p, 2);
2442 clear_tv(rettv);
2443 return FAIL;
2444 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002445 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002446 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002447
2448 /*
2449 * Get the second variable.
2450 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002451 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2452 {
2453 error_white_both(p, 2);
2454 clear_tv(rettv);
2455 return FAIL;
2456 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002457 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2458 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002459 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002460 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002461 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002462
2463 /*
2464 * Compute the result.
2465 */
2466 if (evaluate && !result)
2467 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002468 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002469 result = tv_get_bool_chk(&var2, &error);
2470 else if (tv_get_number_chk(&var2, &error) != 0)
2471 result = TRUE;
2472 clear_tv(&var2);
2473 if (error)
2474 return FAIL;
2475 }
2476 if (evaluate)
2477 {
2478 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002479 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002480 rettv->v_type = VAR_BOOL;
2481 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002482 }
2483 else
2484 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002485 rettv->v_type = VAR_NUMBER;
2486 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002487 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002488 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002489
2490 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002492
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002493 if (evalarg == NULL)
2494 clear_evalarg(&local_evalarg, NULL);
2495 else
2496 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 }
2498
2499 return OK;
2500}
2501
2502/*
2503 * Handle second level expression:
2504 * expr3 && expr3 && expr3 logical AND
2505 *
2506 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002507 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 *
2509 * Return OK or FAIL.
2510 */
2511 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002512eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002514 char_u *p;
2515 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516
2517 /*
2518 * Get the first variable.
2519 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002520 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 return FAIL;
2522
2523 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002524 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002526 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002527 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002529 evalarg_T *evalarg_used = evalarg;
2530 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002531 int orig_flags;
2532 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002533 long result = TRUE;
2534 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002535 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002536 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002537
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002538 if (evalarg == NULL)
2539 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002540 CLEAR_FIELD(local_evalarg);
2541 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002542 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002543 orig_flags = evalarg_used->eval_flags;
2544 evaluate = orig_flags & EVAL_EVALUATE;
2545 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002546 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002547 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002548 result = tv_get_bool_chk(rettv, &error);
2549 else if (tv_get_number_chk(rettv, &error) == 0)
2550 result = FALSE;
2551 clear_tv(rettv);
2552 if (error)
2553 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 }
2555
2556 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002557 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002559 while (p[0] == '&' && p[1] == '&')
2560 {
2561 if (getnext)
2562 *arg = eval_next_line(evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002563 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002564 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002565 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002566 {
2567 error_white_both(p, 2);
2568 clear_tv(rettv);
2569 return FAIL;
2570 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002571 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002572 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002573
2574 /*
2575 * Get the second variable.
2576 */
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002577 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[2]))
2578 {
2579 error_white_both(p, 2);
2580 clear_tv(rettv);
2581 return FAIL;
2582 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002583 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2584 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002585 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002586 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002587 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002588 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002589
2590 /*
2591 * Compute the result.
2592 */
2593 if (evaluate && result)
2594 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002595 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002596 result = tv_get_bool_chk(&var2, &error);
2597 else if (tv_get_number_chk(&var2, &error) == 0)
2598 result = FALSE;
2599 clear_tv(&var2);
2600 if (error)
2601 return FAIL;
2602 }
2603 if (evaluate)
2604 {
2605 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002606 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002607 rettv->v_type = VAR_BOOL;
2608 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002609 }
2610 else
2611 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002612 rettv->v_type = VAR_NUMBER;
2613 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002614 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002615 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002616
2617 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002619
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002620 if (evalarg == NULL)
2621 clear_evalarg(&local_evalarg, NULL);
2622 else
2623 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 }
2625
2626 return OK;
2627}
2628
2629/*
2630 * Handle third level expression:
2631 * var1 == var2
2632 * var1 =~ var2
2633 * var1 != var2
2634 * var1 !~ var2
2635 * var1 > var2
2636 * var1 >= var2
2637 * var1 < var2
2638 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002639 * var1 is var2
2640 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 *
2642 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002643 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 *
2645 * Return OK or FAIL.
2646 */
2647 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002648eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002651 int getnext;
Bram Moolenaar87396072019-12-31 22:36:18 +01002652 exptype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002654 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655
2656 /*
2657 * Get the first variable.
2658 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002659 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 return FAIL;
2661
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002662 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002663 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664
2665 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002666 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002668 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002670 typval_T var2;
2671 int ic;
2672 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002673 int evaluate = evalarg == NULL
2674 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002675
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002676 if (getnext)
2677 *arg = eval_next_line(evalarg);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002678 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2679 {
2680 error_white_both(p, len);
2681 clear_tv(rettv);
2682 return FAIL;
2683 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002684
Bram Moolenaar696ba232020-07-29 21:20:41 +02002685 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2686 {
2687 semsg(_(e_invexpr2), p);
2688 clear_tv(rettv);
2689 return FAIL;
2690 }
2691
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002692 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 if (p[len] == '?')
2694 {
2695 ic = TRUE;
2696 ++len;
2697 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002698 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 else if (p[len] == '#')
2700 {
2701 ic = FALSE;
2702 ++len;
2703 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002704 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002706 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707
2708 /*
2709 * Get the second variable.
2710 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002711 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2712 {
2713 error_white_both(p, 1);
2714 clear_tv(rettv);
2715 return FAIL;
2716 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002717 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002718 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002720 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 return FAIL;
2722 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002723 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002724 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002725 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01002726
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002727 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002728 {
2729 ret = FAIL;
2730 clear_tv(rettv);
2731 }
2732 else
2733 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002734 clear_tv(&var2);
2735 return ret;
2736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 }
2738
2739 return OK;
2740}
2741
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002742/*
2743 * Make a copy of blob "tv1" and append blob "tv2".
2744 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002745 void
2746eval_addblob(typval_T *tv1, typval_T *tv2)
2747{
2748 blob_T *b1 = tv1->vval.v_blob;
2749 blob_T *b2 = tv2->vval.v_blob;
2750 blob_T *b = blob_alloc();
2751 int i;
2752
2753 if (b != NULL)
2754 {
2755 for (i = 0; i < blob_len(b1); i++)
2756 ga_append(&b->bv_ga, blob_get(b1, i));
2757 for (i = 0; i < blob_len(b2); i++)
2758 ga_append(&b->bv_ga, blob_get(b2, i));
2759
2760 clear_tv(tv1);
2761 rettv_blob_set(tv1, b);
2762 }
2763}
2764
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002765/*
2766 * Make a copy of list "tv1" and append list "tv2".
2767 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002768 int
2769eval_addlist(typval_T *tv1, typval_T *tv2)
2770{
2771 typval_T var3;
2772
2773 // concatenate Lists
2774 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2775 {
2776 clear_tv(tv1);
2777 clear_tv(tv2);
2778 return FAIL;
2779 }
2780 clear_tv(tv1);
2781 *tv1 = var3;
2782 return OK;
2783}
2784
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785/*
2786 * Handle fourth level expression:
2787 * + number addition
2788 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002789 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002790 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 *
2792 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002793 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 *
2795 * Return OK or FAIL.
2796 */
2797 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002798eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 /*
2801 * Get the first variable.
2802 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002803 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 return FAIL;
2805
2806 /*
2807 * Repeat computing, until no '+', '-' or '.' is following.
2808 */
2809 for (;;)
2810 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002811 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002812 int getnext;
2813 char_u *p;
2814 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002815 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002816 int concat;
2817 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02002818 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002819
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002820 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarce2c5442020-11-28 21:21:17 +01002821 // "+=" and "-=" are assignment
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002822 p = eval_next_non_blank(*arg, evalarg, &getnext);
2823 op = *p;
2824 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaarce2c5442020-11-28 21:21:17 +01002825 if ((op != '+' && op != '-' && !concat) || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02002827
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002828 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002829 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002830 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002831 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002832 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002833 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02002834 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002835 {
Bram Moolenaarb4caa162020-08-05 11:36:52 +02002836 error_white_both(p, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002837 clear_tv(rettv);
2838 return FAIL;
2839 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002840 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002841 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002842 if ((op != '+' || (rettv->v_type != VAR_LIST
2843 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002844#ifdef FEAT_FLOAT
2845 && (op == '.' || rettv->v_type != VAR_FLOAT)
2846#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002847 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002848 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002849 int error = FALSE;
2850
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002851 // For "list + ...", an illegal use of the first operand as
2852 // a number cannot be determined before evaluating the 2nd
2853 // operand: if this is also a list, all is ok.
2854 // For "something . ...", "something - ..." or "non-list + ...",
2855 // we know that the first operand needs to be a string or number
2856 // without evaluating the 2nd operand. So check before to avoid
2857 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02002858 if (op != '.')
2859 tv_get_number_chk(rettv, &error);
2860 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002861 {
2862 clear_tv(rettv);
2863 return FAIL;
2864 }
2865 }
2866
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 /*
2868 * Get the second variable.
2869 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02002870 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02002871 {
2872 error_white_both(p, oplen);
2873 clear_tv(rettv);
2874 return FAIL;
2875 }
2876 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Bram Moolenaar2e086612020-08-25 22:37:48 +02002877 if (eval6(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002879 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 return FAIL;
2881 }
2882
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002883 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 {
2885 /*
2886 * Compute the result.
2887 */
2888 if (op == '.')
2889 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002890 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2891 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002892 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002893
Bram Moolenaar2e086612020-08-25 22:37:48 +02002894 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002895 || var2.v_type == VAR_CHANNEL
2896 || var2.v_type == VAR_JOB))
2897 emsg(_(e_inval_string));
2898#ifdef FEAT_FLOAT
Bram Moolenaar2e086612020-08-25 22:37:48 +02002899 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002900 {
2901 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
2902 var2.vval.v_float);
2903 s2 = buf2;
2904 }
2905#endif
2906 else
2907 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002908 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002909 {
2910 clear_tv(rettv);
2911 clear_tv(&var2);
2912 return FAIL;
2913 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002914 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002915 clear_tv(rettv);
2916 rettv->v_type = VAR_STRING;
2917 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002919 else if (op == '+' && rettv->v_type == VAR_BLOB
2920 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002921 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002922 else if (op == '+' && rettv->v_type == VAR_LIST
2923 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002924 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002925 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002926 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 else
2929 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002930 int error = FALSE;
2931 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002932#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002933 float_T f1 = 0, f2 = 0;
2934
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002935 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002936 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002937 f1 = rettv->vval.v_float;
2938 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002939 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002940 else
2941#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002942 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002943 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002944 if (error)
2945 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002946 // This can only happen for "list + non-list". For
2947 // "non-list + ..." or "something - ...", we returned
2948 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002949 clear_tv(rettv);
2950 return FAIL;
2951 }
2952#ifdef FEAT_FLOAT
2953 if (var2.v_type == VAR_FLOAT)
2954 f1 = n1;
2955#endif
2956 }
2957#ifdef FEAT_FLOAT
2958 if (var2.v_type == VAR_FLOAT)
2959 {
2960 f2 = var2.vval.v_float;
2961 n2 = 0;
2962 }
2963 else
2964#endif
2965 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002966 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002967 if (error)
2968 {
2969 clear_tv(rettv);
2970 clear_tv(&var2);
2971 return FAIL;
2972 }
2973#ifdef FEAT_FLOAT
2974 if (rettv->v_type == VAR_FLOAT)
2975 f2 = n2;
2976#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002977 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002978 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002979
2980#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002981 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002982 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2983 {
2984 if (op == '+')
2985 f1 = f1 + f2;
2986 else
2987 f1 = f1 - f2;
2988 rettv->v_type = VAR_FLOAT;
2989 rettv->vval.v_float = f1;
2990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002992#endif
2993 {
2994 if (op == '+')
2995 n1 = n1 + n2;
2996 else
2997 n1 = n1 - n2;
2998 rettv->v_type = VAR_NUMBER;
2999 rettv->vval.v_number = n1;
3000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003002 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 }
3004 }
3005 return OK;
3006}
3007
3008/*
3009 * Handle fifth level expression:
3010 * * number multiplication
3011 * / number division
3012 * % number modulo
3013 *
3014 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003015 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 *
3017 * Return OK or FAIL.
3018 */
3019 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003020eval6(
3021 char_u **arg,
3022 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003023 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003024 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003026#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003027 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003028#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029
3030 /*
3031 * Get the first variable.
3032 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003033 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 return FAIL;
3035
3036 /*
3037 * Repeat computing, until no '*', '/' or '%' is following.
3038 */
3039 for (;;)
3040 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003041 int evaluate;
3042 int getnext;
3043 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003044 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003045 int op;
3046 varnumber_T n1, n2;
3047#ifdef FEAT_FLOAT
3048 float_T f1, f2;
3049#endif
3050 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003051
Bram Moolenaar9d489562020-07-30 20:08:50 +02003052 p = eval_next_non_blank(*arg, evalarg, &getnext);
3053 op = *p;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02003054 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003056
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003057 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003058 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02003059 *arg = eval_next_line(evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003060 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003061 {
3062 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3063 {
3064 error_white_both(p, 1);
3065 clear_tv(rettv);
3066 return FAIL;
3067 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003068 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003071#ifdef FEAT_FLOAT
3072 f1 = 0;
3073 f2 = 0;
3074#endif
3075 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003076 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003078#ifdef FEAT_FLOAT
3079 if (rettv->v_type == VAR_FLOAT)
3080 {
3081 f1 = rettv->vval.v_float;
3082 use_float = TRUE;
3083 n1 = 0;
3084 }
3085 else
3086#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003087 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003088 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003089 if (error)
3090 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 }
3092 else
3093 n1 = 0;
3094
3095 /*
3096 * Get the second variable.
3097 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003098 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3099 {
3100 error_white_both(p, 1);
3101 clear_tv(rettv);
3102 return FAIL;
3103 }
3104 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003105 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 return FAIL;
3107
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003108 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003110#ifdef FEAT_FLOAT
3111 if (var2.v_type == VAR_FLOAT)
3112 {
3113 if (!use_float)
3114 {
3115 f1 = n1;
3116 use_float = TRUE;
3117 }
3118 f2 = var2.vval.v_float;
3119 n2 = 0;
3120 }
3121 else
3122#endif
3123 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003124 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003125 clear_tv(&var2);
3126 if (error)
3127 return FAIL;
3128#ifdef FEAT_FLOAT
3129 if (use_float)
3130 f2 = n2;
3131#endif
3132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133
3134 /*
3135 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003136 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003138#ifdef FEAT_FLOAT
3139 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003141 if (op == '*')
3142 f1 = f1 * f2;
3143 else if (op == '/')
3144 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003145# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003146 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003147 if (f2 == 0.0)
3148 {
3149 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003150 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003151 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003152 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003153 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003154 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003155 }
3156 else
3157 f1 = f1 / f2;
3158# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003159 // We rely on the floating point library to handle divide
3160 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003161 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003162# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003165 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003166 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003167 return FAIL;
3168 }
3169 rettv->v_type = VAR_FLOAT;
3170 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 }
3172 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003173#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003175 if (op == '*')
3176 n1 = n1 * n2;
3177 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01003178 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01003180 n1 = num_modulus(n1, n2);
3181
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003182 rettv->v_type = VAR_NUMBER;
3183 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 }
3186 }
3187
3188 return OK;
3189}
3190
3191/*
3192 * Handle sixth level expression:
3193 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003194 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003195 * "string" string constant
3196 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 * &option-name option value
3198 * @r register contents
3199 * identifier variable value
3200 * function() function call
3201 * $VAR environment variable
3202 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003203 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003204 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003205 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003206 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 *
3208 * Also handle:
3209 * ! in front logical NOT
3210 * - in front unary minus
3211 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003212 * trailing [] subscript in String or List
3213 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003214 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 *
3216 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003217 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 *
3219 * Return OK or FAIL.
3220 */
3221 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003222eval7(
3223 char_u **arg,
3224 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003225 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003226 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003228 int evaluate = evalarg != NULL
3229 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 int len;
3231 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 char_u *start_leader, *end_leader;
3233 int ret = OK;
3234 char_u *alias;
3235
3236 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003237 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003238 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003240 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241
3242 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003243 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 */
3245 start_leader = *arg;
3246 while (**arg == '!' || **arg == '-' || **arg == '+')
3247 *arg = skipwhite(*arg + 1);
3248 end_leader = *arg;
3249
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003250 if (**arg == '.' && (!isdigit(*(*arg + 1))
3251#ifdef FEAT_FLOAT
3252 || current_sctx.sc_version < 2
3253#endif
3254 ))
3255 {
3256 semsg(_(e_invexpr2), *arg);
3257 ++*arg;
3258 return FAIL;
3259 }
3260
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 switch (**arg)
3262 {
3263 /*
3264 * Number constant.
3265 */
3266 case '0':
3267 case '1':
3268 case '2':
3269 case '3':
3270 case '4':
3271 case '5':
3272 case '6':
3273 case '7':
3274 case '8':
3275 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003276 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003277
3278 // Apply prefixed "-" and "+" now. Matters especially when
3279 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003280 if (ret == OK && evaluate && end_leader > start_leader
3281 && rettv->v_type != VAR_BLOB)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003282 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 break;
3284
3285 /*
3286 * String constant: "string".
3287 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003288 case '"': ret = eval_string(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 break;
3290
3291 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003292 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003294 case '\'': ret = eval_lit_string(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003295 break;
3296
3297 /*
3298 * List: [expr, expr]
3299 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003300 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 break;
3302
3303 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003304 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003305 */
Bram Moolenaare0de1712020-12-02 17:36:54 +01003306 case '#': if (!in_vim9script() && (*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003307 {
3308 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003309 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003310 }
3311 else
3312 ret = NOTDONE;
3313 break;
3314
3315 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003316 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003317 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003318 */
Bram Moolenaarc2ca9352020-11-25 21:30:11 +01003319 case '{': ret = get_lambda_tv(arg, rettv, in_vim9script(), evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003320 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003321 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003322 break;
3323
3324 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003325 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003327 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 break;
3329
3330 /*
3331 * Environment variable: $VAR.
3332 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003333 case '$': ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 break;
3335
3336 /*
3337 * Register contents: @r.
3338 */
3339 case '@': ++*arg;
3340 if (evaluate)
3341 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003342 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02003343 rettv->vval.v_string = get_reg_contents(**arg,
3344 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 }
3346 if (**arg != NUL)
3347 ++*arg;
3348 break;
3349
3350 /*
3351 * nested expression: (expression).
3352 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003353 case '(': {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003354 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003355 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003356
Bram Moolenaar9215f012020-06-27 21:18:00 +02003357 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003358 if (**arg == ')')
3359 ++*arg;
3360 else if (ret == OK)
3361 {
3362 emsg(_(e_missing_close));
3363 clear_tv(rettv);
3364 ret = FAIL;
3365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 }
3367 break;
3368
Bram Moolenaar8c711452005-01-14 21:53:12 +00003369 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 break;
3371 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003372
3373 if (ret == NOTDONE)
3374 {
3375 /*
3376 * Must be a variable or function name.
3377 * Can also be a curly-braces kind of name: {expr}.
3378 */
3379 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003380 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003381 if (alias != NULL)
3382 s = alias;
3383
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003384 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003385 ret = FAIL;
3386 else
3387 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003388 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
3389
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003390 if ((in_vim9script() ? **arg : *skipwhite(*arg)) == '(')
3391 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003392 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003393 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02003394 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02003395 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02003396 else if (flags & EVAL_CONSTANT)
3397 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003398 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003399 {
3400 // get the value of "true", "false" or a variable
3401 if (len == 4 && in_vim9script() && STRNCMP(s, "true", 4) == 0)
3402 {
3403 rettv->v_type = VAR_BOOL;
3404 rettv->vval.v_number = VVAL_TRUE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003405 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003406 }
3407 else if (len == 5 && in_vim9script()
3408 && STRNCMP(s, "false", 4) == 0)
3409 {
3410 rettv->v_type = VAR_BOOL;
3411 rettv->vval.v_number = VVAL_FALSE;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003412 ret = OK;
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02003413 }
3414 else
3415 ret = eval_variable(s, len, rettv, NULL, TRUE, FALSE);
3416 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003417 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003418 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003419 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003420 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003421 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003422 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003423 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003424 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003425 }
3426
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003427 // Handle following '[', '(' and '.' for expr[expr], expr.name,
3428 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003429 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003430 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431
3432 /*
3433 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3434 */
3435 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003436 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003437 return ret;
3438}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003439
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003440/*
3441 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003442 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003443 * Adjusts "end_leaderp" until it is at "start_leader".
3444 */
3445 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003446eval7_leader(
3447 typval_T *rettv,
3448 int numeric_only,
3449 char_u *start_leader,
3450 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003451{
3452 char_u *end_leader = *end_leaderp;
3453 int ret = OK;
3454 int error = FALSE;
3455 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003456 vartype_T type = rettv->v_type;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003457#ifdef FEAT_FLOAT
3458 float_T f = 0.0;
3459
3460 if (rettv->v_type == VAR_FLOAT)
3461 f = rettv->vval.v_float;
3462 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003463#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003464 {
3465 while (VIM_ISWHITE(end_leader[-1]))
3466 --end_leader;
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003467 if (in_vim9script() && end_leader[-1] == '!')
3468 val = tv2bool(rettv);
3469 else
3470 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02003471 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003472 if (error)
3473 {
3474 clear_tv(rettv);
3475 ret = FAIL;
3476 }
3477 else
3478 {
3479 while (end_leader > start_leader)
3480 {
3481 --end_leader;
3482 if (*end_leader == '!')
3483 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003484 if (numeric_only)
3485 {
3486 ++end_leader;
3487 break;
3488 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003489#ifdef FEAT_FLOAT
3490 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01003491 {
3492 if (in_vim9script())
3493 {
3494 rettv->v_type = VAR_BOOL;
3495 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
3496 }
3497 else
3498 f = !f;
3499 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003500 else
3501#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003502 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003503 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003504 type = VAR_BOOL;
3505 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003506 }
3507 else if (*end_leader == '-')
3508 {
3509#ifdef FEAT_FLOAT
3510 if (rettv->v_type == VAR_FLOAT)
3511 f = -f;
3512 else
3513#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003514 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003515 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003516 type = VAR_NUMBER;
3517 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003518 }
3519 }
3520#ifdef FEAT_FLOAT
3521 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003523 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003524 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003526 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003527#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003528 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003529 clear_tv(rettv);
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02003530 if (in_vim9script())
3531 rettv->v_type = type;
3532 else
3533 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003534 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003537 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 return ret;
3539}
3540
3541/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003542 * Call the function referred to in "rettv".
3543 */
3544 static int
3545call_func_rettv(
3546 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003547 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003548 typval_T *rettv,
3549 int evaluate,
3550 dict_T *selfdict,
3551 typval_T *basetv)
3552{
3553 partial_T *pt = NULL;
3554 funcexe_T funcexe;
3555 typval_T functv;
3556 char_u *s;
3557 int ret;
3558
3559 // need to copy the funcref so that we can clear rettv
3560 if (evaluate)
3561 {
3562 functv = *rettv;
3563 rettv->v_type = VAR_UNKNOWN;
3564
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003565 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003566 if (functv.v_type == VAR_PARTIAL)
3567 {
3568 pt = functv.vval.v_partial;
3569 s = partial_name(pt);
3570 }
3571 else
3572 s = functv.vval.v_string;
3573 }
3574 else
3575 s = (char_u *)"";
3576
Bram Moolenaara80faa82020-04-12 19:37:17 +02003577 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003578 funcexe.firstline = curwin->w_cursor.lnum;
3579 funcexe.lastline = curwin->w_cursor.lnum;
3580 funcexe.evaluate = evaluate;
3581 funcexe.partial = pt;
3582 funcexe.selfdict = selfdict;
3583 funcexe.basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003584 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003585
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003586 // Clear the funcref afterwards, so that deleting it while
3587 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003588 if (evaluate)
3589 clear_tv(&functv);
3590
3591 return ret;
3592}
3593
3594/*
3595 * Evaluate "->method()".
3596 * "*arg" points to the '-'.
3597 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3598 */
3599 static int
3600eval_lambda(
3601 char_u **arg,
3602 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003603 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003604 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003605{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003606 int evaluate = evalarg != NULL
3607 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003608 typval_T base = *rettv;
3609 int ret;
3610
3611 // Skip over the ->.
3612 *arg += 2;
3613 rettv->v_type = VAR_UNKNOWN;
3614
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003615 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003616 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003617 return FAIL;
3618 else if (**arg != '(')
3619 {
3620 if (verbose)
3621 {
3622 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003623 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003624 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003625 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003626 }
3627 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003628 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003629 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003630 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003631 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02003632
3633 // Clear the funcref afterwards, so that deleting it while
3634 // evaluating the arguments is possible (see test55).
3635 if (evaluate)
3636 clear_tv(&base);
3637
3638 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003639}
3640
3641/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003642 * Evaluate "->method()".
3643 * "*arg" points to the '-'.
3644 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3645 */
3646 static int
3647eval_method(
3648 char_u **arg,
3649 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02003650 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003651 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003652{
3653 char_u *name;
3654 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003655 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003656 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003657 int ret;
Bram Moolenaare6b53242020-07-01 17:28:33 +02003658 int evaluate = evalarg != NULL
3659 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003660
3661 // Skip over the ->.
3662 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003663 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003664
Bram Moolenaarac92e252019-08-03 21:58:38 +02003665 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003666 len = get_name_len(arg, &alias, evaluate, TRUE);
3667 if (alias != NULL)
3668 name = alias;
3669
3670 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003671 {
3672 if (verbose)
3673 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003674 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003675 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003676 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003677 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003678 *arg = skipwhite(*arg);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003679 if (**arg != '(')
3680 {
3681 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003682 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003683 ret = FAIL;
3684 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003685 else if (VIM_ISWHITE((*arg)[-1]))
3686 {
3687 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003688 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003689 ret = FAIL;
3690 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003691 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02003692 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003693 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003694 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003695
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003696 // Clear the funcref afterwards, so that deleting it while
3697 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003698 if (evaluate)
3699 clear_tv(&base);
3700
Bram Moolenaarac92e252019-08-03 21:58:38 +02003701 return ret;
3702}
3703
3704/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003705 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3706 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003707 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3708 */
3709 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003710eval_index(
3711 char_u **arg,
3712 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003713 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003714 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003715{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003716 int evaluate = evalarg != NULL
3717 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003718 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003719 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003720 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003721 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003722 int keylen = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003723
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003724 if (check_can_index(rettv, evaluate, verbose) == FAIL)
3725 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003726
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003727 init_tv(&var1);
3728 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003729 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003730 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003731 /*
3732 * dict.name
3733 */
3734 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003735 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003736 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003737 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003738 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02003739 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003740 }
3741 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003742 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003743 /*
3744 * something[idx]
3745 *
3746 * Get the (first) variable from inside the [].
3747 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003748 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003749 if (**arg == ':')
3750 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003751 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003752 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003753 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003754 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003755 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003756 clear_tv(&var1);
3757 return FAIL;
3758 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003759
3760 /*
3761 * Get the second variable from inside the [:].
3762 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003763 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003764 if (**arg == ':')
3765 {
3766 range = TRUE;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003767 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003768 if (**arg == ']')
3769 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003770 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003771 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003772 if (!empty1)
3773 clear_tv(&var1);
3774 return FAIL;
3775 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003776 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003777 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003778 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003779 if (!empty1)
3780 clear_tv(&var1);
3781 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003782 return FAIL;
3783 }
3784 }
3785
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003786 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02003787 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003788 if (**arg != ']')
3789 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003790 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003791 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003792 clear_tv(&var1);
3793 if (range)
3794 clear_tv(&var2);
3795 return FAIL;
3796 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02003797 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003798 }
3799
3800 if (evaluate)
3801 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003802 int res = eval_index_inner(rettv, range,
3803 empty1 ? NULL : &var1, empty2 ? NULL : &var2,
3804 key, keylen, verbose);
3805 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003806 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003807 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003808 clear_tv(&var2);
3809 return res;
3810 }
3811 return OK;
3812}
3813
3814/*
3815 * Check if "rettv" can have an [index] or [sli:ce]
3816 */
3817 int
3818check_can_index(typval_T *rettv, int evaluate, int verbose)
3819{
3820 switch (rettv->v_type)
3821 {
3822 case VAR_FUNC:
3823 case VAR_PARTIAL:
3824 if (verbose)
3825 emsg(_("E695: Cannot index a Funcref"));
3826 return FAIL;
3827 case VAR_FLOAT:
3828#ifdef FEAT_FLOAT
3829 if (verbose)
3830 emsg(_(e_float_as_string));
3831 return FAIL;
3832#endif
3833 case VAR_BOOL:
3834 case VAR_SPECIAL:
3835 case VAR_JOB:
3836 case VAR_CHANNEL:
3837 if (verbose)
3838 emsg(_(e_cannot_index_special_variable));
3839 return FAIL;
3840 case VAR_UNKNOWN:
3841 case VAR_ANY:
3842 case VAR_VOID:
3843 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003844 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003845 emsg(_(e_cannot_index_special_variable));
3846 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003847 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003848 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003849
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003850 case VAR_STRING:
3851 case VAR_LIST:
3852 case VAR_DICT:
3853 case VAR_BLOB:
3854 break;
3855 case VAR_NUMBER:
3856 if (in_vim9script())
3857 emsg(_(e_cannot_index_number));
3858 break;
3859 }
3860 return OK;
3861}
3862
3863/*
3864 * Apply index or range to "rettv".
3865 * "var1" is the first index, NULL for [:expr].
3866 * "var2" is the second index, NULL for [expr] and [expr: ]
3867 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
3868 */
3869 int
3870eval_index_inner(
3871 typval_T *rettv,
3872 int is_range,
3873 typval_T *var1,
3874 typval_T *var2,
3875 char_u *key,
3876 int keylen,
3877 int verbose)
3878{
3879 long n1, n2 = 0;
3880 long len;
3881
3882 n1 = 0;
3883 if (var1 != NULL && rettv->v_type != VAR_DICT)
3884 n1 = tv_get_number(var1);
3885
3886 if (is_range)
3887 {
3888 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003889 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003890 if (verbose)
3891 emsg(_(e_cannot_slice_dictionary));
3892 return FAIL;
3893 }
3894 if (var2 == NULL)
3895 n2 = -1;
3896 else
3897 n2 = tv_get_number(var2);
3898 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01003899
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003900 switch (rettv->v_type)
3901 {
3902 case VAR_UNKNOWN:
3903 case VAR_ANY:
3904 case VAR_VOID:
3905 case VAR_FUNC:
3906 case VAR_PARTIAL:
3907 case VAR_FLOAT:
3908 case VAR_BOOL:
3909 case VAR_SPECIAL:
3910 case VAR_JOB:
3911 case VAR_CHANNEL:
3912 break; // not evaluating, skipping over subscript
3913
3914 case VAR_NUMBER:
3915 case VAR_STRING:
3916 {
3917 char_u *s = tv_get_string(rettv);
3918
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003919 len = (long)STRLEN(s);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003920 if (in_vim9script())
3921 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003922 if (is_range)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003923 s = string_slice(s, n1, n2);
3924 else
3925 s = char_from_string(s, n1);
3926 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003927 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003928 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003929 // The resulting variable is a substring. If the indexes
3930 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003931 if (n1 < 0)
3932 {
3933 n1 = len + n1;
3934 if (n1 < 0)
3935 n1 = 0;
3936 }
3937 if (n2 < 0)
3938 n2 = len + n2;
3939 else if (n2 >= len)
3940 n2 = len;
3941 if (n1 >= len || n2 < 0 || n1 > n2)
3942 s = NULL;
3943 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02003944 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003945 }
3946 else
3947 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003948 // The resulting variable is a string of a single
3949 // character. If the index is too big or negative the
3950 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003951 if (n1 >= len || n1 < 0)
3952 s = NULL;
3953 else
3954 s = vim_strnsave(s + n1, 1);
3955 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003956 clear_tv(rettv);
3957 rettv->v_type = VAR_STRING;
3958 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003959 }
3960 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003961
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003962 case VAR_BLOB:
3963 len = blob_len(rettv->vval.v_blob);
3964 if (is_range)
3965 {
3966 // The resulting variable is a sub-blob. If the indexes
3967 // are out of range the result is empty.
3968 if (n1 < 0)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003969 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003970 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003971 if (n1 < 0)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003972 n1 = 0;
3973 }
3974 if (n2 < 0)
3975 n2 = len + n2;
3976 else if (n2 >= len)
3977 n2 = len - 1;
3978 if (n1 >= len || n2 < 0 || n1 > n2)
3979 {
3980 clear_tv(rettv);
3981 rettv->v_type = VAR_BLOB;
3982 rettv->vval.v_blob = NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003983 }
3984 else
3985 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003986 blob_T *blob = blob_alloc();
3987 long i;
3988
3989 if (blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003990 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003991 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003992 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003993 blob_free(blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003994 return FAIL;
3995 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003996 blob->bv_ga.ga_len = n2 - n1 + 1;
3997 for (i = n1; i <= n2; i++)
3998 blob_set(blob, i - n1,
3999 blob_get(rettv->vval.v_blob, i));
4000
4001 clear_tv(rettv);
4002 rettv_blob_set(rettv, blob);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004003 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004004 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004005 }
4006 else
4007 {
4008 // The resulting variable is a byte value.
4009 // If the index is too big or negative that is an error.
4010 if (n1 < 0)
4011 n1 = len + n1;
4012 if (n1 < len && n1 >= 0)
4013 {
4014 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004015
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004016 clear_tv(rettv);
4017 rettv->v_type = VAR_NUMBER;
4018 rettv->vval.v_number = v;
4019 }
4020 else
4021 semsg(_(e_blobidx), n1);
4022 }
4023 break;
4024
4025 case VAR_LIST:
4026 if (var1 == NULL)
4027 n1 = 0;
4028 if (var2 == NULL)
4029 n2 = -1;
4030 if (list_slice_or_index(rettv->vval.v_list,
4031 is_range, n1, n2, rettv, verbose) == FAIL)
4032 return FAIL;
4033 break;
4034
4035 case VAR_DICT:
4036 {
4037 dictitem_T *item;
4038 typval_T tmp;
4039
4040 if (key == NULL)
4041 {
4042 key = tv_get_string_chk(var1);
4043 if (key == NULL)
4044 return FAIL;
4045 }
4046
4047 item = dict_find(rettv->vval.v_dict, key, (int)keylen);
4048
4049 if (item == NULL && verbose)
4050 semsg(_(e_dictkey), key);
4051 if (item == NULL)
4052 return FAIL;
4053
4054 copy_tv(&item->di_tv, &tmp);
4055 clear_tv(rettv);
4056 *rettv = tmp;
4057 }
4058 break;
4059 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004060 return OK;
4061}
4062
4063/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004064 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004065 */
4066 char_u *
4067partial_name(partial_T *pt)
4068{
4069 if (pt->pt_name != NULL)
4070 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004071 if (pt->pt_func != NULL)
4072 return pt->pt_func->uf_name;
4073 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004074}
4075
Bram Moolenaarddecc252016-04-06 22:59:37 +02004076 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004077partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004078{
4079 int i;
4080
4081 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004082 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004083 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004084 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004085 if (pt->pt_name != NULL)
4086 {
4087 func_unref(pt->pt_name);
4088 vim_free(pt->pt_name);
4089 }
4090 else
4091 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004092
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004093 // Decrease the reference count for the context of a closure. If down
4094 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004095 if (pt->pt_funcstack != NULL)
4096 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004097 --pt->pt_funcstack->fs_refcount;
4098 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004099 }
4100
Bram Moolenaarddecc252016-04-06 22:59:37 +02004101 vim_free(pt);
4102}
4103
4104/*
4105 * Unreference a closure: decrement the reference count and free it when it
4106 * becomes zero.
4107 */
4108 void
4109partial_unref(partial_T *pt)
4110{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004111 if (pt != NULL)
4112 {
4113 if (--pt->pt_refcount <= 0)
4114 partial_free(pt);
4115
4116 // If the reference count goes down to one, the funcstack may be the
4117 // only reference and can be freed if no other partials reference it.
4118 else if (pt->pt_refcount == 1 && pt->pt_funcstack != NULL)
4119 funcstack_check_refcount(pt->pt_funcstack);
4120 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004121}
4122
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004123/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004124 * Return the next (unique) copy ID.
4125 * Used for serializing nested structures.
4126 */
4127 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004128get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004129{
4130 current_copyID += COPYID_INC;
4131 return current_copyID;
4132}
4133
4134/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004135 * Garbage collection for lists and dictionaries.
4136 *
4137 * We use reference counts to be able to free most items right away when they
4138 * are no longer used. But for composite items it's possible that it becomes
4139 * unused while the reference count is > 0: When there is a recursive
4140 * reference. Example:
4141 * :let l = [1, 2, 3]
4142 * :let d = {9: l}
4143 * :let l[1] = d
4144 *
4145 * Since this is quite unusual we handle this with garbage collection: every
4146 * once in a while find out which lists and dicts are not referenced from any
4147 * variable.
4148 *
4149 * Here is a good reference text about garbage collection (refers to Python
4150 * but it applies to all reference-counting mechanisms):
4151 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004152 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004153
4154/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004155 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004156 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004157 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004158 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004159 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004160garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004161{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004162 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004163 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004164 buf_T *buf;
4165 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004166 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004167 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004168
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004169 if (!testing)
4170 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004171 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004172 want_garbage_collect = FALSE;
4173 may_garbage_collect = FALSE;
4174 garbage_collect_at_exit = FALSE;
4175 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004176
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004177 // The execution stack can grow big, limit the size.
4178 if (exestack.ga_maxlen - exestack.ga_len > 500)
4179 {
4180 size_t new_len;
4181 char_u *pp;
4182 int n;
4183
4184 // Keep 150% of the current size, with a minimum of the growth size.
4185 n = exestack.ga_len / 2;
4186 if (n < exestack.ga_growsize)
4187 n = exestack.ga_growsize;
4188
4189 // Don't make it bigger though.
4190 if (exestack.ga_len + n < exestack.ga_maxlen)
4191 {
4192 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
4193 pp = vim_realloc(exestack.ga_data, new_len);
4194 if (pp == NULL)
4195 return FAIL;
4196 exestack.ga_maxlen = exestack.ga_len + n;
4197 exestack.ga_data = pp;
4198 }
4199 }
4200
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004201 // We advance by two because we add one for items referenced through
4202 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004203 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004204
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004205 /*
4206 * 1. Go through all accessible variables and mark all lists and dicts
4207 * with copyID.
4208 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004209
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004210 // Don't free variables in the previous_funccal list unless they are only
4211 // referenced through previous_funccal. This must be first, because if
4212 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004213 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004214
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004215 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004216 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004217
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004218 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004219 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004220 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4221 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004222
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004223 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004224 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004225 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4226 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004227 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004228 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4229 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004230#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004231 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004232 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4233 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004234 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004235 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004236 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4237 NULL, NULL);
4238#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004239
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004240 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004241 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004242 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4243 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004244 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004245 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004246
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004247 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004248 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004249
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004250 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004251 abort = abort || set_ref_in_functions(copyID);
4252
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004253 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004254 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004255
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004256 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004257 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004258
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004259 // callbacks in buffers
4260 abort = abort || set_ref_in_buffers(copyID);
4261
Bram Moolenaar1dced572012-04-05 16:54:08 +02004262#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004263 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004264#endif
4265
Bram Moolenaardb913952012-06-29 12:54:53 +02004266#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004267 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004268#endif
4269
4270#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004271 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004272#endif
4273
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004274#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004275 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004276 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004277#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004278#ifdef FEAT_NETBEANS_INTG
4279 abort = abort || set_ref_in_nb_channel(copyID);
4280#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004281
Bram Moolenaare3188e22016-05-31 21:13:04 +02004282#ifdef FEAT_TIMERS
4283 abort = abort || set_ref_in_timer(copyID);
4284#endif
4285
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004286#ifdef FEAT_QUICKFIX
4287 abort = abort || set_ref_in_quickfix(copyID);
4288#endif
4289
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004290#ifdef FEAT_TERMINAL
4291 abort = abort || set_ref_in_term(copyID);
4292#endif
4293
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004294#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004295 abort = abort || set_ref_in_popups(copyID);
4296#endif
4297
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004298 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004299 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004300 /*
4301 * 2. Free lists and dictionaries that are not referenced.
4302 */
4303 did_free = free_unref_items(copyID);
4304
4305 /*
4306 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004307 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004308 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004309 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004310 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004311 else if (p_verbose > 0)
4312 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004313 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004314 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004315
4316 return did_free;
4317}
4318
4319/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004320 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004321 */
4322 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004323free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004324{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004325 int did_free = FALSE;
4326
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004327 // Let all "free" functions know that we are here. This means no
4328 // dictionaries, lists, channels or jobs are to be freed, because we will
4329 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004330 in_free_unref_items = TRUE;
4331
4332 /*
4333 * PASS 1: free the contents of the items. We don't free the items
4334 * themselves yet, so that it is possible to decrement refcount counters
4335 */
4336
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004337 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004338 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004339
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004340 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004341 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004342
4343#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004344 // Go through the list of jobs and free items without the copyID. This
4345 // must happen before doing channels, because jobs refer to channels, but
4346 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004347 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4348
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004349 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004350 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4351#endif
4352
4353 /*
4354 * PASS 2: free the items themselves.
4355 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004356 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004357 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004358
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004359#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004360 // Go through the list of jobs and free items without the copyID. This
4361 // must happen before doing channels, because jobs refer to channels, but
4362 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004363 free_unused_jobs(copyID, COPYID_MASK);
4364
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004365 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004366 free_unused_channels(copyID, COPYID_MASK);
4367#endif
4368
4369 in_free_unref_items = FALSE;
4370
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004371 return did_free;
4372}
4373
4374/*
4375 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004376 * "list_stack" is used to add lists to be marked. Can be NULL.
4377 *
4378 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004379 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004380 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004381set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004382{
4383 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004384 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004385 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004386 hashtab_T *cur_ht;
4387 ht_stack_T *ht_stack = NULL;
4388 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004389
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004390 cur_ht = ht;
4391 for (;;)
4392 {
4393 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004394 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004395 // Mark each item in the hashtab. If the item contains a hashtab
4396 // it is added to ht_stack, if it contains a list it is added to
4397 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004398 todo = (int)cur_ht->ht_used;
4399 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4400 if (!HASHITEM_EMPTY(hi))
4401 {
4402 --todo;
4403 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4404 &ht_stack, list_stack);
4405 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004406 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004407
4408 if (ht_stack == NULL)
4409 break;
4410
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004411 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004412 cur_ht = ht_stack->ht;
4413 tempitem = ht_stack;
4414 ht_stack = ht_stack->prev;
4415 free(tempitem);
4416 }
4417
4418 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004419}
4420
4421/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004422 * Mark a dict and its items with "copyID".
4423 * Returns TRUE if setting references failed somehow.
4424 */
4425 int
4426set_ref_in_dict(dict_T *d, int copyID)
4427{
4428 if (d != NULL && d->dv_copyID != copyID)
4429 {
4430 d->dv_copyID = copyID;
4431 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4432 }
4433 return FALSE;
4434}
4435
4436/*
4437 * Mark a list and its items with "copyID".
4438 * Returns TRUE if setting references failed somehow.
4439 */
4440 int
4441set_ref_in_list(list_T *ll, int copyID)
4442{
4443 if (ll != NULL && ll->lv_copyID != copyID)
4444 {
4445 ll->lv_copyID = copyID;
4446 return set_ref_in_list_items(ll, copyID, NULL);
4447 }
4448 return FALSE;
4449}
4450
4451/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004452 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004453 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4454 *
4455 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004456 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004457 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004458set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004459{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004460 listitem_T *li;
4461 int abort = FALSE;
4462 list_T *cur_l;
4463 list_stack_T *list_stack = NULL;
4464 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004465
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004466 cur_l = l;
4467 for (;;)
4468 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004469 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004470 // Mark each item in the list. If the item contains a hashtab
4471 // it is added to ht_stack, if it contains a list it is added to
4472 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004473 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4474 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4475 ht_stack, &list_stack);
4476 if (list_stack == NULL)
4477 break;
4478
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004479 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004480 cur_l = list_stack->list;
4481 tempitem = list_stack;
4482 list_stack = list_stack->prev;
4483 free(tempitem);
4484 }
4485
4486 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004487}
4488
4489/*
4490 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004491 * "list_stack" is used to add lists to be marked. Can be NULL.
4492 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4493 *
4494 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004495 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004496 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004497set_ref_in_item(
4498 typval_T *tv,
4499 int copyID,
4500 ht_stack_T **ht_stack,
4501 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004502{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004503 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004504
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004505 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004506 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004507 dict_T *dd = tv->vval.v_dict;
4508
Bram Moolenaara03f2332016-02-06 18:09:59 +01004509 if (dd != NULL && dd->dv_copyID != copyID)
4510 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004511 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004512 dd->dv_copyID = copyID;
4513 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004514 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004515 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4516 }
4517 else
4518 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004519 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
4520
Bram Moolenaara03f2332016-02-06 18:09:59 +01004521 if (newitem == NULL)
4522 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004523 else
4524 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004525 newitem->ht = &dd->dv_hashtab;
4526 newitem->prev = *ht_stack;
4527 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004528 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004529 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004530 }
4531 }
4532 else if (tv->v_type == VAR_LIST)
4533 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004534 list_T *ll = tv->vval.v_list;
4535
Bram Moolenaara03f2332016-02-06 18:09:59 +01004536 if (ll != NULL && ll->lv_copyID != copyID)
4537 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004538 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004539 ll->lv_copyID = copyID;
4540 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004541 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004542 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004543 }
4544 else
4545 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02004546 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
4547
Bram Moolenaara03f2332016-02-06 18:09:59 +01004548 if (newitem == NULL)
4549 abort = TRUE;
4550 else
4551 {
4552 newitem->list = ll;
4553 newitem->prev = *list_stack;
4554 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004555 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004556 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004557 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004558 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004559 else if (tv->v_type == VAR_FUNC)
4560 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004561 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004562 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004563 else if (tv->v_type == VAR_PARTIAL)
4564 {
4565 partial_T *pt = tv->vval.v_partial;
4566 int i;
4567
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004568 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004569 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004570 // Didn't see this partial yet.
4571 pt->pt_copyID = copyID;
4572
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004573 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004574
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004575 if (pt->pt_dict != NULL)
4576 {
4577 typval_T dtv;
4578
4579 dtv.v_type = VAR_DICT;
4580 dtv.vval.v_dict = pt->pt_dict;
4581 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4582 }
4583
4584 for (i = 0; i < pt->pt_argc; ++i)
4585 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4586 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004587 if (pt->pt_funcstack != NULL)
4588 {
4589 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4590
4591 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4592 abort = abort || set_ref_in_item(stack + i, copyID,
4593 ht_stack, list_stack);
4594 }
4595
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004596 }
4597 }
4598#ifdef FEAT_JOB_CHANNEL
4599 else if (tv->v_type == VAR_JOB)
4600 {
4601 job_T *job = tv->vval.v_job;
4602 typval_T dtv;
4603
4604 if (job != NULL && job->jv_copyID != copyID)
4605 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004606 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004607 if (job->jv_channel != NULL)
4608 {
4609 dtv.v_type = VAR_CHANNEL;
4610 dtv.vval.v_channel = job->jv_channel;
4611 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4612 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004613 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004614 {
4615 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004616 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004617 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4618 }
4619 }
4620 }
4621 else if (tv->v_type == VAR_CHANNEL)
4622 {
4623 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004624 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004625 typval_T dtv;
4626 jsonq_T *jq;
4627 cbq_T *cq;
4628
4629 if (ch != NULL && ch->ch_copyID != copyID)
4630 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004631 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004632 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004633 {
4634 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4635 jq = jq->jq_next)
4636 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4637 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4638 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004639 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004640 {
4641 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004642 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004643 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4644 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004645 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004646 {
4647 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004648 dtv.vval.v_partial =
4649 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004650 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4651 }
4652 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004653 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004654 {
4655 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004656 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004657 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4658 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004659 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004660 {
4661 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004662 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004663 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4664 }
4665 }
4666 }
4667#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004668 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004669}
4670
Bram Moolenaar8c711452005-01-14 21:53:12 +00004671/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004672 * Return a string with the string representation of a variable.
4673 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004674 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004675 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004676 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01004677 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02004678 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004679 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4680 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004681 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004682 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004683 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004684echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004685 typval_T *tv,
4686 char_u **tofree,
4687 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004688 int copyID,
4689 int echo_style,
4690 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004691 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004692{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004693 static int recurse = 0;
4694 char_u *r = NULL;
4695
Bram Moolenaar33570922005-01-25 22:26:29 +00004696 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004697 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004698 if (!did_echo_string_emsg)
4699 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004700 // Only give this message once for a recursive call to avoid
4701 // flooding the user with errors. And stop iterating over lists
4702 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004703 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004704 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004705 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004706 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004707 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004708 }
4709 ++recurse;
4710
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004711 switch (tv->v_type)
4712 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004713 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004714 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004715 {
4716 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004717 r = tv->vval.v_string;
4718 if (r == NULL)
4719 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004720 }
4721 else
4722 {
4723 *tofree = string_quote(tv->vval.v_string, FALSE);
4724 r = *tofree;
4725 }
4726 break;
4727
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004728 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004729 if (echo_style)
4730 {
4731 *tofree = NULL;
4732 r = tv->vval.v_string;
4733 }
4734 else
4735 {
4736 *tofree = string_quote(tv->vval.v_string, TRUE);
4737 r = *tofree;
4738 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004739 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004740
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004741 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004742 {
4743 partial_T *pt = tv->vval.v_partial;
4744 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004745 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004746 garray_T ga;
4747 int i;
4748 char_u *tf;
4749
4750 ga_init2(&ga, 1, 100);
4751 ga_concat(&ga, (char_u *)"function(");
4752 if (fname != NULL)
4753 {
4754 ga_concat(&ga, fname);
4755 vim_free(fname);
4756 }
4757 if (pt != NULL && pt->pt_argc > 0)
4758 {
4759 ga_concat(&ga, (char_u *)", [");
4760 for (i = 0; i < pt->pt_argc; ++i)
4761 {
4762 if (i > 0)
4763 ga_concat(&ga, (char_u *)", ");
4764 ga_concat(&ga,
4765 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4766 vim_free(tf);
4767 }
4768 ga_concat(&ga, (char_u *)"]");
4769 }
4770 if (pt != NULL && pt->pt_dict != NULL)
4771 {
4772 typval_T dtv;
4773
4774 ga_concat(&ga, (char_u *)", ");
4775 dtv.v_type = VAR_DICT;
4776 dtv.vval.v_dict = pt->pt_dict;
4777 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4778 vim_free(tf);
4779 }
4780 ga_concat(&ga, (char_u *)")");
4781
4782 *tofree = ga.ga_data;
4783 r = *tofree;
4784 break;
4785 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004786
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004787 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004788 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004789 break;
4790
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004791 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004792 if (tv->vval.v_list == NULL)
4793 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004794 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004795 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004796 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004797 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004798 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4799 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004800 {
4801 *tofree = NULL;
4802 r = (char_u *)"[...]";
4803 }
4804 else
4805 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004806 int old_copyID = tv->vval.v_list->lv_copyID;
4807
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004808 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004809 *tofree = list2string(tv, copyID, restore_copyID);
4810 if (restore_copyID)
4811 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004812 r = *tofree;
4813 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004814 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004815
Bram Moolenaar8c711452005-01-14 21:53:12 +00004816 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004817 if (tv->vval.v_dict == NULL)
4818 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004819 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004820 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004821 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004822 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004823 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4824 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004825 {
4826 *tofree = NULL;
4827 r = (char_u *)"{...}";
4828 }
4829 else
4830 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004831 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004832
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004833 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004834 *tofree = dict2string(tv, copyID, restore_copyID);
4835 if (restore_copyID)
4836 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004837 r = *tofree;
4838 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004839 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004840
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004841 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004842 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004843 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004844 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004845 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004846 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004847 break;
4848
Bram Moolenaar835dc632016-02-07 14:27:38 +01004849 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004850 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004851 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004852 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004853 if (composite_val)
4854 {
4855 *tofree = string_quote(r, FALSE);
4856 r = *tofree;
4857 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004858 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004859
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004860 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004861#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004862 *tofree = NULL;
4863 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4864 r = numbuf;
4865 break;
4866#endif
4867
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004868 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004869 case VAR_SPECIAL:
4870 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004871 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004872 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004873 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004874
Bram Moolenaar8502c702014-06-17 12:51:16 +02004875 if (--recurse == 0)
4876 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004877 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004878}
4879
4880/*
4881 * Return a string with the string representation of a variable.
4882 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4883 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004884 * Does not put quotes around strings, as ":echo" displays values.
4885 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4886 * May return NULL.
4887 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004888 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004889echo_string(
4890 typval_T *tv,
4891 char_u **tofree,
4892 char_u *numbuf,
4893 int copyID)
4894{
4895 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4896}
4897
4898/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004899 * Return string "str" in ' quotes, doubling ' characters.
4900 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004901 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004902 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004903 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004904string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004905{
Bram Moolenaar33570922005-01-25 22:26:29 +00004906 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004907 char_u *p, *r, *s;
4908
Bram Moolenaar33570922005-01-25 22:26:29 +00004909 len = (function ? 13 : 3);
4910 if (str != NULL)
4911 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004912 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004913 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004914 if (*p == '\'')
4915 ++len;
4916 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004917 s = r = alloc(len);
4918 if (r != NULL)
4919 {
4920 if (function)
4921 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004922 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004923 r += 10;
4924 }
4925 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004926 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004927 if (str != NULL)
4928 for (p = str; *p != NUL; )
4929 {
4930 if (*p == '\'')
4931 *r++ = '\'';
4932 MB_COPY_CHAR(p, r);
4933 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004934 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004935 if (function)
4936 *r++ = ')';
4937 *r++ = NUL;
4938 }
4939 return s;
4940}
4941
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004942#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004943/*
4944 * Convert the string "text" to a floating point number.
4945 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4946 * this always uses a decimal point.
4947 * Returns the length of the text that was consumed.
4948 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004949 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004950string2float(
4951 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004952 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004953{
4954 char *s = (char *)text;
4955 float_T f;
4956
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004957 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004958 if (STRNICMP(text, "inf", 3) == 0)
4959 {
4960 *value = INFINITY;
4961 return 3;
4962 }
4963 if (STRNICMP(text, "-inf", 3) == 0)
4964 {
4965 *value = -INFINITY;
4966 return 4;
4967 }
4968 if (STRNICMP(text, "nan", 3) == 0)
4969 {
4970 *value = NAN;
4971 return 3;
4972 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004973 f = strtod(s, &s);
4974 *value = f;
4975 return (int)((char_u *)s - text);
4976}
4977#endif
4978
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004979/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004981 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004983 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004984var2fpos(
4985 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004986 int dollar_lnum, // TRUE when $ is last line
4987 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004989 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004991 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004993 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004994 if (varp->v_type == VAR_LIST)
4995 {
4996 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004997 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004998 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004999 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005000
5001 l = varp->vval.v_list;
5002 if (l == NULL)
5003 return NULL;
5004
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005005 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005006 pos.lnum = list_find_nr(l, 0L, &error);
5007 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005008 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005009 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005010
Bram Moolenaarec65d772020-08-20 22:29:12 +02005011 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005012 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005013 li = list_find(l, 1L);
5014 if (li != NULL && li->li_tv.v_type == VAR_STRING
5015 && li->li_tv.vval.v_string != NULL
5016 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005017 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005018 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005019 }
5020 else
5021 {
5022 pos.col = list_find_nr(l, 1L, &error);
5023 if (error)
5024 return NULL;
5025 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005026
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005027 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005028 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005029 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005030 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005031
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005032 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005033 pos.coladd = list_find_nr(l, 2L, &error);
5034 if (error)
5035 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005036
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005037 return &pos;
5038 }
5039
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005040 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005041 if (name == NULL)
5042 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005043 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005045 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005046 {
5047 if (VIsual_active)
5048 return &VIsual;
5049 return &curwin->w_cursor;
5050 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005051 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005053 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5055 return NULL;
5056 return pp;
5057 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005058
Bram Moolenaara5525202006-03-02 22:52:09 +00005059 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005060
Bram Moolenaar477933c2007-07-17 14:32:23 +00005061 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005062 {
5063 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005064 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005065 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005066 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005067 // In silent Ex mode topline is zero, but that's not a valid line
5068 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005069 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005070 return &pos;
5071 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005072 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005073 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005074 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005075 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005076 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005077 return &pos;
5078 }
5079 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005080 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005082 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 {
5084 pos.lnum = curbuf->b_ml.ml_line_count;
5085 pos.col = 0;
5086 }
5087 else
5088 {
5089 pos.lnum = curwin->w_cursor.lnum;
5090 pos.col = (colnr_T)STRLEN(ml_get_curline());
5091 }
5092 return &pos;
5093 }
5094 return NULL;
5095}
5096
5097/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005098 * Convert list in "arg" into a position and optional file number.
5099 * When "fnump" is NULL there is no file number, only 3 items.
5100 * Note that the column is passed on as-is, the caller may want to decrement
5101 * it to use 1 for the first column.
5102 * Return FAIL when conversion is not possible, doesn't check the position for
5103 * validity.
5104 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005105 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005106list2fpos(
5107 typval_T *arg,
5108 pos_T *posp,
5109 int *fnump,
5110 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005111{
5112 list_T *l = arg->vval.v_list;
5113 long i = 0;
5114 long n;
5115
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005116 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5117 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005118 if (arg->v_type != VAR_LIST
5119 || l == NULL
5120 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005121 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005122 return FAIL;
5123
5124 if (fnump != NULL)
5125 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005126 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005127 if (n < 0)
5128 return FAIL;
5129 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005130 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005131 *fnump = n;
5132 }
5133
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005134 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005135 if (n < 0)
5136 return FAIL;
5137 posp->lnum = n;
5138
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005139 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005140 if (n < 0)
5141 return FAIL;
5142 posp->col = n;
5143
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005144 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005145 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005146 posp->coladd = 0;
5147 else
5148 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005149
Bram Moolenaar493c1782014-05-28 14:34:46 +02005150 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005151 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005152
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005153 return OK;
5154}
5155
5156/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 * Get the length of an environment variable name.
5158 * Advance "arg" to the first character after the name.
5159 * Return 0 for error.
5160 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005161 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005162get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163{
5164 char_u *p;
5165 int len;
5166
5167 for (p = *arg; vim_isIDc(*p); ++p)
5168 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005169 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 return 0;
5171
5172 len = (int)(p - *arg);
5173 *arg = p;
5174 return len;
5175}
5176
5177/*
5178 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005179 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 * Return 0 if something is wrong.
5181 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005182 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005183get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184{
5185 char_u *p;
5186 int len;
5187
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005188 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005190 {
5191 if (*p == ':')
5192 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005193 // "s:" is start of "s:var", but "n:" is not and can be used in
5194 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005195 len = (int)(p - *arg);
5196 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5197 || len > 1)
5198 break;
5199 }
5200 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005201 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 return 0;
5203
5204 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005205 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206
5207 return len;
5208}
5209
5210/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005211 * Get the length of the name of a variable or function.
5212 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005214 * Return -1 if curly braces expansion failed.
5215 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 * If the name contains 'magic' {}'s, expand them and return the
5217 * expanded name in an allocated string via 'alias' - caller must free.
5218 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005219 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005220get_name_len(
5221 char_u **arg,
5222 char_u **alias,
5223 int evaluate,
5224 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225{
5226 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 char_u *p;
5228 char_u *expr_start;
5229 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005231 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232
5233 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5234 && (*arg)[2] == (int)KE_SNR)
5235 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005236 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 *arg += 3;
5238 return get_id_len(arg) + 3;
5239 }
5240 len = eval_fname_script(*arg);
5241 if (len > 0)
5242 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005243 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 *arg += len;
5245 }
5246
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005248 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005250 p = find_name_end(*arg, &expr_start, &expr_end,
5251 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 if (expr_start != NULL)
5253 {
5254 char_u *temp_string;
5255
5256 if (!evaluate)
5257 {
5258 len += (int)(p - *arg);
5259 *arg = skipwhite(p);
5260 return len;
5261 }
5262
5263 /*
5264 * Include any <SID> etc in the expanded string:
5265 * Thus the -len here.
5266 */
5267 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5268 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005269 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 *alias = temp_string;
5271 *arg = skipwhite(p);
5272 return (int)STRLEN(temp_string);
5273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274
5275 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005276 // Only give an error when there is something, otherwise it will be
5277 // reported at a higher level.
5278 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005279 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280
5281 return len;
5282}
5283
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005284/*
5285 * Find the end of a variable or function name, taking care of magic braces.
5286 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5287 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005288 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005289 * Return a pointer to just after the name. Equal to "arg" if there is no
5290 * valid name.
5291 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005292 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005293find_name_end(
5294 char_u *arg,
5295 char_u **expr_start,
5296 char_u **expr_end,
5297 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005299 int mb_nest = 0;
5300 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005302 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02005303 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005305 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005307 *expr_start = NULL;
5308 *expr_end = NULL;
5309 }
5310
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005311 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005312 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5313 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005314 return arg;
5315
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005316 for (p = arg; *p != NUL
5317 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005318 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02005319 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005320 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005321 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005322 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005323 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005324 if (*p == '\'')
5325 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005326 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005327 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005328 ;
5329 if (*p == NUL)
5330 break;
5331 }
5332 else if (*p == '"')
5333 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005334 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005335 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005336 if (*p == '\\' && p[1] != NUL)
5337 ++p;
5338 if (*p == NUL)
5339 break;
5340 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005341 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5342 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005343 // "s:" is start of "s:var", but "n:" is not and can be used in
5344 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005345 len = (int)(p - arg);
5346 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005347 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005348 break;
5349 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005350
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005351 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005353 if (*p == '[')
5354 ++br_nest;
5355 else if (*p == ']')
5356 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005358
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005359 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005361 if (*p == '{')
5362 {
5363 mb_nest++;
5364 if (expr_start != NULL && *expr_start == NULL)
5365 *expr_start = p;
5366 }
5367 else if (*p == '}')
5368 {
5369 mb_nest--;
5370 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5371 *expr_end = p;
5372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 }
5375
5376 return p;
5377}
5378
5379/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005380 * Expands out the 'magic' {}'s in a variable/function name.
5381 * Note that this can call itself recursively, to deal with
5382 * constructs like foo{bar}{baz}{bam}
5383 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5384 * "in_start" ^
5385 * "expr_start" ^
5386 * "expr_end" ^
5387 * "in_end" ^
5388 *
5389 * Returns a new allocated string, which the caller must free.
5390 * Returns NULL for failure.
5391 */
5392 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005393make_expanded_name(
5394 char_u *in_start,
5395 char_u *expr_start,
5396 char_u *expr_end,
5397 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005398{
5399 char_u c1;
5400 char_u *retval = NULL;
5401 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005402
5403 if (expr_end == NULL || in_end == NULL)
5404 return NULL;
5405 *expr_start = NUL;
5406 *expr_end = NUL;
5407 c1 = *in_end;
5408 *in_end = NUL;
5409
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005410 temp_result = eval_to_string(expr_start + 1, FALSE);
5411 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005412 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005413 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5414 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005415 if (retval != NULL)
5416 {
5417 STRCPY(retval, in_start);
5418 STRCAT(retval, temp_result);
5419 STRCAT(retval, expr_end + 1);
5420 }
5421 }
5422 vim_free(temp_result);
5423
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005424 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005425 *expr_start = '{';
5426 *expr_end = '}';
5427
5428 if (retval != NULL)
5429 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005430 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005431 if (expr_start != NULL)
5432 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005433 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005434 temp_result = make_expanded_name(retval, expr_start,
5435 expr_end, temp_result);
5436 vim_free(retval);
5437 retval = temp_result;
5438 }
5439 }
5440
5441 return retval;
5442}
5443
5444/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005446 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005448 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005449eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005451 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005452}
5453
5454/*
5455 * Return TRUE if character "c" can be used as the first character in a
5456 * variable or function name (excluding '{' and '}').
5457 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005458 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005459eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005460{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005461 return ASCII_ISALPHA(c) || c == '_';
5462}
5463
5464/*
5465 * Return TRUE if character "c" can be used as the first character of a
5466 * dictionary key.
5467 */
5468 int
5469eval_isdictc(int c)
5470{
5471 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472}
5473
5474/*
Bram Moolenaare3c37d82020-08-15 18:39:05 +02005475 * Return the character "str[index]" where "index" is the character index. If
5476 * "index" is out of range NULL is returned.
5477 */
5478 char_u *
5479char_from_string(char_u *str, varnumber_T index)
5480{
5481 size_t nbyte = 0;
5482 varnumber_T nchar = index;
5483 size_t slen;
5484
5485 if (str == NULL || index < 0)
5486 return NULL;
5487 slen = STRLEN(str);
5488 while (nchar > 0 && nbyte < slen)
5489 {
5490 nbyte += MB_CPTR2LEN(str + nbyte);
5491 --nchar;
5492 }
5493 if (nbyte >= slen)
5494 return NULL;
5495 return vim_strnsave(str + nbyte, MB_CPTR2LEN(str + nbyte));
5496}
5497
5498/*
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005499 * Get the byte index for character index "idx" in string "str" with length
5500 * "str_len".
5501 * If going over the end return "str_len".
5502 * If "idx" is negative count from the end, -1 is the last character.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005503 * When going over the start return -1.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005504 */
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005505 static long
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005506char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
5507{
5508 varnumber_T nchar = idx;
5509 size_t nbyte = 0;
5510
5511 if (nchar >= 0)
5512 {
5513 while (nchar > 0 && nbyte < str_len)
5514 {
5515 nbyte += MB_CPTR2LEN(str + nbyte);
5516 --nchar;
5517 }
5518 }
5519 else
5520 {
5521 nbyte = str_len;
5522 while (nchar < 0 && nbyte > 0)
5523 {
5524 --nbyte;
5525 nbyte -= mb_head_off(str, str + nbyte);
5526 ++nchar;
5527 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005528 if (nchar < 0)
5529 return -1;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005530 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005531 return (long)nbyte;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005532}
5533
5534/*
5535 * Return the slice "str[first:last]" using character indexes.
5536 * Return NULL when the result is empty.
5537 */
5538 char_u *
5539string_slice(char_u *str, varnumber_T first, varnumber_T last)
5540{
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005541 long start_byte, end_byte;
5542 size_t slen;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005543
5544 if (str == NULL)
5545 return NULL;
5546 slen = STRLEN(str);
5547 start_byte = char_idx2byte(str, slen, first);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005548 if (start_byte < 0)
5549 start_byte = 0; // first index very negative: use zero
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005550 if (last == -1)
Bram Moolenaar25660542020-08-28 16:38:11 +02005551 end_byte = (long)slen;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005552 else
5553 {
5554 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005555 if (end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005556 // end index is inclusive
5557 end_byte += MB_CPTR2LEN(str + end_byte);
5558 }
5559
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005560 if (start_byte >= (long)slen || end_byte <= start_byte)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005561 return NULL;
5562 return vim_strnsave(str + start_byte, end_byte - start_byte);
5563}
5564
5565/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005566 * Handle:
5567 * - expr[expr], expr[expr:expr] subscript
5568 * - ".name" lookup
5569 * - function call with Funcref variable: func(expr)
5570 * - method call: var->method()
5571 *
5572 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005573 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005574 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005575handle_subscript(
5576 char_u **arg,
5577 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005578 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02005579 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005580{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005581 int evaluate = evalarg != NULL
5582 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005583 int ret = OK;
5584 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005585 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005586 int getnext;
5587 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005588
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005589 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005590 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005591 // When at the end of the line and ".name" or "->{" or "->X" follows in
5592 // the next line then consume the line break.
5593 p = eval_next_non_blank(*arg, evalarg, &getnext);
5594 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02005595 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaar9d489562020-07-30 20:08:50 +02005596 || (p[0] == '-' && p[1] == '>'
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005597 && (p[2] == '{' || ASCII_ISALPHA(p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005598 {
5599 *arg = eval_next_line(evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02005600 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02005601 check_white = FALSE;
5602 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005603
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005604 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
5605 || rettv->v_type == VAR_PARTIAL))
5606 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005607 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02005608 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
5609 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005610
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005611 // Stop the expression evaluation when immediately aborting on
5612 // error, or when an interrupt occurred or an exception was thrown
5613 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005614 if (aborting())
5615 {
5616 if (ret == OK)
5617 clear_tv(rettv);
5618 ret = FAIL;
5619 }
5620 dict_unref(selfdict);
5621 selfdict = NULL;
5622 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02005623 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02005624 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02005625 *arg = p;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005626 if (ret == OK)
5627 {
5628 if ((*arg)[2] == '{')
5629 // expr->{lambda}()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005630 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005631 else
5632 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02005633 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005634 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005635 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005636 // "." is ".name" lookup when we found a dict or when evaluating and
5637 // scriptversion is at least 2, where string concatenation is "..".
5638 else if (**arg == '['
5639 || (**arg == '.' && (rettv->v_type == VAR_DICT
5640 || (!evaluate
5641 && (*arg)[1] != '.'
5642 && current_sctx.sc_version >= 2))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005643 {
5644 dict_unref(selfdict);
5645 if (rettv->v_type == VAR_DICT)
5646 {
5647 selfdict = rettv->vval.v_dict;
5648 if (selfdict != NULL)
5649 ++selfdict->dv_refcount;
5650 }
5651 else
5652 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005653 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005654 {
5655 clear_tv(rettv);
5656 ret = FAIL;
5657 }
5658 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02005659 else
5660 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005661 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005662
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005663 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5664 // Don't do this when "Func" is already a partial that was bound
5665 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005666 if (selfdict != NULL
5667 && (rettv->v_type == VAR_FUNC
5668 || (rettv->v_type == VAR_PARTIAL
5669 && (rettv->vval.v_partial->pt_auto
5670 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005671 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005672
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005673 dict_unref(selfdict);
5674 return ret;
5675}
5676
5677/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005678 * Make a copy of an item.
5679 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005680 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5681 * reference to an already copied list/dict can be used.
5682 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005683 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005684 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005685item_copy(
5686 typval_T *from,
5687 typval_T *to,
5688 int deep,
5689 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005690{
5691 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005692 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005693
Bram Moolenaar33570922005-01-25 22:26:29 +00005694 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005695 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005696 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005697 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005698 }
5699 ++recurse;
5700
5701 switch (from->v_type)
5702 {
5703 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005704 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005705 case VAR_STRING:
5706 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005707 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005708 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005709 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005710 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005711 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005712 copy_tv(from, to);
5713 break;
5714 case VAR_LIST:
5715 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005716 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005717 if (from->vval.v_list == NULL)
5718 to->vval.v_list = NULL;
5719 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5720 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005721 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005722 to->vval.v_list = from->vval.v_list->lv_copylist;
5723 ++to->vval.v_list->lv_refcount;
5724 }
5725 else
5726 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5727 if (to->vval.v_list == NULL)
5728 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005729 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005730 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005731 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005732 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005733 case VAR_DICT:
5734 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005735 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005736 if (from->vval.v_dict == NULL)
5737 to->vval.v_dict = NULL;
5738 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5739 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005740 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005741 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5742 ++to->vval.v_dict->dv_refcount;
5743 }
5744 else
5745 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5746 if (to->vval.v_dict == NULL)
5747 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005748 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005749 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005750 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005751 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005752 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005753 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005754 }
5755 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005756 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005757}
5758
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005759 void
5760echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5761{
5762 char_u *tofree;
5763 char_u numbuf[NUMBUFLEN];
5764 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5765
5766 if (*atstart)
5767 {
5768 *atstart = FALSE;
5769 // Call msg_start() after eval1(), evaluating the expression
5770 // may cause a message to appear.
5771 if (with_space)
5772 {
5773 // Mark the saved text as finishing the line, so that what
5774 // follows is displayed on a new line when scrolling back
5775 // at the more prompt.
5776 msg_sb_eol();
5777 msg_start();
5778 }
5779 }
5780 else if (with_space)
5781 msg_puts_attr(" ", echo_attr);
5782
5783 if (p != NULL)
5784 for ( ; *p != NUL && !got_int; ++p)
5785 {
5786 if (*p == '\n' || *p == '\r' || *p == TAB)
5787 {
5788 if (*p != TAB && *needclr)
5789 {
5790 // remove any text still there from the command
5791 msg_clr_eos();
5792 *needclr = FALSE;
5793 }
5794 msg_putchar_attr(*p, echo_attr);
5795 }
5796 else
5797 {
5798 if (has_mbyte)
5799 {
5800 int i = (*mb_ptr2len)(p);
5801
5802 (void)msg_outtrans_len_attr(p, i, echo_attr);
5803 p += i - 1;
5804 }
5805 else
5806 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5807 }
5808 }
5809 vim_free(tofree);
5810}
5811
Bram Moolenaare9a41262005-01-15 22:18:47 +00005812/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 * ":echo expr1 ..." print each argument separated with a space, add a
5814 * newline at the end.
5815 * ":echon expr1 ..." print each argument plain.
5816 */
5817 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005818ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819{
5820 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005821 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 char_u *p;
5823 int needclr = TRUE;
5824 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01005825 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005826 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005827 evalarg_T evalarg;
5828
Bram Moolenaare707c882020-07-01 13:07:37 +02005829 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830
5831 if (eap->skip)
5832 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005833 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005835 // If eval1() causes an error message the text from the command may
5836 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005837 need_clr_eos = needclr;
5838
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005840 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 {
5842 /*
5843 * Report the invalid expression unless the expression evaluation
5844 * has been cancelled due to an aborting error, an interrupt, or an
5845 * exception.
5846 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005847 if (!aborting() && did_emsg == did_emsg_before
5848 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005849 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005850 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 break;
5852 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005853 need_clr_eos = FALSE;
5854
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005856 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005857
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005858 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 arg = skipwhite(arg);
5860 }
5861 eap->nextcmd = check_nextcmd(arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02005862 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863
5864 if (eap->skip)
5865 --emsg_skip;
5866 else
5867 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005868 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 if (needclr)
5870 msg_clr_eos();
5871 if (eap->cmdidx == CMD_echo)
5872 msg_end();
5873 }
5874}
5875
5876/*
5877 * ":echohl {name}".
5878 */
5879 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005880ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005882 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883}
5884
5885/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005886 * Returns the :echo attribute
5887 */
5888 int
5889get_echo_attr(void)
5890{
5891 return echo_attr;
5892}
5893
5894/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 * ":execute expr1 ..." execute the result of an expression.
5896 * ":echomsg expr1 ..." Print a message
5897 * ":echoerr expr1 ..." Print an error
5898 * Each gets spaces around each argument and a newline at the end for
5899 * echo commands
5900 */
5901 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005902ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903{
5904 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005905 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 int ret = OK;
5907 char_u *p;
5908 garray_T ga;
5909 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910
5911 ga_init2(&ga, 1, 80);
5912
5913 if (eap->skip)
5914 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02005915 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02005917 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01005918 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920
5921 if (!eap->skip)
5922 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005923 char_u buf[NUMBUFLEN];
5924
5925 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01005926 {
5927 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
5928 {
5929 emsg(_(e_inval_string));
5930 p = NULL;
5931 }
5932 else
5933 p = tv_get_string_buf(&rettv, buf);
5934 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005935 else
5936 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005937 if (p == NULL)
5938 {
5939 clear_tv(&rettv);
5940 ret = FAIL;
5941 break;
5942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 len = (int)STRLEN(p);
5944 if (ga_grow(&ga, len + 2) == FAIL)
5945 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005946 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 ret = FAIL;
5948 break;
5949 }
5950 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 ga.ga_len += len;
5954 }
5955
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005956 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 arg = skipwhite(arg);
5958 }
5959
5960 if (ret != FAIL && ga.ga_data != NULL)
5961 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005962 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
5963 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005964 // Mark the already saved text as finishing the line, so that what
5965 // follows is displayed on a new line when scrolling back at the
5966 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005967 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005968 }
5969
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005971 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005972 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005973 out_flush();
5974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 else if (eap->cmdidx == CMD_echoerr)
5976 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005977 int save_did_emsg = did_emsg;
5978
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005979 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005980 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 if (!force_abort)
5982 did_emsg = save_did_emsg;
5983 }
5984 else if (eap->cmdidx == CMD_execute)
5985 do_cmdline((char_u *)ga.ga_data,
5986 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
5987 }
5988
5989 ga_clear(&ga);
5990
5991 if (eap->skip)
5992 --emsg_skip;
5993
5994 eap->nextcmd = check_nextcmd(arg);
5995}
5996
5997/*
5998 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
5999 * "arg" points to the "&" or '+' when called, to "option" when returning.
6000 * Returns NULL when no option name found. Otherwise pointer to the char
6001 * after the option name.
6002 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006003 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006004find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005{
6006 char_u *p = *arg;
6007
6008 ++p;
6009 if (*p == 'g' && p[1] == ':')
6010 {
6011 *opt_flags = OPT_GLOBAL;
6012 p += 2;
6013 }
6014 else if (*p == 'l' && p[1] == ':')
6015 {
6016 *opt_flags = OPT_LOCAL;
6017 p += 2;
6018 }
6019 else
6020 *opt_flags = 0;
6021
6022 if (!ASCII_ISALPHA(*p))
6023 return NULL;
6024 *arg = p;
6025
6026 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006027 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 else
6029 while (ASCII_ISALPHA(*p))
6030 ++p;
6031 return p;
6032}
6033
6034/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006035 * Display script name where an item was last set.
6036 * Should only be invoked when 'verbose' is non-zero.
6037 */
6038 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006039last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006040{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006041 char_u *p;
6042
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006043 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006044 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006045 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006046 if (p != NULL)
6047 {
6048 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006049 msg_puts(_("\n\tLast set from "));
6050 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006051 if (script_ctx.sc_lnum > 0)
6052 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006053 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006054 msg_outnum((long)script_ctx.sc_lnum);
6055 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006056 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006057 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006058 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006059 }
6060}
6061
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006062#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063
6064/*
6065 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006066 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 * "flags" can be "g" to do a global substitute.
6068 * Returns an allocated string, NULL for error.
6069 */
6070 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006071do_string_sub(
6072 char_u *str,
6073 char_u *pat,
6074 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006075 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006076 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077{
6078 int sublen;
6079 regmatch_T regmatch;
6080 int i;
6081 int do_all;
6082 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006083 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 garray_T ga;
6085 char_u *ret;
6086 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006087 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006089 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006091 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092
6093 ga_init2(&ga, 1, 200);
6094
6095 do_all = (flags[0] == 'g');
6096
6097 regmatch.rm_ic = p_ic;
6098 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6099 if (regmatch.regprog != NULL)
6100 {
6101 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006102 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6104 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006105 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006106 if (regmatch.startp[0] == regmatch.endp[0])
6107 {
6108 if (zero_width == regmatch.startp[0])
6109 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006110 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006111 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006112 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6113 (size_t)i);
6114 ga.ga_len += i;
6115 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006116 continue;
6117 }
6118 zero_width = regmatch.startp[0];
6119 }
6120
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 /*
6122 * Get some space for a temporary buffer to do the substitution
6123 * into. It will contain:
6124 * - The text up to where the match is.
6125 * - The substituted text.
6126 * - The text after the match.
6127 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006128 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006129 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6131 {
6132 ga_clear(&ga);
6133 break;
6134 }
6135
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006136 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 i = (int)(regmatch.startp[0] - tail);
6138 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006139 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006140 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 + ga.ga_len + i, TRUE, TRUE, FALSE);
6142 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006143 tail = regmatch.endp[0];
6144 if (*tail == NUL)
6145 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 if (!do_all)
6147 break;
6148 }
6149
6150 if (ga.ga_data != NULL)
6151 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6152
Bram Moolenaar473de612013-06-08 18:19:48 +02006153 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 }
6155
6156 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6157 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006158 if (p_cpo == empty_option)
6159 p_cpo = save_cpo;
6160 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006161 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006162 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163
6164 return ret;
6165}