blob: b1842647e6e0b76a030f054c9c45854864e3abc7 [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 Moolenaar9ef486d2005-01-17 22:23:00 +000023static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010025#define NAMESPACE_CHAR (char_u *)"abglstvw"
26
Bram Moolenaar532c7802005-01-27 14:44:31 +000027/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000028 * When recursively copying lists and dicts we need to remember which ones we
29 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000030 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000031 */
32static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020033
Bram Moolenaar071d4272004-06-13 20:20:40 +000034/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000035 * Info used by a ":for" loop.
36 */
Bram Moolenaar33570922005-01-25 22:26:29 +000037typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000038{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010039 int fi_semicolon; // TRUE if ending in '; var]'
40 int fi_varcount; // nr of variables in the list
41 listwatch_T fi_lw; // keep an eye on the item used.
42 list_T *fi_list; // list being used
43 int fi_bi; // index of blob
44 blob_T *fi_blob; // blob being used
Bram Moolenaar33570922005-01-25 22:26:29 +000045} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000046
Bram Moolenaar48e697e2016-01-23 22:17:30 +010047static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar32e35112020-05-14 22:41:15 +020048static int eval2(char_u **arg, typval_T *rettv, int flags);
49static int eval3(char_u **arg, typval_T *rettv, int flags);
50static int eval4(char_u **arg, typval_T *rettv, int flags);
51static int eval5(char_u **arg, typval_T *rettv, int flags);
52static int eval6(char_u **arg, typval_T *rettv, int flags, int want_string);
53static int eval7(char_u **arg, typval_T *rettv, int flags, int want_string);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +020054static int eval7_leader(typval_T *rettv, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000055
Bram Moolenaar48e697e2016-01-23 22:17:30 +010056static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020057static 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 +020058
Bram Moolenaare21c1582019-03-02 11:57:09 +010059/*
60 * Return "n1" divided by "n2", taking care of dividing by zero.
61 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020062 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010063num_divide(varnumber_T n1, varnumber_T n2)
64{
65 varnumber_T result;
66
67 if (n2 == 0) // give an error message?
68 {
69 if (n1 == 0)
70 result = VARNUM_MIN; // similar to NaN
71 else if (n1 < 0)
72 result = -VARNUM_MAX;
73 else
74 result = VARNUM_MAX;
75 }
76 else
77 result = n1 / n2;
78
79 return result;
80}
81
82/*
83 * Return "n1" modulus "n2", taking care of dividing by zero.
84 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020085 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010086num_modulus(varnumber_T n1, varnumber_T n2)
87{
88 // Give an error when n2 is 0?
89 return (n2 == 0) ? 0 : (n1 % n2);
90}
91
Bram Moolenaara1fa8922017-01-12 20:06:33 +010092#if defined(EBCDIC) || defined(PROTO)
93/*
94 * Compare struct fst by function name.
95 */
96 static int
97compare_func_name(const void *s1, const void *s2)
98{
99 struct fst *p1 = (struct fst *)s1;
100 struct fst *p2 = (struct fst *)s2;
101
102 return STRCMP(p1->f_name, p2->f_name);
103}
104
105/*
106 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100107 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100108 * On machines using EBCDIC we have to sort it.
109 */
110 static void
111sortFunctions(void)
112{
113 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
114
115 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
116}
117#endif
118
Bram Moolenaar33570922005-01-25 22:26:29 +0000119/*
120 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000121 */
122 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100123eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000124{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200125 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200126 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000127
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200128#ifdef EBCDIC
129 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100130 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200131 */
132 sortFunctions();
133#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000134}
135
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000136#if defined(EXITFREE) || defined(PROTO)
137 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100138eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000139{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200140 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100141 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200142 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000143
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200144 // autoloaded script names
145 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000146
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200147 // unreferenced lists and dicts
148 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200149
150 // functions not garbage collected
151 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000152}
153#endif
154
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155/*
156 * Top level evaluation function, returning a boolean.
157 * Sets "error" to TRUE if there was an error.
158 * Return TRUE or FALSE.
159 */
160 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100161eval_to_bool(
162 char_u *arg,
163 int *error,
164 char_u **nextcmd,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100165 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166{
Bram Moolenaar33570922005-01-25 22:26:29 +0000167 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200168 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169
170 if (skip)
171 ++emsg_skip;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200172 if (eval0(arg, &tv, nextcmd, skip ? 0 : EVAL_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174 else
175 {
176 *error = FALSE;
177 if (!skip)
178 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100179 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000180 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 }
182 }
183 if (skip)
184 --emsg_skip;
185
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200186 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187}
188
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100189/*
190 * Call eval1() and give an error message if not done at a lower level.
191 */
192 static int
193eval1_emsg(char_u **arg, typval_T *rettv, int evaluate)
194{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100195 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100196 int ret;
197 int did_emsg_before = did_emsg;
198 int called_emsg_before = called_emsg;
199
Bram Moolenaar32e35112020-05-14 22:41:15 +0200200 ret = eval1(arg, rettv, evaluate ? EVAL_EVALUATE : 0);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100201 if (ret == FAIL)
202 {
203 // Report the invalid expression unless the expression evaluation has
204 // been cancelled due to an aborting error, an interrupt, or an
205 // exception, or we already gave a more specific error.
206 // Also check called_emsg for when using assert_fails().
207 if (!aborting() && did_emsg == did_emsg_before
208 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100209 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100210 }
211 return ret;
212}
213
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100214/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200215 * Return whether a typval is a valid expression to pass to eval_expr_typval()
216 * or eval_expr_to_bool(). An empty string returns FALSE;
217 */
218 int
219eval_expr_valid_arg(typval_T *tv)
220{
221 return tv->v_type != VAR_UNKNOWN
222 && (tv->v_type != VAR_STRING
223 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
224}
225
226/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100227 * Evaluate an expression, which can be a function, partial or string.
228 * Pass arguments "argv[argc]".
229 * Return the result in "rettv" and OK or FAIL.
230 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200231 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100232eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
233{
234 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100235 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200236 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100237
238 if (expr->v_type == VAR_FUNC)
239 {
240 s = expr->vval.v_string;
241 if (s == NULL || *s == NUL)
242 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200243 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200244 funcexe.evaluate = TRUE;
245 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100246 return FAIL;
247 }
248 else if (expr->v_type == VAR_PARTIAL)
249 {
250 partial_T *partial = expr->vval.v_partial;
251
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200252 if (partial == NULL)
253 return FAIL;
254
Bram Moolenaar822ba242020-05-24 23:00:18 +0200255 if (partial->pt_func != NULL
256 && partial->pt_func->uf_dfunc_idx != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100257 {
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200258 if (call_def_function(partial->pt_func, argc, argv,
259 partial, rettv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100260 return FAIL;
261 }
262 else
263 {
264 s = partial_name(partial);
265 if (s == NULL || *s == NUL)
266 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200267 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100268 funcexe.evaluate = TRUE;
269 funcexe.partial = partial;
270 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
271 return FAIL;
272 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100273 }
274 else
275 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100276 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100277 if (s == NULL)
278 return FAIL;
279 s = skipwhite(s);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100280 if (eval1_emsg(&s, rettv, TRUE) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100281 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100282 if (*s != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100283 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200284 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100285 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100286 return FAIL;
287 }
288 }
289 return OK;
290}
291
292/*
293 * Like eval_to_bool() but using a typval_T instead of a string.
294 * Works for string, funcref and partial.
295 */
296 int
297eval_expr_to_bool(typval_T *expr, int *error)
298{
299 typval_T rettv;
300 int res;
301
302 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
303 {
304 *error = TRUE;
305 return FALSE;
306 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100307 res = (tv_get_number_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100308 clear_tv(&rettv);
309 return res;
310}
311
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312/*
313 * Top level evaluation function, returning a string. If "skip" is TRUE,
314 * only parsing to "nextcmd" is done, without reporting errors. Return
315 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
316 */
317 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100318eval_to_string_skip(
319 char_u *arg,
320 char_u **nextcmd,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100321 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322{
Bram Moolenaar33570922005-01-25 22:26:29 +0000323 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 char_u *retval;
325
326 if (skip)
327 ++emsg_skip;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200328 if (eval0(arg, &tv, nextcmd, skip ? 0 : EVAL_EVALUATE) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 retval = NULL;
330 else
331 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100332 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000333 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 }
335 if (skip)
336 --emsg_skip;
337
338 return retval;
339}
340
341/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000342 * Skip over an expression at "*pp".
343 * Return FAIL for an error, OK otherwise.
344 */
345 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100346skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000347{
Bram Moolenaar33570922005-01-25 22:26:29 +0000348 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000349
350 *pp = skipwhite(*pp);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200351 return eval1(pp, &rettv, 0);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000352}
353
354/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000356 * When "convert" is TRUE convert a List into a sequence of lines and convert
357 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 * Return pointer to allocated memory, or NULL for failure.
359 */
360 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100361eval_to_string(
362 char_u *arg,
363 char_u **nextcmd,
364 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365{
Bram Moolenaar33570922005-01-25 22:26:29 +0000366 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000368 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000369#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000370 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372
Bram Moolenaar32e35112020-05-14 22:41:15 +0200373 if (eval0(arg, &tv, nextcmd, EVAL_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 retval = NULL;
375 else
376 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000377 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000378 {
379 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000380 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200381 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200382 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200383 if (tv.vval.v_list->lv_len > 0)
384 ga_append(&ga, NL);
385 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000386 ga_append(&ga, NUL);
387 retval = (char_u *)ga.ga_data;
388 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000389#ifdef FEAT_FLOAT
390 else if (convert && tv.v_type == VAR_FLOAT)
391 {
392 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
393 retval = vim_strsave(numbuf);
394 }
395#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000396 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100397 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000398 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 }
400
401 return retval;
402}
403
404/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000405 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200406 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 */
408 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100409eval_to_string_safe(
410 char_u *arg,
411 char_u **nextcmd,
412 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413{
414 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200415 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200417 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000418 if (use_sandbox)
419 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200420 ++textwinlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000421 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000422 if (use_sandbox)
423 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200424 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200425 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426 return retval;
427}
428
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429/*
430 * Top level evaluation function, returning a number.
431 * Evaluates "expr" silently.
432 * Returns -1 for an error.
433 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200434 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100435eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436{
Bram Moolenaar33570922005-01-25 22:26:29 +0000437 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200438 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000439 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440
441 ++emsg_off;
442
Bram Moolenaar32e35112020-05-14 22:41:15 +0200443 if (eval1(&p, &rettv, EVAL_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 retval = -1;
445 else
446 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100447 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000448 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 }
450 --emsg_off;
451
452 return retval;
453}
454
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000455/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000456 * Top level evaluation function.
457 * Returns an allocated typval_T with the result.
458 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000459 */
460 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100461eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000462{
463 typval_T *tv;
464
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200465 tv = ALLOC_ONE(typval_T);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200466 if (tv != NULL && eval0(arg, tv, nextcmd, EVAL_EVALUATE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100467 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000468
469 return tv;
470}
471
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100473 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200474 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
475 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000476 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200478 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100479call_vim_function(
480 char_u *func,
481 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200482 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200483 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000485 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200486 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100488 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200489 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200490 funcexe.firstline = curwin->w_cursor.lnum;
491 funcexe.lastline = curwin->w_cursor.lnum;
492 funcexe.evaluate = TRUE;
493 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000494 if (ret == FAIL)
495 clear_tv(rettv);
496
497 return ret;
498}
499
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100500/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100501 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100502 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200503 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
504 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100505 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200506 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100507call_func_retnr(
508 char_u *func,
509 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200510 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100511{
512 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200513 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100514
Bram Moolenaarded27a12018-08-01 19:06:03 +0200515 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100516 return -1;
517
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100518 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100519 clear_tv(&rettv);
520 return retval;
521}
522
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000523/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100524 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000525 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200526 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
527 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000528 */
529 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100530call_func_retstr(
531 char_u *func,
532 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200533 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000534{
535 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000536 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000537
Bram Moolenaarded27a12018-08-01 19:06:03 +0200538 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000539 return NULL;
540
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100541 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000542 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 return retval;
544}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000545
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000546/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100547 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200548 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
549 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000550 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000551 */
552 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100553call_func_retlist(
554 char_u *func,
555 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200556 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000557{
558 typval_T rettv;
559
Bram Moolenaarded27a12018-08-01 19:06:03 +0200560 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000561 return NULL;
562
563 if (rettv.v_type != VAR_LIST)
564 {
565 clear_tv(&rettv);
566 return NULL;
567 }
568
569 return rettv.vval.v_list;
570}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572#ifdef FEAT_FOLDING
573/*
574 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
575 * it in "*cp". Doesn't give error messages.
576 */
577 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100578eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579{
Bram Moolenaar33570922005-01-25 22:26:29 +0000580 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200581 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000583 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
584 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585
586 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000587 if (use_sandbox)
588 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200589 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 *cp = NUL;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200591 if (eval0(arg, &tv, NULL, EVAL_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 retval = 0;
593 else
594 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100595 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000596 if (tv.v_type == VAR_NUMBER)
597 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000598 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 retval = 0;
600 else
601 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100602 // If the result is a string, check if there is a non-digit before
603 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000604 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 if (!VIM_ISDIGIT(*s) && *s != '-')
606 *cp = *s++;
607 retval = atol((char *)s);
608 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000609 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 }
611 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000612 if (use_sandbox)
613 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200614 --textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200616 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617}
618#endif
619
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000621 * Get an lval: variable, Dict item or List item that can be assigned a value
622 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
623 * "name.key", "name.key[expr]" etc.
624 * Indexing only works if "name" is an existing List or Dictionary.
625 * "name" points to the start of the name.
626 * If "rettv" is not NULL it points to the value to be assigned.
627 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
628 * wrong; must end in space or cmd separator.
629 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100630 * flags:
631 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100632 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100633 * GLV_NO_AUTOLOAD: do not use script autoloading
634 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000635 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000636 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000637 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000638 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200639 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100640get_lval(
641 char_u *name,
642 typval_T *rettv,
643 lval_T *lp,
644 int unlet,
645 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100646 int flags, // GLV_ values
647 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000648{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000649 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000650 char_u *expr_start, *expr_end;
651 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000652 dictitem_T *v;
653 typval_T var1;
654 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000655 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000656 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000657 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000658 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +0000659 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100660 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000661
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100662 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200663 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000664
665 if (skip)
666 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100667 // When skipping just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000668 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000669 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000670 }
671
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100672 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000673 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100674 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000675 if (expr_start != NULL)
676 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100677 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100678 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000679 && *p != '[' && *p != '.')
680 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100681 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000682 return NULL;
683 }
684
685 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
686 if (lp->ll_exp_name == NULL)
687 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100688 // Report an invalid expression in braces, unless the
689 // expression evaluation has been cancelled due to an
690 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000691 if (!aborting() && !quiet)
692 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000693 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100694 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000695 return NULL;
696 }
697 }
698 lp->ll_name = lp->ll_exp_name;
699 }
700 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100701 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000702 lp->ll_name = name;
703
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100704 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9 && *p == ':')
705 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100706 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100707 char_u *tp = skipwhite(p + 1);
708
709 // parse the type after the name
710 lp->ll_type = parse_type(&tp, &si->sn_type_list);
711 lp->ll_name_end = tp;
712 }
713 }
714
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100715 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000716 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
717 return p;
718
719 cc = *p;
720 *p = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100721 // Only pass &ht when we would write to the variable, it prevents autoload
722 // as well.
Bram Moolenaar6e65d592017-12-07 22:11:27 +0100723 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
724 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000725 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100726 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000727 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000728 if (v == NULL)
729 return NULL;
730
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000731 /*
732 * Loop until no more [idx] or .key is following.
733 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000734 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100735 var1.v_type = VAR_UNKNOWN;
736 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000737 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000738 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000739 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
740 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100741 && lp->ll_tv->vval.v_dict != NULL)
742 && !(lp->ll_tv->v_type == VAR_BLOB
743 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000744 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000745 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100746 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000747 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000748 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000749 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000750 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000751 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100752 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000753 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000754 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000755
Bram Moolenaar8c711452005-01-14 21:53:12 +0000756 len = -1;
757 if (*p == '.')
758 {
759 key = p + 1;
760 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
761 ;
762 if (len == 0)
763 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000764 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100765 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000766 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000767 }
768 p = key + len;
769 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000770 else
771 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100772 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000773 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000774 if (*p == ':')
775 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000776 else
777 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000778 empty1 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200779 if (eval1(&p, &var1, EVAL_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000780 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100781 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000782 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100783 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000784 clear_tv(&var1);
785 return NULL;
786 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000787 }
788
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100789 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000790 if (*p == ':')
791 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000792 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000793 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000794 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100795 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100796 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000797 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000798 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100799 if (rettv != NULL
800 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100801 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100802 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100803 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +0000804 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000805 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100806 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100807 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000808 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000809 }
810 p = skipwhite(p + 1);
811 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000812 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000813 else
814 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000815 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200816 // recursive!
817 if (eval1(&p, &var2, EVAL_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000818 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100819 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000820 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000821 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100822 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000823 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100824 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100825 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000826 clear_tv(&var2);
827 return NULL;
828 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000829 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000830 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000831 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000832 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000833 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000834
Bram Moolenaar8c711452005-01-14 21:53:12 +0000835 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000836 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000837 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100838 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100839 clear_tv(&var1);
840 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000841 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000842 }
843
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100844 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000845 ++p;
846 }
847
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000848 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000849 {
850 if (len == -1)
851 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100852 // "[key]": get key from "var1"
853 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +0200854 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000855 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000856 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000857 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000858 }
859 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000860 lp->ll_list = NULL;
861 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000862 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200863
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100864 // When assigning to a scope dictionary check that a function and
865 // variable name is valid (only variable name unless it is l: or
866 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200867 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200868 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200869 int prevval;
870 int wrong;
871
872 if (len != -1)
873 {
874 prevval = key[len];
875 key[len] = NUL;
876 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +0200877 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100878 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200879 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
880 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200881 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200882 || !valid_varname(key);
883 if (len != -1)
884 key[len] = prevval;
885 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200886 {
887 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200888 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200889 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200890 }
891
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000892 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000893 {
Bram Moolenaar31b81602019-02-10 22:14:27 +0100894 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200895 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +0100896 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200897 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100898 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +0100899 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200900 return NULL;
901 }
902
Bram Moolenaar31b81602019-02-10 22:14:27 +0100903 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000904 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000905 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000906 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100907 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100908 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000909 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000910 }
911 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000912 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000913 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000914 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100915 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000916 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000917 p = NULL;
918 break;
919 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100920 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +0100921 else if ((flags & GLV_READ_ONLY) == 0
922 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +0100923 {
924 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200925 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +0100926 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200927
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100928 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000929 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000930 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100931 else if (lp->ll_tv->v_type == VAR_BLOB)
932 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100933 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
934
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100935 /*
936 * Get the number and item for the only or first index of the List.
937 */
938 if (empty1)
939 lp->ll_n1 = 0;
940 else
941 // is number or string
942 lp->ll_n1 = (long)tv_get_number(&var1);
943 clear_tv(&var1);
944
945 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100946 || lp->ll_n1 > bloblen
947 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100948 {
949 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100950 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100951 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100952 return NULL;
953 }
954 if (lp->ll_range && !lp->ll_empty2)
955 {
956 lp->ll_n2 = (long)tv_get_number(&var2);
957 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100958 if (lp->ll_n2 < 0
959 || lp->ll_n2 >= bloblen
960 || lp->ll_n2 < lp->ll_n1)
961 {
962 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100963 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100964 return NULL;
965 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100966 }
967 lp->ll_blob = lp->ll_tv->vval.v_blob;
968 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +0100969 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100970 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000971 else
972 {
973 /*
974 * Get the number and item for the only or first index of the List.
975 */
976 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000977 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000978 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100979 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100980 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100981 clear_tv(&var1);
982
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000983 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000984 lp->ll_list = lp->ll_tv->vval.v_list;
985 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
986 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000987 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000988 if (lp->ll_n1 < 0)
989 {
990 lp->ll_n1 = 0;
991 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
992 }
993 }
994 if (lp->ll_li == NULL)
995 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100996 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +0200997 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100998 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000999 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001000 }
1001
1002 /*
1003 * May need to find the item or absolute index for the second
1004 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001005 * When no index given: "lp->ll_empty2" is TRUE.
1006 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001007 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001008 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001009 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001010 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001011 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001012 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001013 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001014 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001015 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001016 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001017 {
1018 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001019 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001020 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001021 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001022 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001023 }
1024
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001025 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001026 if (lp->ll_n1 < 0)
1027 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1028 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001029 {
1030 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001031 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001032 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001033 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001034 }
1035
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001036 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001037 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001038 }
1039
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001040 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001041 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001042 return p;
1043}
1044
1045/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001046 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001047 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001048 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001049clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001050{
1051 vim_free(lp->ll_exp_name);
1052 vim_free(lp->ll_newkey);
1053}
1054
1055/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001056 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001057 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001058 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1059 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001060 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001061 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001062set_var_lval(
1063 lval_T *lp,
1064 char_u *endp,
1065 typval_T *rettv,
1066 int copy,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001067 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar7454a062016-01-30 15:14:10 +01001068 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001069{
1070 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001071 listitem_T *ri;
1072 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001073
1074 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001075 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001076 cc = *endp;
1077 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001078 if (lp->ll_blob != NULL)
1079 {
1080 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001081
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001082 if (op != NULL && *op != '=')
1083 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001084 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001085 return;
1086 }
1087
1088 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1089 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001090 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001091
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001092 if (lp->ll_empty2)
1093 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1094
1095 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001096 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001097 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001098 return;
1099 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001100 if (lp->ll_empty2)
1101 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001102
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001103 ir = 0;
1104 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1105 blob_set(lp->ll_blob, il,
1106 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001107 }
1108 else
1109 {
1110 val = (int)tv_get_number_chk(rettv, &error);
1111 if (!error)
1112 {
1113 garray_T *gap = &lp->ll_blob->bv_ga;
1114
1115 // Allow for appending a byte. Setting a byte beyond
1116 // the end is an error otherwise.
1117 if (lp->ll_n1 < gap->ga_len
1118 || (lp->ll_n1 == gap->ga_len
1119 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1120 {
1121 blob_set(lp->ll_blob, lp->ll_n1, val);
1122 if (lp->ll_n1 == gap->ga_len)
1123 ++gap->ga_len;
1124 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001125 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001126 }
1127 }
1128 }
1129 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001130 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001131 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001132
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001133 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001134 {
1135 emsg(_(e_cannot_mod));
1136 *endp = cc;
1137 return;
1138 }
1139
Bram Moolenaarff697e62019-02-12 22:28:33 +01001140 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001141 di = NULL;
1142 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
1143 &tv, &di, TRUE, FALSE) == OK)
1144 {
1145 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001146 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1147 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001148 && tv_op(&tv, rettv, op) == OK)
1149 set_var(lp->ll_name, &tv, FALSE);
1150 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001151 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001152 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001153 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001154 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001155 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001156 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001157 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001158 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001159 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001160 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001161 else if (lp->ll_range)
1162 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001163 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001164 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001165
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001166 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001167 {
1168 emsg(_("E996: Cannot lock a range"));
1169 return;
1170 }
1171
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001172 /*
1173 * Check whether any of the list items is locked
1174 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001175 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001176 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001177 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001178 return;
1179 ri = ri->li_next;
1180 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1181 break;
1182 ll_li = ll_li->li_next;
1183 ++ll_n1;
1184 }
1185
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001186 /*
1187 * Assign the List values to the list items.
1188 */
1189 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001190 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001191 if (op != NULL && *op != '=')
1192 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1193 else
1194 {
1195 clear_tv(&lp->ll_li->li_tv);
1196 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1197 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001198 ri = ri->li_next;
1199 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1200 break;
1201 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001202 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001203 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001204 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001205 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001206 ri = NULL;
1207 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001208 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001209 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001210 lp->ll_li = lp->ll_li->li_next;
1211 ++lp->ll_n1;
1212 }
1213 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001214 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001215 else if (lp->ll_empty2
1216 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001217 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001218 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001219 }
1220 else
1221 {
1222 /*
1223 * Assign to a List or Dictionary item.
1224 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001225 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001226 {
1227 emsg(_("E996: Cannot lock a list or dict"));
1228 return;
1229 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001230 if (lp->ll_newkey != NULL)
1231 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001232 if (op != NULL && *op != '=')
1233 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001234 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001235 return;
1236 }
1237
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001238 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001239 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001240 if (di == NULL)
1241 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001242 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1243 {
1244 vim_free(di);
1245 return;
1246 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001247 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001248 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001249 else if (op != NULL && *op != '=')
1250 {
1251 tv_op(lp->ll_tv, rettv, op);
1252 return;
1253 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001254 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001255 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001256
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001257 /*
1258 * Assign the value to the variable or list item.
1259 */
1260 if (copy)
1261 copy_tv(rettv, lp->ll_tv);
1262 else
1263 {
1264 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001265 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001266 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001267 }
1268 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001269}
1270
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001271/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001272 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1273 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001274 * Returns OK or FAIL.
1275 */
1276 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001277tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001278{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001279 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001280 char_u numbuf[NUMBUFLEN];
1281 char_u *s;
1282
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001283 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001284 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001285 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001286 {
1287 switch (tv1->v_type)
1288 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001289 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001290 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001291 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001292 case VAR_DICT:
1293 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001294 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001295 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001296 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001297 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001298 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001299 break;
1300
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001301 case VAR_BLOB:
1302 if (*op != '+' || tv2->v_type != VAR_BLOB)
1303 break;
1304 // BLOB += BLOB
1305 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1306 {
1307 blob_T *b1 = tv1->vval.v_blob;
1308 blob_T *b2 = tv2->vval.v_blob;
1309 int i, len = blob_len(b2);
1310 for (i = 0; i < len; i++)
1311 ga_append(&b1->bv_ga, blob_get(b2, i));
1312 }
1313 return OK;
1314
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001315 case VAR_LIST:
1316 if (*op != '+' || tv2->v_type != VAR_LIST)
1317 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001318 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001319 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
1320 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1321 return OK;
1322
1323 case VAR_NUMBER:
1324 case VAR_STRING:
1325 if (tv2->v_type == VAR_LIST)
1326 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001327 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001328 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001329 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001330 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001331#ifdef FEAT_FLOAT
1332 if (tv2->v_type == VAR_FLOAT)
1333 {
1334 float_T f = n;
1335
Bram Moolenaarff697e62019-02-12 22:28:33 +01001336 if (*op == '%')
1337 break;
1338 switch (*op)
1339 {
1340 case '+': f += tv2->vval.v_float; break;
1341 case '-': f -= tv2->vval.v_float; break;
1342 case '*': f *= tv2->vval.v_float; break;
1343 case '/': f /= tv2->vval.v_float; break;
1344 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001345 clear_tv(tv1);
1346 tv1->v_type = VAR_FLOAT;
1347 tv1->vval.v_float = f;
1348 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001349 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001350#endif
1351 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001352 switch (*op)
1353 {
1354 case '+': n += tv_get_number(tv2); break;
1355 case '-': n -= tv_get_number(tv2); break;
1356 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001357 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1358 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001359 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001360 clear_tv(tv1);
1361 tv1->v_type = VAR_NUMBER;
1362 tv1->vval.v_number = n;
1363 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001364 }
1365 else
1366 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001367 if (tv2->v_type == VAR_FLOAT)
1368 break;
1369
Bram Moolenaarff697e62019-02-12 22:28:33 +01001370 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001371 s = tv_get_string(tv1);
1372 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001373 clear_tv(tv1);
1374 tv1->v_type = VAR_STRING;
1375 tv1->vval.v_string = s;
1376 }
1377 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001378
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001379 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001380#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001381 {
1382 float_T f;
1383
Bram Moolenaarff697e62019-02-12 22:28:33 +01001384 if (*op == '%' || *op == '.'
1385 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001386 && tv2->v_type != VAR_NUMBER
1387 && tv2->v_type != VAR_STRING))
1388 break;
1389 if (tv2->v_type == VAR_FLOAT)
1390 f = tv2->vval.v_float;
1391 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001392 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001393 switch (*op)
1394 {
1395 case '+': tv1->vval.v_float += f; break;
1396 case '-': tv1->vval.v_float -= f; break;
1397 case '*': tv1->vval.v_float *= f; break;
1398 case '/': tv1->vval.v_float /= f; break;
1399 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001400 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001401#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001402 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001403 }
1404 }
1405
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001406 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001407 return FAIL;
1408}
1409
1410/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001411 * Evaluate the expression used in a ":for var in expr" command.
1412 * "arg" points to "var".
1413 * Set "*errp" to TRUE for an error, FALSE otherwise;
1414 * Return a pointer that holds the info. Null when there is an error.
1415 */
1416 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001417eval_for_line(
1418 char_u *arg,
1419 int *errp,
1420 char_u **nextcmdp,
1421 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001422{
Bram Moolenaar33570922005-01-25 22:26:29 +00001423 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001424 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001425 typval_T tv;
1426 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001427
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001428 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001429
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001430 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001431 if (fi == NULL)
1432 return NULL;
1433
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001434 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001435 if (expr == NULL)
1436 return fi;
1437
1438 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001439 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001440 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001441 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001442 return fi;
1443 }
1444
1445 if (skip)
1446 ++emsg_skip;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001447 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, skip ? 0 : EVAL_EVALUATE)
1448 == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001449 {
1450 *errp = FALSE;
1451 if (!skip)
1452 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001453 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001454 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001455 l = tv.vval.v_list;
1456 if (l == NULL)
1457 {
1458 // a null list is like an empty list: do nothing
1459 clear_tv(&tv);
1460 }
1461 else
1462 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001463 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001464 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001465
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001466 // No need to increment the refcount, it's already set for
1467 // the list being used in "tv".
1468 fi->fi_list = l;
1469 list_add_watch(l, &fi->fi_lw);
1470 fi->fi_lw.lw_item = l->lv_first;
1471 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001472 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001473 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001474 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001475 fi->fi_bi = 0;
1476 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001477 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001478 typval_T btv;
1479
1480 // Make a copy, so that the iteration still works when the
1481 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001482 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001483 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001484 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001485 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001486 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001487 else
1488 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001489 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001490 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001491 }
1492 }
1493 }
1494 if (skip)
1495 --emsg_skip;
1496
1497 return fi;
1498}
1499
1500/*
1501 * Use the first item in a ":for" list. Advance to the next.
1502 * Assign the values to the variable (list). "arg" points to the first one.
1503 * Return TRUE when a valid item was found, FALSE when at end of list or
1504 * something wrong.
1505 */
1506 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001507next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001508{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001509 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001510 int result;
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001511 int flag = current_sctx.sc_version == SCRIPT_VERSION_VIM9 ?
1512 LET_NO_COMMAND : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001513 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001514
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001515 if (fi->fi_blob != NULL)
1516 {
1517 typval_T tv;
1518
1519 if (fi->fi_bi >= blob_len(fi->fi_blob))
1520 return FALSE;
1521 tv.v_type = VAR_NUMBER;
1522 tv.v_lock = VAR_FIXED;
1523 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1524 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001525 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001526 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001527 }
1528
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001529 item = fi->fi_lw.lw_item;
1530 if (item == NULL)
1531 result = FALSE;
1532 else
1533 {
1534 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001535 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001536 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001537 }
1538 return result;
1539}
1540
1541/*
1542 * Free the structure used to store info used by ":for".
1543 */
1544 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001545free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001546{
Bram Moolenaar33570922005-01-25 22:26:29 +00001547 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001548
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001549 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001550 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001551 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001552 list_unref(fi->fi_list);
1553 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001554 if (fi != NULL && fi->fi_blob != NULL)
1555 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001556 vim_free(fi);
1557}
1558
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001560set_context_for_expression(
1561 expand_T *xp,
1562 char_u *arg,
1563 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564{
1565 int got_eq = FALSE;
1566 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001567 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001569 if (cmdidx == CMD_let || cmdidx == CMD_const)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001570 {
1571 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001572 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001573 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001574 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001575 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001576 {
1577 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001578 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001579 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001580 break;
1581 }
1582 return;
1583 }
1584 }
1585 else
1586 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1587 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 while ((xp->xp_pattern = vim_strpbrk(arg,
1589 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1590 {
1591 c = *xp->xp_pattern;
1592 if (c == '&')
1593 {
1594 c = xp->xp_pattern[1];
1595 if (c == '&')
1596 {
1597 ++xp->xp_pattern;
1598 xp->xp_context = cmdidx != CMD_let || got_eq
1599 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1600 }
1601 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001602 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001604 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1605 xp->xp_pattern += 2;
1606
1607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 }
1609 else if (c == '$')
1610 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001611 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 xp->xp_context = EXPAND_ENV_VARS;
1613 }
1614 else if (c == '=')
1615 {
1616 got_eq = TRUE;
1617 xp->xp_context = EXPAND_EXPRESSION;
1618 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001619 else if (c == '#'
1620 && xp->xp_context == EXPAND_EXPRESSION)
1621 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001622 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001623 break;
1624 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001625 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 && xp->xp_context == EXPAND_FUNCTIONS
1627 && vim_strchr(xp->xp_pattern, '(') == NULL)
1628 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001629 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 break;
1631 }
1632 else if (cmdidx != CMD_let || got_eq)
1633 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001634 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 {
1636 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1637 if (c == '\\' && xp->xp_pattern[1] != NUL)
1638 ++xp->xp_pattern;
1639 xp->xp_context = EXPAND_NOTHING;
1640 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001641 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001643 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1645 /* skip */ ;
1646 xp->xp_context = EXPAND_NOTHING;
1647 }
1648 else if (c == '|')
1649 {
1650 if (xp->xp_pattern[1] == '|')
1651 {
1652 ++xp->xp_pattern;
1653 xp->xp_context = EXPAND_EXPRESSION;
1654 }
1655 else
1656 xp->xp_context = EXPAND_COMMANDS;
1657 }
1658 else
1659 xp->xp_context = EXPAND_EXPRESSION;
1660 }
1661 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001662 // Doesn't look like something valid, expand as an expression
1663 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001664 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 arg = xp->xp_pattern;
1666 if (*arg != NUL)
1667 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1668 /* skip */ ;
1669 }
1670 xp->xp_pattern = arg;
1671}
1672
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001674 * Return TRUE if "pat" matches "text".
1675 * Does not use 'cpo' and always uses 'magic'.
1676 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001677 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001678pattern_match(char_u *pat, char_u *text, int ic)
1679{
1680 int matches = FALSE;
1681 char_u *save_cpo;
1682 regmatch_T regmatch;
1683
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001684 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001685 save_cpo = p_cpo;
1686 p_cpo = (char_u *)"";
1687 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1688 if (regmatch.regprog != NULL)
1689 {
1690 regmatch.rm_ic = ic;
1691 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1692 vim_regfree(regmatch.regprog);
1693 }
1694 p_cpo = save_cpo;
1695 return matches;
1696}
1697
1698/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001699 * Handle a name followed by "(". Both for just "name(arg)" and for
1700 * "expr->name(arg)".
1701 * Returns OK or FAIL.
1702 */
1703 static int
1704eval_func(
1705 char_u **arg, // points to "(", will be advanced
1706 char_u *name,
1707 int name_len,
1708 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001709 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001710 typval_T *basetv) // "expr" for "expr->name(arg)"
1711{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001712 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001713 char_u *s = name;
1714 int len = name_len;
1715 partial_T *partial;
1716 int ret = OK;
1717
1718 if (!evaluate)
1719 check_vars(s, len);
1720
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001721 // If "s" is the name of a variable of type VAR_FUNC
1722 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001723 s = deref_func_name(s, &len, &partial, !evaluate);
1724
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001725 // Need to make a copy, in case evaluating the arguments makes
1726 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001727 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001728 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001729 ret = FAIL;
1730 else
1731 {
1732 funcexe_T funcexe;
1733
1734 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001735 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001736 funcexe.firstline = curwin->w_cursor.lnum;
1737 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001738 funcexe.evaluate = evaluate;
1739 funcexe.partial = partial;
1740 funcexe.basetv = basetv;
1741 ret = get_func_tv(s, len, rettv, arg, &funcexe);
1742 }
1743 vim_free(s);
1744
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001745 // If evaluate is FALSE rettv->v_type was not set in
1746 // get_func_tv, but it's needed in handle_subscript() to parse
1747 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001748 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
1749 {
1750 rettv->vval.v_string = NULL;
1751 rettv->v_type = VAR_FUNC;
1752 }
1753
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001754 // Stop the expression evaluation when immediately
1755 // aborting on error, or when an interrupt occurred or
1756 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001757 if (evaluate && aborting())
1758 {
1759 if (ret == OK)
1760 clear_tv(rettv);
1761 ret = FAIL;
1762 }
1763 return ret;
1764}
1765
1766/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001768 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
1770 */
1771
1772/*
1773 * Handle zero level expression.
1774 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001775 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001776 * Note: "rettv.v_lock" is not set.
Bram Moolenaar32e35112020-05-14 22:41:15 +02001777 * "flags" has EVAL_EVALUATE and similar flags.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 * Return OK or FAIL.
1779 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001780 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001781eval0(
1782 char_u *arg,
1783 typval_T *rettv,
1784 char_u **nextcmd,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001785 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786{
1787 int ret;
1788 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001789 int did_emsg_before = did_emsg;
1790 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791
1792 p = skipwhite(arg);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001793 ret = eval1(&p, rettv, flags);
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001794 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 {
1796 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001797 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 /*
1799 * Report the invalid expression unless the expression evaluation has
1800 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001801 * exception, or we already gave a more specific error.
1802 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02001804 if (!aborting()
1805 && did_emsg == did_emsg_before
1806 && called_emsg == called_emsg_before
1807 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001808 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 ret = FAIL;
1810 }
1811 if (nextcmd != NULL)
1812 *nextcmd = check_nextcmd(p);
1813
1814 return ret;
1815}
1816
1817/*
1818 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00001819 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 *
1821 * "arg" must point to the first non-white of the expression.
1822 * "arg" is advanced to the next non-white after the recognized expression.
1823 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00001824 * Note: "rettv.v_lock" is not set.
1825 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 * Return OK or FAIL.
1827 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02001828 int
Bram Moolenaar32e35112020-05-14 22:41:15 +02001829eval1(char_u **arg, typval_T *rettv, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830{
1831 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00001832 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833
1834 /*
1835 * Get the first variable.
1836 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02001837 if (eval2(arg, rettv, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 return FAIL;
1839
1840 if ((*arg)[0] == '?')
1841 {
Bram Moolenaar32e35112020-05-14 22:41:15 +02001842 int evaluate = flags & EVAL_EVALUATE;
1843
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 result = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001845 if (flags & EVAL_EVALUATE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001847 int error = FALSE;
1848
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001849 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001851 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001852 if (error)
1853 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 }
1855
1856 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02001857 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 */
1859 *arg = skipwhite(*arg + 1);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001860 if (eval1(arg, rettv, result ? flags : flags & ~EVAL_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 return FAIL;
1862
1863 /*
1864 * Check for the ":".
1865 */
1866 if ((*arg)[0] != ':')
1867 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001868 emsg(_(e_missing_colon));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001870 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 return FAIL;
1872 }
1873
1874 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02001875 * Get the third variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 */
1877 *arg = skipwhite(*arg + 1);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001878 if (eval1(arg, &var2, !result ? flags : flags & ~EVAL_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 {
1880 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001881 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 return FAIL;
1883 }
1884 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001885 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 }
1887
1888 return OK;
1889}
1890
1891/*
1892 * Handle first level expression:
1893 * expr2 || expr2 || expr2 logical OR
1894 *
1895 * "arg" must point to the first non-white of the expression.
1896 * "arg" is advanced to the next non-white after the recognized expression.
1897 *
1898 * Return OK or FAIL.
1899 */
1900 static int
Bram Moolenaar32e35112020-05-14 22:41:15 +02001901eval2(char_u **arg, typval_T *rettv, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902{
Bram Moolenaar33570922005-01-25 22:26:29 +00001903 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 long result;
1905 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001906 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907
1908 /*
1909 * Get the first variable.
1910 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02001911 if (eval3(arg, rettv, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 return FAIL;
1913
1914 /*
1915 * Repeat until there is no following "||".
1916 */
1917 first = TRUE;
1918 result = FALSE;
1919 while ((*arg)[0] == '|' && (*arg)[1] == '|')
1920 {
Bram Moolenaar32e35112020-05-14 22:41:15 +02001921 int evaluate = flags & EVAL_EVALUATE;
1922
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 if (evaluate && first)
1924 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001925 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001927 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001928 if (error)
1929 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 first = FALSE;
1931 }
1932
1933 /*
1934 * Get the second variable.
1935 */
1936 *arg = skipwhite(*arg + 2);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001937 if (eval3(arg, &var2, !result ? flags : flags & ~EVAL_EVALUATE)
1938 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 return FAIL;
1940
1941 /*
1942 * Compute the result.
1943 */
1944 if (evaluate && !result)
1945 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001946 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001948 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001949 if (error)
1950 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 }
1952 if (evaluate)
1953 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001954 rettv->v_type = VAR_NUMBER;
1955 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 }
1957 }
1958
1959 return OK;
1960}
1961
1962/*
1963 * Handle second level expression:
1964 * expr3 && expr3 && expr3 logical AND
1965 *
1966 * "arg" must point to the first non-white of the expression.
1967 * "arg" is advanced to the next non-white after the recognized expression.
1968 *
1969 * Return OK or FAIL.
1970 */
1971 static int
Bram Moolenaar32e35112020-05-14 22:41:15 +02001972eval3(char_u **arg, typval_T *rettv, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973{
Bram Moolenaar33570922005-01-25 22:26:29 +00001974 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 long result;
1976 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001977 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978
1979 /*
1980 * Get the first variable.
1981 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02001982 if (eval4(arg, rettv, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 return FAIL;
1984
1985 /*
1986 * Repeat until there is no following "&&".
1987 */
1988 first = TRUE;
1989 result = TRUE;
1990 while ((*arg)[0] == '&' && (*arg)[1] == '&')
1991 {
Bram Moolenaar32e35112020-05-14 22:41:15 +02001992 int evaluate = flags & EVAL_EVALUATE;
1993
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 if (evaluate && first)
1995 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001996 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001998 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001999 if (error)
2000 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 first = FALSE;
2002 }
2003
2004 /*
2005 * Get the second variable.
2006 */
2007 *arg = skipwhite(*arg + 2);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002008 if (eval4(arg, &var2, result ? flags : flags & ~EVAL_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 return FAIL;
2010
2011 /*
2012 * Compute the result.
2013 */
2014 if (evaluate && result)
2015 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002016 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002018 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002019 if (error)
2020 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 }
2022 if (evaluate)
2023 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002024 rettv->v_type = VAR_NUMBER;
2025 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 }
2027 }
2028
2029 return OK;
2030}
2031
2032/*
2033 * Handle third level expression:
2034 * var1 == var2
2035 * var1 =~ var2
2036 * var1 != var2
2037 * var1 !~ var2
2038 * var1 > var2
2039 * var1 >= var2
2040 * var1 < var2
2041 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002042 * var1 is var2
2043 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 *
2045 * "arg" must point to the first non-white of the expression.
2046 * "arg" is advanced to the next non-white after the recognized expression.
2047 *
2048 * Return OK or FAIL.
2049 */
2050 static int
Bram Moolenaar32e35112020-05-14 22:41:15 +02002051eval4(char_u **arg, typval_T *rettv, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052{
Bram Moolenaar33570922005-01-25 22:26:29 +00002053 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 char_u *p;
2055 int i;
Bram Moolenaar87396072019-12-31 22:36:18 +01002056 exptype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059
2060 /*
2061 * Get the first variable.
2062 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002063 if (eval5(arg, rettv, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 return FAIL;
2065
2066 p = *arg;
2067 switch (p[0])
2068 {
2069 case '=': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002070 type = EXPR_EQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002072 type = EXPR_MATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 break;
2074 case '!': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002075 type = EXPR_NEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002077 type = EXPR_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 break;
2079 case '>': if (p[1] != '=')
2080 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002081 type = EXPR_GREATER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 len = 1;
2083 }
2084 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002085 type = EXPR_GEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 break;
2087 case '<': if (p[1] != '=')
2088 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002089 type = EXPR_SMALLER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 len = 1;
2091 }
2092 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002093 type = EXPR_SEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002095 case 'i': if (p[1] == 's')
2096 {
2097 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2098 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02002099 i = p[len];
2100 if (!isalnum(i) && i != '_')
Bram Moolenaar87396072019-12-31 22:36:18 +01002101 type = len == 2 ? EXPR_IS : EXPR_ISNOT;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002102 }
2103 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 }
2105
2106 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002107 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002109 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002111 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 if (p[len] == '?')
2113 {
2114 ic = TRUE;
2115 ++len;
2116 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002117 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 else if (p[len] == '#')
2119 {
2120 ic = FALSE;
2121 ++len;
2122 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002123 // nothing appended: use 'ignorecase'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 else
2125 ic = p_ic;
2126
2127 /*
2128 * Get the second variable.
2129 */
2130 *arg = skipwhite(p + len);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002131 if (eval5(arg, &var2, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002133 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 return FAIL;
2135 }
Bram Moolenaar32e35112020-05-14 22:41:15 +02002136 if (flags & EVAL_EVALUATE)
Bram Moolenaar31988702018-02-13 12:57:42 +01002137 {
Bram Moolenaar07a3db82019-12-25 18:14:14 +01002138 int ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002139
2140 clear_tv(&var2);
2141 return ret;
2142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 }
2144
2145 return OK;
2146}
2147
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002148 void
2149eval_addblob(typval_T *tv1, typval_T *tv2)
2150{
2151 blob_T *b1 = tv1->vval.v_blob;
2152 blob_T *b2 = tv2->vval.v_blob;
2153 blob_T *b = blob_alloc();
2154 int i;
2155
2156 if (b != NULL)
2157 {
2158 for (i = 0; i < blob_len(b1); i++)
2159 ga_append(&b->bv_ga, blob_get(b1, i));
2160 for (i = 0; i < blob_len(b2); i++)
2161 ga_append(&b->bv_ga, blob_get(b2, i));
2162
2163 clear_tv(tv1);
2164 rettv_blob_set(tv1, b);
2165 }
2166}
2167
2168 int
2169eval_addlist(typval_T *tv1, typval_T *tv2)
2170{
2171 typval_T var3;
2172
2173 // concatenate Lists
2174 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2175 {
2176 clear_tv(tv1);
2177 clear_tv(tv2);
2178 return FAIL;
2179 }
2180 clear_tv(tv1);
2181 *tv1 = var3;
2182 return OK;
2183}
2184
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185/*
2186 * Handle fourth level expression:
2187 * + number addition
2188 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002189 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002190 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 *
2192 * "arg" must point to the first non-white of the expression.
2193 * "arg" is advanced to the next non-white after the recognized expression.
2194 *
2195 * Return OK or FAIL.
2196 */
2197 static int
Bram Moolenaar32e35112020-05-14 22:41:15 +02002198eval5(char_u **arg, typval_T *rettv, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199{
Bram Moolenaar33570922005-01-25 22:26:29 +00002200 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002202 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002203#ifdef FEAT_FLOAT
2204 float_T f1 = 0, f2 = 0;
2205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 char_u *s1, *s2;
2207 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2208 char_u *p;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002209 int concat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210
2211 /*
2212 * Get the first variable.
2213 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002214 if (eval6(arg, rettv, flags, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 return FAIL;
2216
2217 /*
2218 * Repeat computing, until no '+', '-' or '.' is following.
2219 */
2220 for (;;)
2221 {
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002222 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 op = **arg;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002224 concat = op == '.'
2225 && (*(*arg + 1) == '.' || current_sctx.sc_version < 2);
2226 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 break;
2228
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002229 if ((op != '+' || (rettv->v_type != VAR_LIST
2230 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002231#ifdef FEAT_FLOAT
2232 && (op == '.' || rettv->v_type != VAR_FLOAT)
2233#endif
2234 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002235 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002236 // For "list + ...", an illegal use of the first operand as
2237 // a number cannot be determined before evaluating the 2nd
2238 // operand: if this is also a list, all is ok.
2239 // For "something . ...", "something - ..." or "non-list + ...",
2240 // we know that the first operand needs to be a string or number
2241 // without evaluating the 2nd operand. So check before to avoid
2242 // side effects after an error.
Bram Moolenaar32e35112020-05-14 22:41:15 +02002243 if ((flags & EVAL_EVALUATE) && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002244 {
2245 clear_tv(rettv);
2246 return FAIL;
2247 }
2248 }
2249
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 /*
2251 * Get the second variable.
2252 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002253 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
2254 ++*arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 *arg = skipwhite(*arg + 1);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002256 if (eval6(arg, &var2, flags, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002258 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 return FAIL;
2260 }
2261
Bram Moolenaar32e35112020-05-14 22:41:15 +02002262 if (flags & EVAL_EVALUATE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 {
2264 /*
2265 * Compute the result.
2266 */
2267 if (op == '.')
2268 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002269 s1 = tv_get_string_buf(rettv, buf1); // already checked
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002270 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002271 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002272 {
2273 clear_tv(rettv);
2274 clear_tv(&var2);
2275 return FAIL;
2276 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002277 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002278 clear_tv(rettv);
2279 rettv->v_type = VAR_STRING;
2280 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002282 else if (op == '+' && rettv->v_type == VAR_BLOB
2283 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002284 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002285 else if (op == '+' && rettv->v_type == VAR_LIST
2286 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002287 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002288 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002289 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 else
2292 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002293 int error = FALSE;
2294
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002295#ifdef FEAT_FLOAT
2296 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002297 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002298 f1 = rettv->vval.v_float;
2299 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002300 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002301 else
2302#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002303 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002304 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002305 if (error)
2306 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002307 // This can only happen for "list + non-list". For
2308 // "non-list + ..." or "something - ...", we returned
2309 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002310 clear_tv(rettv);
2311 return FAIL;
2312 }
2313#ifdef FEAT_FLOAT
2314 if (var2.v_type == VAR_FLOAT)
2315 f1 = n1;
2316#endif
2317 }
2318#ifdef FEAT_FLOAT
2319 if (var2.v_type == VAR_FLOAT)
2320 {
2321 f2 = var2.vval.v_float;
2322 n2 = 0;
2323 }
2324 else
2325#endif
2326 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002327 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002328 if (error)
2329 {
2330 clear_tv(rettv);
2331 clear_tv(&var2);
2332 return FAIL;
2333 }
2334#ifdef FEAT_FLOAT
2335 if (rettv->v_type == VAR_FLOAT)
2336 f2 = n2;
2337#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002338 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002339 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002340
2341#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002342 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002343 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2344 {
2345 if (op == '+')
2346 f1 = f1 + f2;
2347 else
2348 f1 = f1 - f2;
2349 rettv->v_type = VAR_FLOAT;
2350 rettv->vval.v_float = f1;
2351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002353#endif
2354 {
2355 if (op == '+')
2356 n1 = n1 + n2;
2357 else
2358 n1 = n1 - n2;
2359 rettv->v_type = VAR_NUMBER;
2360 rettv->vval.v_number = n1;
2361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002363 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 }
2365 }
2366 return OK;
2367}
2368
2369/*
2370 * Handle fifth level expression:
2371 * * number multiplication
2372 * / number division
2373 * % number modulo
2374 *
2375 * "arg" must point to the first non-white of the expression.
2376 * "arg" is advanced to the next non-white after the recognized expression.
2377 *
2378 * Return OK or FAIL.
2379 */
2380 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002381eval6(
2382 char_u **arg,
2383 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002384 int flags,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002385 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386{
Bram Moolenaar33570922005-01-25 22:26:29 +00002387 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002389 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002390#ifdef FEAT_FLOAT
2391 int use_float = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02002392 float_T f1 = 0, f2 = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002393#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002394 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395
2396 /*
2397 * Get the first variable.
2398 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002399 if (eval7(arg, rettv, flags, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 return FAIL;
2401
2402 /*
2403 * Repeat computing, until no '*', '/' or '%' is following.
2404 */
2405 for (;;)
2406 {
2407 op = **arg;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002408 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 break;
2410
Bram Moolenaar32e35112020-05-14 22:41:15 +02002411 if (flags & EVAL_EVALUATE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002413#ifdef FEAT_FLOAT
2414 if (rettv->v_type == VAR_FLOAT)
2415 {
2416 f1 = rettv->vval.v_float;
2417 use_float = TRUE;
2418 n1 = 0;
2419 }
2420 else
2421#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002422 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002423 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002424 if (error)
2425 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 }
2427 else
2428 n1 = 0;
2429
2430 /*
2431 * Get the second variable.
2432 */
2433 *arg = skipwhite(*arg + 1);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002434 if (eval7(arg, &var2, flags, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 return FAIL;
2436
Bram Moolenaar32e35112020-05-14 22:41:15 +02002437 if (flags & EVAL_EVALUATE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002439#ifdef FEAT_FLOAT
2440 if (var2.v_type == VAR_FLOAT)
2441 {
2442 if (!use_float)
2443 {
2444 f1 = n1;
2445 use_float = TRUE;
2446 }
2447 f2 = var2.vval.v_float;
2448 n2 = 0;
2449 }
2450 else
2451#endif
2452 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002453 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002454 clear_tv(&var2);
2455 if (error)
2456 return FAIL;
2457#ifdef FEAT_FLOAT
2458 if (use_float)
2459 f2 = n2;
2460#endif
2461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462
2463 /*
2464 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002465 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002467#ifdef FEAT_FLOAT
2468 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002470 if (op == '*')
2471 f1 = f1 * f2;
2472 else if (op == '/')
2473 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002474# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002475 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002476 if (f2 == 0.0)
2477 {
2478 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002479 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002480 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002481 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002482 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002483 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002484 }
2485 else
2486 f1 = f1 / f2;
2487# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002488 // We rely on the floating point library to handle divide
2489 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002490 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002491# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002494 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002495 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002496 return FAIL;
2497 }
2498 rettv->v_type = VAR_FLOAT;
2499 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 }
2501 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002502#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002504 if (op == '*')
2505 n1 = n1 * n2;
2506 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01002507 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01002509 n1 = num_modulus(n1, n2);
2510
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002511 rettv->v_type = VAR_NUMBER;
2512 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 }
2515 }
2516
2517 return OK;
2518}
2519
2520/*
2521 * Handle sixth level expression:
2522 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002523 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002524 * "string" string constant
2525 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 * &option-name option value
2527 * @r register contents
2528 * identifier variable value
2529 * function() function call
2530 * $VAR environment variable
2531 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002532 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002533 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002534 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002535 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 *
2537 * Also handle:
2538 * ! in front logical NOT
2539 * - in front unary minus
2540 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002541 * trailing [] subscript in String or List
2542 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02002543 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 *
2545 * "arg" must point to the first non-white of the expression.
2546 * "arg" is advanced to the next non-white after the recognized expression.
2547 *
2548 * Return OK or FAIL.
2549 */
2550 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002551eval7(
2552 char_u **arg,
2553 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002554 int flags,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002555 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002557 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 int len;
2559 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 char_u *start_leader, *end_leader;
2561 int ret = OK;
2562 char_u *alias;
2563
2564 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002565 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002566 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002568 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569
2570 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02002571 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 */
2573 start_leader = *arg;
2574 while (**arg == '!' || **arg == '-' || **arg == '+')
2575 *arg = skipwhite(*arg + 1);
2576 end_leader = *arg;
2577
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002578 if (**arg == '.' && (!isdigit(*(*arg + 1))
2579#ifdef FEAT_FLOAT
2580 || current_sctx.sc_version < 2
2581#endif
2582 ))
2583 {
2584 semsg(_(e_invexpr2), *arg);
2585 ++*arg;
2586 return FAIL;
2587 }
2588
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 switch (**arg)
2590 {
2591 /*
2592 * Number constant.
2593 */
2594 case '0':
2595 case '1':
2596 case '2':
2597 case '3':
2598 case '4':
2599 case '5':
2600 case '6':
2601 case '7':
2602 case '8':
2603 case '9':
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002604 case '.': ret = get_number_tv(arg, rettv, evaluate, want_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 break;
2606
2607 /*
2608 * String constant: "string".
2609 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002610 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 break;
2612
2613 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002614 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002616 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002617 break;
2618
2619 /*
2620 * List: [expr, expr]
2621 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002622 case '[': ret = get_list_tv(arg, rettv, flags, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 break;
2624
2625 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002626 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002627 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002628 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002629 {
2630 ++*arg;
Bram Moolenaar32e35112020-05-14 22:41:15 +02002631 ret = eval_dict(arg, rettv, flags, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002632 }
2633 else
2634 ret = NOTDONE;
2635 break;
2636
2637 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002638 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002639 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00002640 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002641 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
2642 if (ret == NOTDONE)
Bram Moolenaar32e35112020-05-14 22:41:15 +02002643 ret = eval_dict(arg, rettv, flags, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002644 break;
2645
2646 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00002647 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00002649 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 break;
2651
2652 /*
2653 * Environment variable: $VAR.
2654 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002655 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 break;
2657
2658 /*
2659 * Register contents: @r.
2660 */
2661 case '@': ++*arg;
2662 if (evaluate)
2663 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002664 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002665 rettv->vval.v_string = get_reg_contents(**arg,
2666 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 }
2668 if (**arg != NUL)
2669 ++*arg;
2670 break;
2671
2672 /*
2673 * nested expression: (expression).
2674 */
2675 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar32e35112020-05-14 22:41:15 +02002676 ret = eval1(arg, rettv, flags); // recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 if (**arg == ')')
2678 ++*arg;
2679 else if (ret == OK)
2680 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002681 emsg(_(e_missing_close));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002682 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 ret = FAIL;
2684 }
2685 break;
2686
Bram Moolenaar8c711452005-01-14 21:53:12 +00002687 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 break;
2689 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690
2691 if (ret == NOTDONE)
2692 {
2693 /*
2694 * Must be a variable or function name.
2695 * Can also be a curly-braces kind of name: {expr}.
2696 */
2697 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002698 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699 if (alias != NULL)
2700 s = alias;
2701
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002702 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 ret = FAIL;
2704 else
2705 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002706 if (**arg == '(') // recursive!
Bram Moolenaar32e35112020-05-14 22:41:15 +02002707 ret = eval_func(arg, s, len, rettv, flags, NULL);
Bram Moolenaar227a69d2020-05-15 18:17:28 +02002708 else if (flags & EVAL_CONSTANT)
2709 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002710 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002711 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002712 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002713 {
2714 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002715 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002716 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01002718 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002719 }
2720
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 *arg = skipwhite(*arg);
2722
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002723 // Handle following '[', '(' and '.' for expr[expr], expr.name,
2724 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002725 if (ret == OK)
Bram Moolenaar32e35112020-05-14 22:41:15 +02002726 ret = handle_subscript(arg, rettv, flags, TRUE,
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002727 start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728
2729 /*
2730 * Apply logical NOT and unary '-', from right to left, ignore '+'.
2731 */
2732 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002733 ret = eval7_leader(rettv, start_leader, &end_leader);
2734 return ret;
2735}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002736
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002737/*
2738 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
2739 * Adjusts "end_leaderp" until it is at "start_leader".
2740 */
2741 static int
2742eval7_leader(typval_T *rettv, char_u *start_leader, char_u **end_leaderp)
2743{
2744 char_u *end_leader = *end_leaderp;
2745 int ret = OK;
2746 int error = FALSE;
2747 varnumber_T val = 0;
2748#ifdef FEAT_FLOAT
2749 float_T f = 0.0;
2750
2751 if (rettv->v_type == VAR_FLOAT)
2752 f = rettv->vval.v_float;
2753 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002754#endif
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002755 val = tv_get_number_chk(rettv, &error);
2756 if (error)
2757 {
2758 clear_tv(rettv);
2759 ret = FAIL;
2760 }
2761 else
2762 {
2763 while (end_leader > start_leader)
2764 {
2765 --end_leader;
2766 if (*end_leader == '!')
2767 {
2768#ifdef FEAT_FLOAT
2769 if (rettv->v_type == VAR_FLOAT)
2770 f = !f;
2771 else
2772#endif
2773 val = !val;
2774 }
2775 else if (*end_leader == '-')
2776 {
2777#ifdef FEAT_FLOAT
2778 if (rettv->v_type == VAR_FLOAT)
2779 f = -f;
2780 else
2781#endif
2782 val = -val;
2783 }
2784 }
2785#ifdef FEAT_FLOAT
2786 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002788 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002789 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002791 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002792#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002793 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002794 clear_tv(rettv);
2795 rettv->v_type = VAR_NUMBER;
2796 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002799 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 return ret;
2801}
2802
2803/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002804 * Call the function referred to in "rettv".
2805 */
2806 static int
2807call_func_rettv(
2808 char_u **arg,
2809 typval_T *rettv,
2810 int evaluate,
2811 dict_T *selfdict,
2812 typval_T *basetv)
2813{
2814 partial_T *pt = NULL;
2815 funcexe_T funcexe;
2816 typval_T functv;
2817 char_u *s;
2818 int ret;
2819
2820 // need to copy the funcref so that we can clear rettv
2821 if (evaluate)
2822 {
2823 functv = *rettv;
2824 rettv->v_type = VAR_UNKNOWN;
2825
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002826 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002827 if (functv.v_type == VAR_PARTIAL)
2828 {
2829 pt = functv.vval.v_partial;
2830 s = partial_name(pt);
2831 }
2832 else
2833 s = functv.vval.v_string;
2834 }
2835 else
2836 s = (char_u *)"";
2837
Bram Moolenaara80faa82020-04-12 19:37:17 +02002838 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002839 funcexe.firstline = curwin->w_cursor.lnum;
2840 funcexe.lastline = curwin->w_cursor.lnum;
2841 funcexe.evaluate = evaluate;
2842 funcexe.partial = pt;
2843 funcexe.selfdict = selfdict;
2844 funcexe.basetv = basetv;
2845 ret = get_func_tv(s, -1, rettv, arg, &funcexe);
2846
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002847 // Clear the funcref afterwards, so that deleting it while
2848 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002849 if (evaluate)
2850 clear_tv(&functv);
2851
2852 return ret;
2853}
2854
2855/*
2856 * Evaluate "->method()".
2857 * "*arg" points to the '-'.
2858 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
2859 */
2860 static int
2861eval_lambda(
2862 char_u **arg,
2863 typval_T *rettv,
2864 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002865 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002866{
2867 typval_T base = *rettv;
2868 int ret;
2869
2870 // Skip over the ->.
2871 *arg += 2;
2872 rettv->v_type = VAR_UNKNOWN;
2873
2874 ret = get_lambda_tv(arg, rettv, evaluate);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01002875 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002876 return FAIL;
2877 else if (**arg != '(')
2878 {
2879 if (verbose)
2880 {
2881 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002882 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002883 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002884 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002885 }
2886 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02002887 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002888 }
Bram Moolenaar86173482019-10-01 17:02:16 +02002889 else
2890 ret = call_func_rettv(arg, rettv, evaluate, NULL, &base);
2891
2892 // Clear the funcref afterwards, so that deleting it while
2893 // evaluating the arguments is possible (see test55).
2894 if (evaluate)
2895 clear_tv(&base);
2896
2897 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002898}
2899
2900/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02002901 * Evaluate "->method()".
2902 * "*arg" points to the '-'.
2903 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
2904 */
2905 static int
2906eval_method(
2907 char_u **arg,
2908 typval_T *rettv,
2909 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002910 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02002911{
2912 char_u *name;
2913 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002914 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002915 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002916 int ret;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002917
2918 // Skip over the ->.
2919 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002920 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002921
Bram Moolenaarac92e252019-08-03 21:58:38 +02002922 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002923 len = get_name_len(arg, &alias, evaluate, TRUE);
2924 if (alias != NULL)
2925 name = alias;
2926
2927 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02002928 {
2929 if (verbose)
2930 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002931 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002932 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002933 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02002934 {
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002935 if (**arg != '(')
2936 {
2937 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002938 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002939 ret = FAIL;
2940 }
Bram Moolenaar51841322019-08-08 21:10:01 +02002941 else if (VIM_ISWHITE((*arg)[-1]))
2942 {
2943 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002944 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02002945 ret = FAIL;
2946 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002947 else
Bram Moolenaar32e35112020-05-14 22:41:15 +02002948 ret = eval_func(arg, name, len, rettv,
2949 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02002950 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002951
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002952 // Clear the funcref afterwards, so that deleting it while
2953 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02002954 if (evaluate)
2955 clear_tv(&base);
2956
Bram Moolenaarac92e252019-08-03 21:58:38 +02002957 return ret;
2958}
2959
2960/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002961 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
2962 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002963 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
2964 */
2965 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002966eval_index(
2967 char_u **arg,
2968 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002969 int flags,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002970 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002971{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002972 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002973 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002974 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002975 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002976 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002977 long len = -1;
2978 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002979 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002980 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002981
Bram Moolenaara03f2332016-02-06 18:09:59 +01002982 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002983 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01002984 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002985 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002986 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002987 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01002988 return FAIL;
2989 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02002990#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01002991 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002992 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01002993 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02002994#endif
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002995 case VAR_BOOL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002996 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002997 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002998 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002999 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003000 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003001 return FAIL;
3002 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003003 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003004 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003005 if (evaluate)
3006 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003007 // FALLTHROUGH
Bram Moolenaara03f2332016-02-06 18:09:59 +01003008
3009 case VAR_STRING:
3010 case VAR_NUMBER:
3011 case VAR_LIST:
3012 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003013 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003014 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003015 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003016
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003017 init_tv(&var1);
3018 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003019 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003020 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003021 /*
3022 * dict.name
3023 */
3024 key = *arg + 1;
3025 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
3026 ;
3027 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003028 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003029 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003030 }
3031 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003032 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003033 /*
3034 * something[idx]
3035 *
3036 * Get the (first) variable from inside the [].
3037 */
3038 *arg = skipwhite(*arg + 1);
3039 if (**arg == ':')
3040 empty1 = TRUE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02003041 else if (eval1(arg, &var1, flags) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003042 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003043 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003044 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003045 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003046 clear_tv(&var1);
3047 return FAIL;
3048 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003049
3050 /*
3051 * Get the second variable from inside the [:].
3052 */
3053 if (**arg == ':')
3054 {
3055 range = TRUE;
3056 *arg = skipwhite(*arg + 1);
3057 if (**arg == ']')
3058 empty2 = TRUE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02003059 else if (eval1(arg, &var2, flags) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003060 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003061 if (!empty1)
3062 clear_tv(&var1);
3063 return FAIL;
3064 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003065 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003066 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003067 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003068 if (!empty1)
3069 clear_tv(&var1);
3070 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003071 return FAIL;
3072 }
3073 }
3074
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003075 // Check for the ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00003076 if (**arg != ']')
3077 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003078 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003079 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003080 clear_tv(&var1);
3081 if (range)
3082 clear_tv(&var2);
3083 return FAIL;
3084 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003085 *arg = skipwhite(*arg + 1); // skip the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003086 }
3087
3088 if (evaluate)
3089 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003090 n1 = 0;
3091 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003092 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003093 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003094 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003095 }
3096 if (range)
3097 {
3098 if (empty2)
3099 n2 = -1;
3100 else
3101 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003102 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003103 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003104 }
3105 }
3106
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003107 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003108 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003109 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003110 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003111 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003112 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003113 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003114 case VAR_FLOAT:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003115 case VAR_BOOL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003116 case VAR_SPECIAL:
3117 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003118 case VAR_CHANNEL:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003119 break; // not evaluating, skipping over subscript
Bram Moolenaara03f2332016-02-06 18:09:59 +01003120
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003121 case VAR_NUMBER:
3122 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003123 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003124 len = (long)STRLEN(s);
3125 if (range)
3126 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003127 // The resulting variable is a substring. If the indexes
3128 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003129 if (n1 < 0)
3130 {
3131 n1 = len + n1;
3132 if (n1 < 0)
3133 n1 = 0;
3134 }
3135 if (n2 < 0)
3136 n2 = len + n2;
3137 else if (n2 >= len)
3138 n2 = len;
3139 if (n1 >= len || n2 < 0 || n1 > n2)
3140 s = NULL;
3141 else
3142 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
3143 }
3144 else
3145 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003146 // The resulting variable is a string of a single
3147 // character. If the index is too big or negative the
3148 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003149 if (n1 >= len || n1 < 0)
3150 s = NULL;
3151 else
3152 s = vim_strnsave(s + n1, 1);
3153 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003154 clear_tv(rettv);
3155 rettv->v_type = VAR_STRING;
3156 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003157 break;
3158
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003159 case VAR_BLOB:
3160 len = blob_len(rettv->vval.v_blob);
3161 if (range)
3162 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003163 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003164 // are out of range the result is empty.
3165 if (n1 < 0)
3166 {
3167 n1 = len + n1;
3168 if (n1 < 0)
3169 n1 = 0;
3170 }
3171 if (n2 < 0)
3172 n2 = len + n2;
3173 else if (n2 >= len)
3174 n2 = len - 1;
3175 if (n1 >= len || n2 < 0 || n1 > n2)
3176 {
3177 clear_tv(rettv);
3178 rettv->v_type = VAR_BLOB;
3179 rettv->vval.v_blob = NULL;
3180 }
3181 else
3182 {
3183 blob_T *blob = blob_alloc();
3184
3185 if (blob != NULL)
3186 {
3187 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
3188 {
3189 blob_free(blob);
3190 return FAIL;
3191 }
3192 blob->bv_ga.ga_len = n2 - n1 + 1;
3193 for (i = n1; i <= n2; i++)
3194 blob_set(blob, i - n1,
3195 blob_get(rettv->vval.v_blob, i));
3196
3197 clear_tv(rettv);
3198 rettv_blob_set(rettv, blob);
3199 }
3200 }
3201 }
3202 else
3203 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003204 // The resulting variable is a byte value.
3205 // If the index is too big or negative that is an error.
3206 if (n1 < 0)
3207 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003208 if (n1 < len && n1 >= 0)
3209 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003210 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003211
3212 clear_tv(rettv);
3213 rettv->v_type = VAR_NUMBER;
3214 rettv->vval.v_number = v;
3215 }
3216 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003217 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003218 }
3219 break;
3220
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003221 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003222 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003223 if (n1 < 0)
3224 n1 = len + n1;
3225 if (!empty1 && (n1 < 0 || n1 >= len))
3226 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003227 // For a range we allow invalid values and return an empty
3228 // list. A list index out of range is an error.
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003229 if (!range)
3230 {
3231 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003232 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003233 return FAIL;
3234 }
3235 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003236 }
3237 if (range)
3238 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003239 list_T *l;
3240 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003241
3242 if (n2 < 0)
3243 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003244 else if (n2 >= len)
3245 n2 = len - 1;
3246 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003247 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003248 l = list_alloc();
3249 if (l == NULL)
3250 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003251 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003252 n1 <= n2; ++n1)
3253 {
3254 if (list_append_tv(l, &item->li_tv) == FAIL)
3255 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003256 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003257 return FAIL;
3258 }
3259 item = item->li_next;
3260 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003261 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003262 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003263 }
3264 else
3265 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003266 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003267 clear_tv(rettv);
3268 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003269 }
3270 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003271
3272 case VAR_DICT:
3273 if (range)
3274 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003275 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003276 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003277 if (len == -1)
3278 clear_tv(&var1);
3279 return FAIL;
3280 }
3281 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003282 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003283
3284 if (len == -1)
3285 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003286 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02003287 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003288 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003289 clear_tv(&var1);
3290 return FAIL;
3291 }
3292 }
3293
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003294 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003295
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003296 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003297 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003298 if (len == -1)
3299 clear_tv(&var1);
3300 if (item == NULL)
3301 return FAIL;
3302
3303 copy_tv(&item->di_tv, &var1);
3304 clear_tv(rettv);
3305 *rettv = var1;
3306 }
3307 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003308 }
3309 }
3310
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003311 return OK;
3312}
3313
3314/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02003315 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003316 */
3317 char_u *
3318partial_name(partial_T *pt)
3319{
3320 if (pt->pt_name != NULL)
3321 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02003322 if (pt->pt_func != NULL)
3323 return pt->pt_func->uf_name;
3324 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003325}
3326
Bram Moolenaarddecc252016-04-06 22:59:37 +02003327 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003328partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003329{
3330 int i;
3331
3332 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003333 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003334 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003335 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003336 if (pt->pt_name != NULL)
3337 {
3338 func_unref(pt->pt_name);
3339 vim_free(pt->pt_name);
3340 }
3341 else
3342 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003343
3344 if (pt->pt_funcstack != NULL)
3345 {
3346 // Decrease the reference count for the context of a closure. If down
3347 // to zero free it and clear the variables on the stack.
3348 if (--pt->pt_funcstack->fs_refcount == 0)
3349 {
3350 garray_T *gap = &pt->pt_funcstack->fs_ga;
3351 typval_T *stack = gap->ga_data;
3352
3353 for (i = 0; i < gap->ga_len; ++i)
3354 clear_tv(stack + i);
3355 ga_clear(gap);
3356 vim_free(pt->pt_funcstack);
3357 }
3358 pt->pt_funcstack = NULL;
3359 }
3360
Bram Moolenaarddecc252016-04-06 22:59:37 +02003361 vim_free(pt);
3362}
3363
3364/*
3365 * Unreference a closure: decrement the reference count and free it when it
3366 * becomes zero.
3367 */
3368 void
3369partial_unref(partial_T *pt)
3370{
3371 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003372 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003373}
3374
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003375/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003376 * Return the next (unique) copy ID.
3377 * Used for serializing nested structures.
3378 */
3379 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003380get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003381{
3382 current_copyID += COPYID_INC;
3383 return current_copyID;
3384}
3385
3386/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003387 * Garbage collection for lists and dictionaries.
3388 *
3389 * We use reference counts to be able to free most items right away when they
3390 * are no longer used. But for composite items it's possible that it becomes
3391 * unused while the reference count is > 0: When there is a recursive
3392 * reference. Example:
3393 * :let l = [1, 2, 3]
3394 * :let d = {9: l}
3395 * :let l[1] = d
3396 *
3397 * Since this is quite unusual we handle this with garbage collection: every
3398 * once in a while find out which lists and dicts are not referenced from any
3399 * variable.
3400 *
3401 * Here is a good reference text about garbage collection (refers to Python
3402 * but it applies to all reference-counting mechanisms):
3403 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00003404 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00003405
3406/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003407 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02003408 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003409 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00003410 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003411 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003412garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003413{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003414 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003415 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003416 buf_T *buf;
3417 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01003418 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003419 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003420
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003421 if (!testing)
3422 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003423 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003424 want_garbage_collect = FALSE;
3425 may_garbage_collect = FALSE;
3426 garbage_collect_at_exit = FALSE;
3427 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00003428
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01003429 // The execution stack can grow big, limit the size.
3430 if (exestack.ga_maxlen - exestack.ga_len > 500)
3431 {
3432 size_t new_len;
3433 char_u *pp;
3434 int n;
3435
3436 // Keep 150% of the current size, with a minimum of the growth size.
3437 n = exestack.ga_len / 2;
3438 if (n < exestack.ga_growsize)
3439 n = exestack.ga_growsize;
3440
3441 // Don't make it bigger though.
3442 if (exestack.ga_len + n < exestack.ga_maxlen)
3443 {
3444 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
3445 pp = vim_realloc(exestack.ga_data, new_len);
3446 if (pp == NULL)
3447 return FAIL;
3448 exestack.ga_maxlen = exestack.ga_len + n;
3449 exestack.ga_data = pp;
3450 }
3451 }
3452
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003453 // We advance by two because we add one for items referenced through
3454 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003455 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003456
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003457 /*
3458 * 1. Go through all accessible variables and mark all lists and dicts
3459 * with copyID.
3460 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003461
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003462 // Don't free variables in the previous_funccal list unless they are only
3463 // referenced through previous_funccal. This must be first, because if
3464 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003465 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003466
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003467 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003468 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003469
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003470 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003471 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003472 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
3473 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003474
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003475 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003476 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003477 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3478 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02003479 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003480 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
3481 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003482#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003483 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003484 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3485 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003486 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003487 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003488 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3489 NULL, NULL);
3490#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003491
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003492 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003493 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003494 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
3495 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003496 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003497 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003498
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003499 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003500 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003501
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003502 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003503 abort = abort || set_ref_in_functions(copyID);
3504
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003505 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003506 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003507
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003508 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003509 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00003510
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003511 // callbacks in buffers
3512 abort = abort || set_ref_in_buffers(copyID);
3513
Bram Moolenaar1dced572012-04-05 16:54:08 +02003514#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003515 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02003516#endif
3517
Bram Moolenaardb913952012-06-29 12:54:53 +02003518#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003519 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003520#endif
3521
3522#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003523 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003524#endif
3525
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003526#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02003527 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003528 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003529#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02003530#ifdef FEAT_NETBEANS_INTG
3531 abort = abort || set_ref_in_nb_channel(copyID);
3532#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003533
Bram Moolenaare3188e22016-05-31 21:13:04 +02003534#ifdef FEAT_TIMERS
3535 abort = abort || set_ref_in_timer(copyID);
3536#endif
3537
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02003538#ifdef FEAT_QUICKFIX
3539 abort = abort || set_ref_in_quickfix(copyID);
3540#endif
3541
Bram Moolenaara2c45a12017-07-27 22:14:59 +02003542#ifdef FEAT_TERMINAL
3543 abort = abort || set_ref_in_term(copyID);
3544#endif
3545
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003546#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003547 abort = abort || set_ref_in_popups(copyID);
3548#endif
3549
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003550 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003551 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003552 /*
3553 * 2. Free lists and dictionaries that are not referenced.
3554 */
3555 did_free = free_unref_items(copyID);
3556
3557 /*
3558 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003559 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003560 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003561 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003562 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003563 else if (p_verbose > 0)
3564 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003565 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003566 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003567
3568 return did_free;
3569}
3570
3571/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003572 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003573 */
3574 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003575free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003576{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003577 int did_free = FALSE;
3578
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003579 // Let all "free" functions know that we are here. This means no
3580 // dictionaries, lists, channels or jobs are to be freed, because we will
3581 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003582 in_free_unref_items = TRUE;
3583
3584 /*
3585 * PASS 1: free the contents of the items. We don't free the items
3586 * themselves yet, so that it is possible to decrement refcount counters
3587 */
3588
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003589 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02003590 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003591
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003592 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02003593 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003594
3595#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003596 // Go through the list of jobs and free items without the copyID. This
3597 // must happen before doing channels, because jobs refer to channels, but
3598 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003599 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
3600
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003601 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003602 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
3603#endif
3604
3605 /*
3606 * PASS 2: free the items themselves.
3607 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003608 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02003609 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01003610
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003611#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003612 // Go through the list of jobs and free items without the copyID. This
3613 // must happen before doing channels, because jobs refer to channels, but
3614 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003615 free_unused_jobs(copyID, COPYID_MASK);
3616
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003617 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003618 free_unused_channels(copyID, COPYID_MASK);
3619#endif
3620
3621 in_free_unref_items = FALSE;
3622
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003623 return did_free;
3624}
3625
3626/*
3627 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003628 * "list_stack" is used to add lists to be marked. Can be NULL.
3629 *
3630 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003631 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003632 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003633set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003634{
3635 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003636 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003637 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003638 hashtab_T *cur_ht;
3639 ht_stack_T *ht_stack = NULL;
3640 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003641
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003642 cur_ht = ht;
3643 for (;;)
3644 {
3645 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003646 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003647 // Mark each item in the hashtab. If the item contains a hashtab
3648 // it is added to ht_stack, if it contains a list it is added to
3649 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003650 todo = (int)cur_ht->ht_used;
3651 for (hi = cur_ht->ht_array; todo > 0; ++hi)
3652 if (!HASHITEM_EMPTY(hi))
3653 {
3654 --todo;
3655 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
3656 &ht_stack, list_stack);
3657 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003658 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003659
3660 if (ht_stack == NULL)
3661 break;
3662
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003663 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003664 cur_ht = ht_stack->ht;
3665 tempitem = ht_stack;
3666 ht_stack = ht_stack->prev;
3667 free(tempitem);
3668 }
3669
3670 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003671}
3672
3673/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003674 * Mark a dict and its items with "copyID".
3675 * Returns TRUE if setting references failed somehow.
3676 */
3677 int
3678set_ref_in_dict(dict_T *d, int copyID)
3679{
3680 if (d != NULL && d->dv_copyID != copyID)
3681 {
3682 d->dv_copyID = copyID;
3683 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
3684 }
3685 return FALSE;
3686}
3687
3688/*
3689 * Mark a list and its items with "copyID".
3690 * Returns TRUE if setting references failed somehow.
3691 */
3692 int
3693set_ref_in_list(list_T *ll, int copyID)
3694{
3695 if (ll != NULL && ll->lv_copyID != copyID)
3696 {
3697 ll->lv_copyID = copyID;
3698 return set_ref_in_list_items(ll, copyID, NULL);
3699 }
3700 return FALSE;
3701}
3702
3703/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003704 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003705 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
3706 *
3707 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003708 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003709 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003710set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003711{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003712 listitem_T *li;
3713 int abort = FALSE;
3714 list_T *cur_l;
3715 list_stack_T *list_stack = NULL;
3716 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003717
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003718 cur_l = l;
3719 for (;;)
3720 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003721 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003722 // Mark each item in the list. If the item contains a hashtab
3723 // it is added to ht_stack, if it contains a list it is added to
3724 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003725 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
3726 abort = abort || set_ref_in_item(&li->li_tv, copyID,
3727 ht_stack, &list_stack);
3728 if (list_stack == NULL)
3729 break;
3730
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003731 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003732 cur_l = list_stack->list;
3733 tempitem = list_stack;
3734 list_stack = list_stack->prev;
3735 free(tempitem);
3736 }
3737
3738 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003739}
3740
3741/*
3742 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003743 * "list_stack" is used to add lists to be marked. Can be NULL.
3744 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
3745 *
3746 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003747 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003748 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003749set_ref_in_item(
3750 typval_T *tv,
3751 int copyID,
3752 ht_stack_T **ht_stack,
3753 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003754{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003755 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00003756
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003757 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003758 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003759 dict_T *dd = tv->vval.v_dict;
3760
Bram Moolenaara03f2332016-02-06 18:09:59 +01003761 if (dd != NULL && dd->dv_copyID != copyID)
3762 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003763 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01003764 dd->dv_copyID = copyID;
3765 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003766 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003767 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
3768 }
3769 else
3770 {
3771 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
3772 if (newitem == NULL)
3773 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003774 else
3775 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003776 newitem->ht = &dd->dv_hashtab;
3777 newitem->prev = *ht_stack;
3778 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003779 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00003780 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01003781 }
3782 }
3783 else if (tv->v_type == VAR_LIST)
3784 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003785 list_T *ll = tv->vval.v_list;
3786
Bram Moolenaara03f2332016-02-06 18:09:59 +01003787 if (ll != NULL && ll->lv_copyID != copyID)
3788 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003789 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01003790 ll->lv_copyID = copyID;
3791 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003792 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003793 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01003794 }
3795 else
3796 {
3797 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003798 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003799 if (newitem == NULL)
3800 abort = TRUE;
3801 else
3802 {
3803 newitem->list = ll;
3804 newitem->prev = *list_stack;
3805 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003806 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00003807 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01003808 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00003809 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003810 else if (tv->v_type == VAR_FUNC)
3811 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003812 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003813 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003814 else if (tv->v_type == VAR_PARTIAL)
3815 {
3816 partial_T *pt = tv->vval.v_partial;
3817 int i;
3818
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003819 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003820 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003821 // Didn't see this partial yet.
3822 pt->pt_copyID = copyID;
3823
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003824 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003825
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003826 if (pt->pt_dict != NULL)
3827 {
3828 typval_T dtv;
3829
3830 dtv.v_type = VAR_DICT;
3831 dtv.vval.v_dict = pt->pt_dict;
3832 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
3833 }
3834
3835 for (i = 0; i < pt->pt_argc; ++i)
3836 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
3837 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003838 if (pt->pt_funcstack != NULL)
3839 {
3840 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
3841
3842 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
3843 abort = abort || set_ref_in_item(stack + i, copyID,
3844 ht_stack, list_stack);
3845 }
3846
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003847 }
3848 }
3849#ifdef FEAT_JOB_CHANNEL
3850 else if (tv->v_type == VAR_JOB)
3851 {
3852 job_T *job = tv->vval.v_job;
3853 typval_T dtv;
3854
3855 if (job != NULL && job->jv_copyID != copyID)
3856 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02003857 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003858 if (job->jv_channel != NULL)
3859 {
3860 dtv.v_type = VAR_CHANNEL;
3861 dtv.vval.v_channel = job->jv_channel;
3862 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
3863 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003864 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003865 {
3866 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003867 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003868 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
3869 }
3870 }
3871 }
3872 else if (tv->v_type == VAR_CHANNEL)
3873 {
3874 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003875 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003876 typval_T dtv;
3877 jsonq_T *jq;
3878 cbq_T *cq;
3879
3880 if (ch != NULL && ch->ch_copyID != copyID)
3881 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02003882 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02003883 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003884 {
3885 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
3886 jq = jq->jq_next)
3887 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
3888 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
3889 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003890 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003891 {
3892 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003893 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003894 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
3895 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003896 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003897 {
3898 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003899 dtv.vval.v_partial =
3900 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003901 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
3902 }
3903 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003904 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003905 {
3906 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003907 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003908 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
3909 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003910 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003911 {
3912 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003913 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003914 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
3915 }
3916 }
3917 }
3918#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003919 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00003920}
3921
Bram Moolenaar8c711452005-01-14 21:53:12 +00003922/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003923 * Return a string with the string representation of a variable.
3924 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003925 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003926 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02003927 * When both "echo_style" and "composite_val" are FALSE, put quotes around
3928 * stings as "string()", otherwise does not put quotes around strings, as
3929 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02003930 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
3931 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00003932 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003933 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003934 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02003935echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003936 typval_T *tv,
3937 char_u **tofree,
3938 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02003939 int copyID,
3940 int echo_style,
3941 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02003942 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003943{
Bram Moolenaare9a41262005-01-15 22:18:47 +00003944 static int recurse = 0;
3945 char_u *r = NULL;
3946
Bram Moolenaar33570922005-01-25 22:26:29 +00003947 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00003948 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02003949 if (!did_echo_string_emsg)
3950 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003951 // Only give this message once for a recursive call to avoid
3952 // flooding the user with errors. And stop iterating over lists
3953 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02003954 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003955 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02003956 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003957 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02003958 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00003959 }
3960 ++recurse;
3961
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003962 switch (tv->v_type)
3963 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02003964 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02003965 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02003966 {
3967 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02003968 r = tv->vval.v_string;
3969 if (r == NULL)
3970 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02003971 }
3972 else
3973 {
3974 *tofree = string_quote(tv->vval.v_string, FALSE);
3975 r = *tofree;
3976 }
3977 break;
3978
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003979 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02003980 if (echo_style)
3981 {
3982 *tofree = NULL;
3983 r = tv->vval.v_string;
3984 }
3985 else
3986 {
3987 *tofree = string_quote(tv->vval.v_string, TRUE);
3988 r = *tofree;
3989 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003990 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003991
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003992 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01003993 {
3994 partial_T *pt = tv->vval.v_partial;
3995 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003996 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01003997 garray_T ga;
3998 int i;
3999 char_u *tf;
4000
4001 ga_init2(&ga, 1, 100);
4002 ga_concat(&ga, (char_u *)"function(");
4003 if (fname != NULL)
4004 {
4005 ga_concat(&ga, fname);
4006 vim_free(fname);
4007 }
4008 if (pt != NULL && pt->pt_argc > 0)
4009 {
4010 ga_concat(&ga, (char_u *)", [");
4011 for (i = 0; i < pt->pt_argc; ++i)
4012 {
4013 if (i > 0)
4014 ga_concat(&ga, (char_u *)", ");
4015 ga_concat(&ga,
4016 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4017 vim_free(tf);
4018 }
4019 ga_concat(&ga, (char_u *)"]");
4020 }
4021 if (pt != NULL && pt->pt_dict != NULL)
4022 {
4023 typval_T dtv;
4024
4025 ga_concat(&ga, (char_u *)", ");
4026 dtv.v_type = VAR_DICT;
4027 dtv.vval.v_dict = pt->pt_dict;
4028 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4029 vim_free(tf);
4030 }
4031 ga_concat(&ga, (char_u *)")");
4032
4033 *tofree = ga.ga_data;
4034 r = *tofree;
4035 break;
4036 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004037
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004038 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004039 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004040 break;
4041
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004042 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004043 if (tv->vval.v_list == NULL)
4044 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004045 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004046 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004047 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004048 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004049 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4050 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004051 {
4052 *tofree = NULL;
4053 r = (char_u *)"[...]";
4054 }
4055 else
4056 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004057 int old_copyID = tv->vval.v_list->lv_copyID;
4058
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004059 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004060 *tofree = list2string(tv, copyID, restore_copyID);
4061 if (restore_copyID)
4062 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004063 r = *tofree;
4064 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004065 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004066
Bram Moolenaar8c711452005-01-14 21:53:12 +00004067 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004068 if (tv->vval.v_dict == NULL)
4069 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004070 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004071 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004072 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004073 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004074 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4075 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004076 {
4077 *tofree = NULL;
4078 r = (char_u *)"{...}";
4079 }
4080 else
4081 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004082 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004083
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004084 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004085 *tofree = dict2string(tv, copyID, restore_copyID);
4086 if (restore_copyID)
4087 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004088 r = *tofree;
4089 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004090 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004091
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004092 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004093 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004094 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004095 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004096 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004097 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004098 break;
4099
Bram Moolenaar835dc632016-02-07 14:27:38 +01004100 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004101 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004102 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004103 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004104 if (composite_val)
4105 {
4106 *tofree = string_quote(r, FALSE);
4107 r = *tofree;
4108 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004109 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004110
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004111 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004112#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004113 *tofree = NULL;
4114 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4115 r = numbuf;
4116 break;
4117#endif
4118
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004119 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004120 case VAR_SPECIAL:
4121 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004122 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004123 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004124 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004125
Bram Moolenaar8502c702014-06-17 12:51:16 +02004126 if (--recurse == 0)
4127 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004128 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004129}
4130
4131/*
4132 * Return a string with the string representation of a variable.
4133 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4134 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004135 * Does not put quotes around strings, as ":echo" displays values.
4136 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4137 * May return NULL.
4138 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004139 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004140echo_string(
4141 typval_T *tv,
4142 char_u **tofree,
4143 char_u *numbuf,
4144 int copyID)
4145{
4146 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4147}
4148
4149/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004150 * Return string "str" in ' quotes, doubling ' characters.
4151 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004152 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004153 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004154 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004155string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004156{
Bram Moolenaar33570922005-01-25 22:26:29 +00004157 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004158 char_u *p, *r, *s;
4159
Bram Moolenaar33570922005-01-25 22:26:29 +00004160 len = (function ? 13 : 3);
4161 if (str != NULL)
4162 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004163 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004164 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004165 if (*p == '\'')
4166 ++len;
4167 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004168 s = r = alloc(len);
4169 if (r != NULL)
4170 {
4171 if (function)
4172 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004173 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004174 r += 10;
4175 }
4176 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004177 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004178 if (str != NULL)
4179 for (p = str; *p != NUL; )
4180 {
4181 if (*p == '\'')
4182 *r++ = '\'';
4183 MB_COPY_CHAR(p, r);
4184 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004185 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004186 if (function)
4187 *r++ = ')';
4188 *r++ = NUL;
4189 }
4190 return s;
4191}
4192
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004193#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004194/*
4195 * Convert the string "text" to a floating point number.
4196 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4197 * this always uses a decimal point.
4198 * Returns the length of the text that was consumed.
4199 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004200 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004201string2float(
4202 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004203 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004204{
4205 char *s = (char *)text;
4206 float_T f;
4207
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004208 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004209 if (STRNICMP(text, "inf", 3) == 0)
4210 {
4211 *value = INFINITY;
4212 return 3;
4213 }
4214 if (STRNICMP(text, "-inf", 3) == 0)
4215 {
4216 *value = -INFINITY;
4217 return 4;
4218 }
4219 if (STRNICMP(text, "nan", 3) == 0)
4220 {
4221 *value = NAN;
4222 return 3;
4223 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004224 f = strtod(s, &s);
4225 *value = f;
4226 return (int)((char_u *)s - text);
4227}
4228#endif
4229
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004230/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004232 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004234 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004235var2fpos(
4236 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004237 int dollar_lnum, // TRUE when $ is last line
4238 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004240 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004242 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004244 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004245 if (varp->v_type == VAR_LIST)
4246 {
4247 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004248 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004249 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004250 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004251
4252 l = varp->vval.v_list;
4253 if (l == NULL)
4254 return NULL;
4255
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004256 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004257 pos.lnum = list_find_nr(l, 0L, &error);
4258 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004259 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004260
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004261 // Get the column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004262 pos.col = list_find_nr(l, 1L, &error);
4263 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004264 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004265 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004266
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004267 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004268 li = list_find(l, 1L);
4269 if (li != NULL && li->li_tv.v_type == VAR_STRING
4270 && li->li_tv.vval.v_string != NULL
4271 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
4272 pos.col = len + 1;
4273
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004274 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004275 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004276 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004277 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004278
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004279 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004280 pos.coladd = list_find_nr(l, 2L, &error);
4281 if (error)
4282 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004283
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004284 return &pos;
4285 }
4286
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004287 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004288 if (name == NULL)
4289 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004290 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004292 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004293 {
4294 if (VIsual_active)
4295 return &VIsual;
4296 return &curwin->w_cursor;
4297 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004298 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004300 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4302 return NULL;
4303 return pp;
4304 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004305
Bram Moolenaara5525202006-03-02 22:52:09 +00004306 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004307
Bram Moolenaar477933c2007-07-17 14:32:23 +00004308 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004309 {
4310 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004311 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004312 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004313 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004314 // In silent Ex mode topline is zero, but that's not a valid line
4315 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004316 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004317 return &pos;
4318 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004319 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004320 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004321 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004322 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004323 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004324 return &pos;
4325 }
4326 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004327 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004329 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 {
4331 pos.lnum = curbuf->b_ml.ml_line_count;
4332 pos.col = 0;
4333 }
4334 else
4335 {
4336 pos.lnum = curwin->w_cursor.lnum;
4337 pos.col = (colnr_T)STRLEN(ml_get_curline());
4338 }
4339 return &pos;
4340 }
4341 return NULL;
4342}
4343
4344/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004345 * Convert list in "arg" into a position and optional file number.
4346 * When "fnump" is NULL there is no file number, only 3 items.
4347 * Note that the column is passed on as-is, the caller may want to decrement
4348 * it to use 1 for the first column.
4349 * Return FAIL when conversion is not possible, doesn't check the position for
4350 * validity.
4351 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004352 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004353list2fpos(
4354 typval_T *arg,
4355 pos_T *posp,
4356 int *fnump,
4357 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004358{
4359 list_T *l = arg->vval.v_list;
4360 long i = 0;
4361 long n;
4362
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004363 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
4364 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00004365 if (arg->v_type != VAR_LIST
4366 || l == NULL
4367 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02004368 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004369 return FAIL;
4370
4371 if (fnump != NULL)
4372 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004373 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004374 if (n < 0)
4375 return FAIL;
4376 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004377 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004378 *fnump = n;
4379 }
4380
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004381 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004382 if (n < 0)
4383 return FAIL;
4384 posp->lnum = n;
4385
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004386 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004387 if (n < 0)
4388 return FAIL;
4389 posp->col = n;
4390
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004391 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004392 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00004393 posp->coladd = 0;
4394 else
4395 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004396
Bram Moolenaar493c1782014-05-28 14:34:46 +02004397 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004398 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02004399
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004400 return OK;
4401}
4402
4403/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 * Get the length of an environment variable name.
4405 * Advance "arg" to the first character after the name.
4406 * Return 0 for error.
4407 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004408 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004409get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410{
4411 char_u *p;
4412 int len;
4413
4414 for (p = *arg; vim_isIDc(*p); ++p)
4415 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004416 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 return 0;
4418
4419 len = (int)(p - *arg);
4420 *arg = p;
4421 return len;
4422}
4423
4424/*
4425 * Get the length of the name of a function or internal variable.
4426 * "arg" is advanced to the first non-white character after the name.
4427 * Return 0 if something is wrong.
4428 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004429 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004430get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431{
4432 char_u *p;
4433 int len;
4434
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004435 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004437 {
4438 if (*p == ':')
4439 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004440 // "s:" is start of "s:var", but "n:" is not and can be used in
4441 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004442 len = (int)(p - *arg);
4443 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
4444 || len > 1)
4445 break;
4446 }
4447 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004448 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 return 0;
4450
4451 len = (int)(p - *arg);
4452 *arg = skipwhite(p);
4453
4454 return len;
4455}
4456
4457/*
Bram Moolenaara7043832005-01-21 11:56:39 +00004458 * Get the length of the name of a variable or function.
4459 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004461 * Return -1 if curly braces expansion failed.
4462 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 * If the name contains 'magic' {}'s, expand them and return the
4464 * expanded name in an allocated string via 'alias' - caller must free.
4465 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004466 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004467get_name_len(
4468 char_u **arg,
4469 char_u **alias,
4470 int evaluate,
4471 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472{
4473 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 char_u *p;
4475 char_u *expr_start;
4476 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004478 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479
4480 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
4481 && (*arg)[2] == (int)KE_SNR)
4482 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004483 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 *arg += 3;
4485 return get_id_len(arg) + 3;
4486 }
4487 len = eval_fname_script(*arg);
4488 if (len > 0)
4489 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004490 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 *arg += len;
4492 }
4493
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004495 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004497 p = find_name_end(*arg, &expr_start, &expr_end,
4498 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 if (expr_start != NULL)
4500 {
4501 char_u *temp_string;
4502
4503 if (!evaluate)
4504 {
4505 len += (int)(p - *arg);
4506 *arg = skipwhite(p);
4507 return len;
4508 }
4509
4510 /*
4511 * Include any <SID> etc in the expanded string:
4512 * Thus the -len here.
4513 */
4514 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
4515 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004516 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 *alias = temp_string;
4518 *arg = skipwhite(p);
4519 return (int)STRLEN(temp_string);
4520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521
4522 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01004523 // Only give an error when there is something, otherwise it will be
4524 // reported at a higher level.
4525 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004526 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527
4528 return len;
4529}
4530
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004531/*
4532 * Find the end of a variable or function name, taking care of magic braces.
4533 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
4534 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004535 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004536 * Return a pointer to just after the name. Equal to "arg" if there is no
4537 * valid name.
4538 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004539 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004540find_name_end(
4541 char_u *arg,
4542 char_u **expr_start,
4543 char_u **expr_end,
4544 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004546 int mb_nest = 0;
4547 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004549 int len;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004550 int vim9script = current_sctx.sc_version == SCRIPT_VERSION_VIM9;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004552 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004554 *expr_start = NULL;
4555 *expr_end = NULL;
4556 }
4557
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004558 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004559 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
4560 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004561 return arg;
4562
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004563 for (p = arg; *p != NUL
4564 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004565 || (*p == '{' && !vim9script)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004566 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004567 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004568 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004569 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00004570 if (*p == '\'')
4571 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004572 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004573 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00004574 ;
4575 if (*p == NUL)
4576 break;
4577 }
4578 else if (*p == '"')
4579 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004580 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004581 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00004582 if (*p == '\\' && p[1] != NUL)
4583 ++p;
4584 if (*p == NUL)
4585 break;
4586 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004587 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
4588 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004589 // "s:" is start of "s:var", but "n:" is not and can be used in
4590 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004591 len = (int)(p - arg);
4592 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01004593 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004594 break;
4595 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00004596
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004597 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004599 if (*p == '[')
4600 ++br_nest;
4601 else if (*p == ']')
4602 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00004604
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004605 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004607 if (*p == '{')
4608 {
4609 mb_nest++;
4610 if (expr_start != NULL && *expr_start == NULL)
4611 *expr_start = p;
4612 }
4613 else if (*p == '}')
4614 {
4615 mb_nest--;
4616 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
4617 *expr_end = p;
4618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 }
4621
4622 return p;
4623}
4624
4625/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004626 * Expands out the 'magic' {}'s in a variable/function name.
4627 * Note that this can call itself recursively, to deal with
4628 * constructs like foo{bar}{baz}{bam}
4629 * The four pointer arguments point to "foo{expre}ss{ion}bar"
4630 * "in_start" ^
4631 * "expr_start" ^
4632 * "expr_end" ^
4633 * "in_end" ^
4634 *
4635 * Returns a new allocated string, which the caller must free.
4636 * Returns NULL for failure.
4637 */
4638 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004639make_expanded_name(
4640 char_u *in_start,
4641 char_u *expr_start,
4642 char_u *expr_end,
4643 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004644{
4645 char_u c1;
4646 char_u *retval = NULL;
4647 char_u *temp_result;
4648 char_u *nextcmd = NULL;
4649
4650 if (expr_end == NULL || in_end == NULL)
4651 return NULL;
4652 *expr_start = NUL;
4653 *expr_end = NUL;
4654 c1 = *in_end;
4655 *in_end = NUL;
4656
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004657 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004658 if (temp_result != NULL && nextcmd == NULL)
4659 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02004660 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
4661 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004662 if (retval != NULL)
4663 {
4664 STRCPY(retval, in_start);
4665 STRCAT(retval, temp_result);
4666 STRCAT(retval, expr_end + 1);
4667 }
4668 }
4669 vim_free(temp_result);
4670
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004671 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004672 *expr_start = '{';
4673 *expr_end = '}';
4674
4675 if (retval != NULL)
4676 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004677 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004678 if (expr_start != NULL)
4679 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004680 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004681 temp_result = make_expanded_name(retval, expr_start,
4682 expr_end, temp_result);
4683 vim_free(retval);
4684 retval = temp_result;
4685 }
4686 }
4687
4688 return retval;
4689}
4690
4691/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00004693 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004695 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004696eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004698 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
4699}
4700
4701/*
4702 * Return TRUE if character "c" can be used as the first character in a
4703 * variable or function name (excluding '{' and '}').
4704 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004705 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004706eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004707{
4708 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709}
4710
4711/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004712 * Handle:
4713 * - expr[expr], expr[expr:expr] subscript
4714 * - ".name" lookup
4715 * - function call with Funcref variable: func(expr)
4716 * - method call: var->method()
4717 *
4718 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004719 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004720 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004721handle_subscript(
4722 char_u **arg,
4723 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004724 int flags, // do more than finding the end
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004725 int verbose, // give error messages
4726 char_u *start_leader, // start of '!' and '-' prefixes
4727 char_u **end_leaderp) // end of '!' and '-' prefixes
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004728{
Bram Moolenaar32e35112020-05-14 22:41:15 +02004729 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004730 int ret = OK;
4731 dict_T *selfdict = NULL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004732
Bram Moolenaar61343f02019-07-20 21:11:13 +02004733 // "." is ".name" lookup when we found a dict or when evaluating and
4734 // scriptversion is at least 2, where string concatenation is "..".
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004735 while (ret == OK
Bram Moolenaarac92e252019-08-03 21:58:38 +02004736 && (((**arg == '['
4737 || (**arg == '.' && (rettv->v_type == VAR_DICT
Bram Moolenaar61343f02019-07-20 21:11:13 +02004738 || (!evaluate
4739 && (*arg)[1] != '.'
4740 && current_sctx.sc_version >= 2)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02004741 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004742 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02004743 && !VIM_ISWHITE(*(*arg - 1)))
4744 || (**arg == '-' && (*arg)[1] == '>')))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004745 {
4746 if (**arg == '(')
4747 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004748 ret = call_func_rettv(arg, rettv, evaluate, selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01004749
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004750 // Stop the expression evaluation when immediately aborting on
4751 // error, or when an interrupt occurred or an exception was thrown
4752 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004753 if (aborting())
4754 {
4755 if (ret == OK)
4756 clear_tv(rettv);
4757 ret = FAIL;
4758 }
4759 dict_unref(selfdict);
4760 selfdict = NULL;
4761 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004762 else if (**arg == '-')
4763 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004764 // Expression "-1.0->method()" applies the leader "-" before
4765 // applying ->.
4766 if (evaluate && *end_leaderp > start_leader)
4767 ret = eval7_leader(rettv, start_leader, end_leaderp);
4768 if (ret == OK)
4769 {
4770 if ((*arg)[2] == '{')
4771 // expr->{lambda}()
4772 ret = eval_lambda(arg, rettv, evaluate, verbose);
4773 else
4774 // expr->name()
4775 ret = eval_method(arg, rettv, evaluate, verbose);
4776 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004777 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004778 else // **arg == '[' || **arg == '.'
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004779 {
4780 dict_unref(selfdict);
4781 if (rettv->v_type == VAR_DICT)
4782 {
4783 selfdict = rettv->vval.v_dict;
4784 if (selfdict != NULL)
4785 ++selfdict->dv_refcount;
4786 }
4787 else
4788 selfdict = NULL;
Bram Moolenaar32e35112020-05-14 22:41:15 +02004789 if (eval_index(arg, rettv, flags, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004790 {
4791 clear_tv(rettv);
4792 ret = FAIL;
4793 }
4794 }
4795 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01004796
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004797 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4798 // Don't do this when "Func" is already a partial that was bound
4799 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02004800 if (selfdict != NULL
4801 && (rettv->v_type == VAR_FUNC
4802 || (rettv->v_type == VAR_PARTIAL
4803 && (rettv->vval.v_partial->pt_auto
4804 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004805 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01004806
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004807 dict_unref(selfdict);
4808 return ret;
4809}
4810
4811/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004812 * Make a copy of an item.
4813 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004814 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
4815 * reference to an already copied list/dict can be used.
4816 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00004817 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004818 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004819item_copy(
4820 typval_T *from,
4821 typval_T *to,
4822 int deep,
4823 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004824{
4825 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004826 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004827
Bram Moolenaar33570922005-01-25 22:26:29 +00004828 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004829 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004830 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004831 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004832 }
4833 ++recurse;
4834
4835 switch (from->v_type)
4836 {
4837 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004838 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004839 case VAR_STRING:
4840 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004841 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004842 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01004843 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004844 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004845 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004846 copy_tv(from, to);
4847 break;
4848 case VAR_LIST:
4849 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004850 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004851 if (from->vval.v_list == NULL)
4852 to->vval.v_list = NULL;
4853 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
4854 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004855 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004856 to->vval.v_list = from->vval.v_list->lv_copylist;
4857 ++to->vval.v_list->lv_refcount;
4858 }
4859 else
4860 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
4861 if (to->vval.v_list == NULL)
4862 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004863 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01004864 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004865 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01004866 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004867 case VAR_DICT:
4868 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004869 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004870 if (from->vval.v_dict == NULL)
4871 to->vval.v_dict = NULL;
4872 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
4873 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004874 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004875 to->vval.v_dict = from->vval.v_dict->dv_copydict;
4876 ++to->vval.v_dict->dv_refcount;
4877 }
4878 else
4879 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
4880 if (to->vval.v_dict == NULL)
4881 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004882 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01004883 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004884 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004885 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01004886 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004887 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004888 }
4889 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004890 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004891}
4892
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004893 void
4894echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
4895{
4896 char_u *tofree;
4897 char_u numbuf[NUMBUFLEN];
4898 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
4899
4900 if (*atstart)
4901 {
4902 *atstart = FALSE;
4903 // Call msg_start() after eval1(), evaluating the expression
4904 // may cause a message to appear.
4905 if (with_space)
4906 {
4907 // Mark the saved text as finishing the line, so that what
4908 // follows is displayed on a new line when scrolling back
4909 // at the more prompt.
4910 msg_sb_eol();
4911 msg_start();
4912 }
4913 }
4914 else if (with_space)
4915 msg_puts_attr(" ", echo_attr);
4916
4917 if (p != NULL)
4918 for ( ; *p != NUL && !got_int; ++p)
4919 {
4920 if (*p == '\n' || *p == '\r' || *p == TAB)
4921 {
4922 if (*p != TAB && *needclr)
4923 {
4924 // remove any text still there from the command
4925 msg_clr_eos();
4926 *needclr = FALSE;
4927 }
4928 msg_putchar_attr(*p, echo_attr);
4929 }
4930 else
4931 {
4932 if (has_mbyte)
4933 {
4934 int i = (*mb_ptr2len)(p);
4935
4936 (void)msg_outtrans_len_attr(p, i, echo_attr);
4937 p += i - 1;
4938 }
4939 else
4940 (void)msg_outtrans_len_attr(p, 1, echo_attr);
4941 }
4942 }
4943 vim_free(tofree);
4944}
4945
Bram Moolenaare9a41262005-01-15 22:18:47 +00004946/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 * ":echo expr1 ..." print each argument separated with a space, add a
4948 * newline at the end.
4949 * ":echon expr1 ..." print each argument plain.
4950 */
4951 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004952ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953{
4954 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004955 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 char_u *p;
4957 int needclr = TRUE;
4958 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01004959 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01004960 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961
4962 if (eap->skip)
4963 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02004964 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004966 // If eval1() causes an error message the text from the command may
4967 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004968 need_clr_eos = needclr;
4969
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 p = arg;
Bram Moolenaar32e35112020-05-14 22:41:15 +02004971 if (eval1(&arg, &rettv, eap->skip ? 0 : EVAL_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 {
4973 /*
4974 * Report the invalid expression unless the expression evaluation
4975 * has been cancelled due to an aborting error, an interrupt, or an
4976 * exception.
4977 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01004978 if (!aborting() && did_emsg == did_emsg_before
4979 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004980 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004981 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 break;
4983 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004984 need_clr_eos = FALSE;
4985
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004987 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004988
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004989 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 arg = skipwhite(arg);
4991 }
4992 eap->nextcmd = check_nextcmd(arg);
4993
4994 if (eap->skip)
4995 --emsg_skip;
4996 else
4997 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004998 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 if (needclr)
5000 msg_clr_eos();
5001 if (eap->cmdidx == CMD_echo)
5002 msg_end();
5003 }
5004}
5005
5006/*
5007 * ":echohl {name}".
5008 */
5009 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005010ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005012 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013}
5014
5015/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005016 * Returns the :echo attribute
5017 */
5018 int
5019get_echo_attr(void)
5020{
5021 return echo_attr;
5022}
5023
5024/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 * ":execute expr1 ..." execute the result of an expression.
5026 * ":echomsg expr1 ..." Print a message
5027 * ":echoerr expr1 ..." Print an error
5028 * Each gets spaces around each argument and a newline at the end for
5029 * echo commands
5030 */
5031 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005032ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033{
5034 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005035 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 int ret = OK;
5037 char_u *p;
5038 garray_T ga;
5039 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040
5041 ga_init2(&ga, 1, 80);
5042
5043 if (eap->skip)
5044 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02005045 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01005047 ret = eval1_emsg(&arg, &rettv, !eap->skip);
5048 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050
5051 if (!eap->skip)
5052 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005053 char_u buf[NUMBUFLEN];
5054
5055 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01005056 {
5057 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
5058 {
5059 emsg(_(e_inval_string));
5060 p = NULL;
5061 }
5062 else
5063 p = tv_get_string_buf(&rettv, buf);
5064 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005065 else
5066 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005067 if (p == NULL)
5068 {
5069 clear_tv(&rettv);
5070 ret = FAIL;
5071 break;
5072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 len = (int)STRLEN(p);
5074 if (ga_grow(&ga, len + 2) == FAIL)
5075 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005076 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 ret = FAIL;
5078 break;
5079 }
5080 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 ga.ga_len += len;
5084 }
5085
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005086 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 arg = skipwhite(arg);
5088 }
5089
5090 if (ret != FAIL && ga.ga_data != NULL)
5091 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005092 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
5093 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005094 // Mark the already saved text as finishing the line, so that what
5095 // follows is displayed on a new line when scrolling back at the
5096 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005097 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005098 }
5099
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005101 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005102 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005103 out_flush();
5104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 else if (eap->cmdidx == CMD_echoerr)
5106 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005107 int save_did_emsg = did_emsg;
5108
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005109 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005110 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 if (!force_abort)
5112 did_emsg = save_did_emsg;
5113 }
5114 else if (eap->cmdidx == CMD_execute)
5115 do_cmdline((char_u *)ga.ga_data,
5116 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
5117 }
5118
5119 ga_clear(&ga);
5120
5121 if (eap->skip)
5122 --emsg_skip;
5123
5124 eap->nextcmd = check_nextcmd(arg);
5125}
5126
5127/*
5128 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
5129 * "arg" points to the "&" or '+' when called, to "option" when returning.
5130 * Returns NULL when no option name found. Otherwise pointer to the char
5131 * after the option name.
5132 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005133 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005134find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135{
5136 char_u *p = *arg;
5137
5138 ++p;
5139 if (*p == 'g' && p[1] == ':')
5140 {
5141 *opt_flags = OPT_GLOBAL;
5142 p += 2;
5143 }
5144 else if (*p == 'l' && p[1] == ':')
5145 {
5146 *opt_flags = OPT_LOCAL;
5147 p += 2;
5148 }
5149 else
5150 *opt_flags = 0;
5151
5152 if (!ASCII_ISALPHA(*p))
5153 return NULL;
5154 *arg = p;
5155
5156 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005157 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 else
5159 while (ASCII_ISALPHA(*p))
5160 ++p;
5161 return p;
5162}
5163
5164/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00005165 * Display script name where an item was last set.
5166 * Should only be invoked when 'verbose' is non-zero.
5167 */
5168 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005169last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005170{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005171 char_u *p;
5172
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005173 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005174 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005175 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005176 if (p != NULL)
5177 {
5178 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01005179 msg_puts(_("\n\tLast set from "));
5180 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005181 if (script_ctx.sc_lnum > 0)
5182 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01005183 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005184 msg_outnum((long)script_ctx.sc_lnum);
5185 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005186 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005187 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005188 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00005189 }
5190}
5191
Bram Moolenaarb005cd82019-09-04 15:54:55 +02005192#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193
5194/*
5195 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005196 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 * "flags" can be "g" to do a global substitute.
5198 * Returns an allocated string, NULL for error.
5199 */
5200 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005201do_string_sub(
5202 char_u *str,
5203 char_u *pat,
5204 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005205 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01005206 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207{
5208 int sublen;
5209 regmatch_T regmatch;
5210 int i;
5211 int do_all;
5212 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005213 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 garray_T ga;
5215 char_u *ret;
5216 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005217 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005219 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005221 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222
5223 ga_init2(&ga, 1, 200);
5224
5225 do_all = (flags[0] == 'g');
5226
5227 regmatch.rm_ic = p_ic;
5228 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
5229 if (regmatch.regprog != NULL)
5230 {
5231 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005232 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
5234 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005235 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01005236 if (regmatch.startp[0] == regmatch.endp[0])
5237 {
5238 if (zero_width == regmatch.startp[0])
5239 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005240 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02005241 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02005242 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
5243 (size_t)i);
5244 ga.ga_len += i;
5245 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005246 continue;
5247 }
5248 zero_width = regmatch.startp[0];
5249 }
5250
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 /*
5252 * Get some space for a temporary buffer to do the substitution
5253 * into. It will contain:
5254 * - The text up to where the match is.
5255 * - The substituted text.
5256 * - The text after the match.
5257 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005258 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01005259 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
5261 {
5262 ga_clear(&ga);
5263 break;
5264 }
5265
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005266 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 i = (int)(regmatch.startp[0] - tail);
5268 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005269 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005270 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 + ga.ga_len + i, TRUE, TRUE, FALSE);
5272 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02005273 tail = regmatch.endp[0];
5274 if (*tail == NUL)
5275 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 if (!do_all)
5277 break;
5278 }
5279
5280 if (ga.ga_data != NULL)
5281 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
5282
Bram Moolenaar473de612013-06-08 18:19:48 +02005283 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 }
5285
5286 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
5287 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005288 if (p_cpo == empty_option)
5289 p_cpo = save_cpo;
5290 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005291 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005292 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293
5294 return ret;
5295}