blob: 0c8ab49c7fa639beaaad36bb9e75495edc7ef793 [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 Moolenaar5409f5d2020-06-24 18:37:35 +020048static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
49static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
50static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
51static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
52static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
53static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +020054static int eval7_leader(typval_T *rettv, int numeric_only, 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,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200164 exarg_T *eap,
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 Moolenaarb171fb12020-06-24 20:34:03 +0200172 if (eval0(arg, &tv, eap, skip ? NULL : &EVALARG_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 Moolenaar5409f5d2020-06-24 18:37:35 +0200200 ret = eval1(arg, rettv, evaluate ? &EVALARG_EVALUATE : NULL);
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
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200256 && partial->pt_func->uf_def_status != 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,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200320 exarg_T *eap,
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 Moolenaarb171fb12020-06-24 20:34:03 +0200328 if (eval0(arg, &tv, eap, skip ? NULL : &EVALARG_EVALUATE)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200329 == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330 retval = NULL;
331 else
332 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100333 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000334 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 }
336 if (skip)
337 --emsg_skip;
338
339 return retval;
340}
341
342/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000343 * Skip over an expression at "*pp".
344 * Return FAIL for an error, OK otherwise.
345 */
346 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100347skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000348{
Bram Moolenaar33570922005-01-25 22:26:29 +0000349 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000350
351 *pp = skipwhite(*pp);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200352 return eval1(pp, &rettv, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000353}
354
355/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000357 * When "convert" is TRUE convert a List into a sequence of lines and convert
358 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 * Return pointer to allocated memory, or NULL for failure.
360 */
361 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100362eval_to_string(
363 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100364 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 Moolenaarb171fb12020-06-24 20:34:03 +0200373 if (eval0(arg, &tv, NULL, &EVALARG_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,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100411 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412{
413 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200414 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200416 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000417 if (use_sandbox)
418 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200419 ++textwinlock;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200420 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000421 if (use_sandbox)
422 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200423 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200424 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425 return retval;
426}
427
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428/*
429 * Top level evaluation function, returning a number.
430 * Evaluates "expr" silently.
431 * Returns -1 for an error.
432 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200433 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100434eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435{
Bram Moolenaar33570922005-01-25 22:26:29 +0000436 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200437 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000438 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439
440 ++emsg_off;
441
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200442 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 retval = -1;
444 else
445 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100446 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000447 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 }
449 --emsg_off;
450
451 return retval;
452}
453
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000454/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000455 * Top level evaluation function.
456 * Returns an allocated typval_T with the result.
457 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000458 */
459 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200460eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000461{
462 typval_T *tv;
463
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200464 tv = ALLOC_ONE(typval_T);
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200465 if (tv != NULL && eval0(arg, tv, eap, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100466 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000467
468 return tv;
469}
470
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100472 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200473 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
474 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000475 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200477 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100478call_vim_function(
479 char_u *func,
480 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200481 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200482 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000484 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200485 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100487 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200488 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200489 funcexe.firstline = curwin->w_cursor.lnum;
490 funcexe.lastline = curwin->w_cursor.lnum;
491 funcexe.evaluate = TRUE;
492 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000493 if (ret == FAIL)
494 clear_tv(rettv);
495
496 return ret;
497}
498
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100499/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100500 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100501 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200502 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
503 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100504 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200505 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100506call_func_retnr(
507 char_u *func,
508 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200509 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100510{
511 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200512 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100513
Bram Moolenaarded27a12018-08-01 19:06:03 +0200514 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100515 return -1;
516
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100517 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100518 clear_tv(&rettv);
519 return retval;
520}
521
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000522/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100523 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000524 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200525 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
526 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000527 */
528 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100529call_func_retstr(
530 char_u *func,
531 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200532 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000533{
534 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000535 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000536
Bram Moolenaarded27a12018-08-01 19:06:03 +0200537 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000538 return NULL;
539
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100540 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000541 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 return retval;
543}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000544
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000545/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100546 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200547 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
548 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000549 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000550 */
551 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100552call_func_retlist(
553 char_u *func,
554 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200555 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000556{
557 typval_T rettv;
558
Bram Moolenaarded27a12018-08-01 19:06:03 +0200559 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000560 return NULL;
561
562 if (rettv.v_type != VAR_LIST)
563 {
564 clear_tv(&rettv);
565 return NULL;
566 }
567
568 return rettv.vval.v_list;
569}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571#ifdef FEAT_FOLDING
572/*
573 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
574 * it in "*cp". Doesn't give error messages.
575 */
576 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100577eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578{
Bram Moolenaar33570922005-01-25 22:26:29 +0000579 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200580 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000582 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
583 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584
585 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000586 if (use_sandbox)
587 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200588 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200590 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 retval = 0;
592 else
593 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100594 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000595 if (tv.v_type == VAR_NUMBER)
596 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000597 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 retval = 0;
599 else
600 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100601 // If the result is a string, check if there is a non-digit before
602 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000603 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 if (!VIM_ISDIGIT(*s) && *s != '-')
605 *cp = *s++;
606 retval = atol((char *)s);
607 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000608 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 }
610 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000611 if (use_sandbox)
612 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200613 --textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200615 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616}
617#endif
618
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000620 * Get an lval: variable, Dict item or List item that can be assigned a value
621 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
622 * "name.key", "name.key[expr]" etc.
623 * Indexing only works if "name" is an existing List or Dictionary.
624 * "name" points to the start of the name.
625 * If "rettv" is not NULL it points to the value to be assigned.
626 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
627 * wrong; must end in space or cmd separator.
628 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100629 * flags:
630 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100631 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100632 * GLV_NO_AUTOLOAD: do not use script autoloading
633 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000634 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000635 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000636 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000637 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200638 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100639get_lval(
640 char_u *name,
641 typval_T *rettv,
642 lval_T *lp,
643 int unlet,
644 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100645 int flags, // GLV_ values
646 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000647{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000648 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000649 char_u *expr_start, *expr_end;
650 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000651 dictitem_T *v;
652 typval_T var1;
653 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000654 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000655 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000656 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000657 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +0000658 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100659 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000660
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100661 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200662 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000663
664 if (skip)
665 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100666 // When skipping just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000667 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000668 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000669 }
670
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100671 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000672 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100673 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000674 if (expr_start != NULL)
675 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100676 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100677 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000678 && *p != '[' && *p != '.')
679 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100680 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000681 return NULL;
682 }
683
684 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
685 if (lp->ll_exp_name == NULL)
686 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100687 // Report an invalid expression in braces, unless the
688 // expression evaluation has been cancelled due to an
689 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000690 if (!aborting() && !quiet)
691 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000692 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100693 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000694 return NULL;
695 }
696 }
697 lp->ll_name = lp->ll_exp_name;
698 }
699 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100700 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000701 lp->ll_name = name;
702
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100703 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9 && *p == ':')
704 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100705 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100706 char_u *tp = skipwhite(p + 1);
707
708 // parse the type after the name
709 lp->ll_type = parse_type(&tp, &si->sn_type_list);
710 lp->ll_name_end = tp;
711 }
712 }
713
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100714 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000715 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
716 return p;
717
718 cc = *p;
719 *p = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100720 // Only pass &ht when we would write to the variable, it prevents autoload
721 // as well.
Bram Moolenaar6e65d592017-12-07 22:11:27 +0100722 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
723 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000724 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100725 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000726 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000727 if (v == NULL)
728 return NULL;
729
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000730 /*
731 * Loop until no more [idx] or .key is following.
732 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000733 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100734 var1.v_type = VAR_UNKNOWN;
735 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000736 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000737 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000738 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
739 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100740 && lp->ll_tv->vval.v_dict != NULL)
741 && !(lp->ll_tv->v_type == VAR_BLOB
742 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000743 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000744 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100745 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000746 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000747 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000748 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000749 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000750 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100751 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000752 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000753 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000754
Bram Moolenaar8c711452005-01-14 21:53:12 +0000755 len = -1;
756 if (*p == '.')
757 {
758 key = p + 1;
759 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
760 ;
761 if (len == 0)
762 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000763 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100764 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000765 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000766 }
767 p = key + len;
768 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000769 else
770 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100771 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000772 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000773 if (*p == ':')
774 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000775 else
776 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000777 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200778 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000779 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100780 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000781 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100782 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000783 clear_tv(&var1);
784 return NULL;
785 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000786 }
787
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100788 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000789 if (*p == ':')
790 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000791 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000792 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000793 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100794 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100795 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000796 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000797 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100798 if (rettv != NULL
799 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100800 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100801 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100802 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +0000803 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000804 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100805 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100806 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000807 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000808 }
809 p = skipwhite(p + 1);
810 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000811 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000812 else
813 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000814 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200815 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200816 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000817 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100818 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000819 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000820 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100821 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000822 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100823 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100824 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000825 clear_tv(&var2);
826 return NULL;
827 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000828 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000829 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000830 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000831 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000832 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000833
Bram Moolenaar8c711452005-01-14 21:53:12 +0000834 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000835 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000836 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100837 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100838 clear_tv(&var1);
839 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000840 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000841 }
842
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100843 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000844 ++p;
845 }
846
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000847 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000848 {
849 if (len == -1)
850 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100851 // "[key]": get key from "var1"
852 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +0200853 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000854 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000855 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000856 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000857 }
858 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000859 lp->ll_list = NULL;
860 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000861 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200862
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100863 // When assigning to a scope dictionary check that a function and
864 // variable name is valid (only variable name unless it is l: or
865 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200866 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200867 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200868 int prevval;
869 int wrong;
870
871 if (len != -1)
872 {
873 prevval = key[len];
874 key[len] = NUL;
875 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +0200876 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100877 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200878 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
879 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200880 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200881 || !valid_varname(key);
882 if (len != -1)
883 key[len] = prevval;
884 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200885 {
886 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200887 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200888 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200889 }
890
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000891 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000892 {
Bram Moolenaar31b81602019-02-10 22:14:27 +0100893 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200894 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +0100895 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200896 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100897 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +0100898 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200899 return NULL;
900 }
901
Bram Moolenaar31b81602019-02-10 22:14:27 +0100902 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000903 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000904 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000905 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100906 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100907 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000908 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000909 }
910 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000911 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000912 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000913 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100914 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000915 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000916 p = NULL;
917 break;
918 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100919 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +0100920 else if ((flags & GLV_READ_ONLY) == 0
921 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +0100922 {
923 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200924 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +0100925 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200926
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100927 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000928 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000929 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100930 else if (lp->ll_tv->v_type == VAR_BLOB)
931 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100932 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
933
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100934 /*
935 * Get the number and item for the only or first index of the List.
936 */
937 if (empty1)
938 lp->ll_n1 = 0;
939 else
940 // is number or string
941 lp->ll_n1 = (long)tv_get_number(&var1);
942 clear_tv(&var1);
943
944 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100945 || lp->ll_n1 > bloblen
946 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100947 {
948 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100949 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100950 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100951 return NULL;
952 }
953 if (lp->ll_range && !lp->ll_empty2)
954 {
955 lp->ll_n2 = (long)tv_get_number(&var2);
956 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100957 if (lp->ll_n2 < 0
958 || lp->ll_n2 >= bloblen
959 || lp->ll_n2 < lp->ll_n1)
960 {
961 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100962 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100963 return NULL;
964 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100965 }
966 lp->ll_blob = lp->ll_tv->vval.v_blob;
967 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +0100968 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100969 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000970 else
971 {
972 /*
973 * Get the number and item for the only or first index of the List.
974 */
975 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000976 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000977 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100978 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100979 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100980 clear_tv(&var1);
981
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000982 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000983 lp->ll_list = lp->ll_tv->vval.v_list;
984 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
985 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000986 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000987 if (lp->ll_n1 < 0)
988 {
989 lp->ll_n1 = 0;
990 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
991 }
992 }
993 if (lp->ll_li == NULL)
994 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100995 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +0200996 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100997 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000998 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000999 }
1000
1001 /*
1002 * May need to find the item or absolute index for the second
1003 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001004 * When no index given: "lp->ll_empty2" is TRUE.
1005 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001006 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001007 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001008 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001009 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001010 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001011 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001012 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001013 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001014 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001015 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001016 {
1017 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001018 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001019 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001020 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001021 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001022 }
1023
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001024 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001025 if (lp->ll_n1 < 0)
1026 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1027 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001028 {
1029 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001030 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001031 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001032 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001033 }
1034
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001035 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001036 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001037 }
1038
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001039 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001040 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001041 return p;
1042}
1043
1044/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001045 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001046 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001047 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001048clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001049{
1050 vim_free(lp->ll_exp_name);
1051 vim_free(lp->ll_newkey);
1052}
1053
1054/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001055 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001056 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001057 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1058 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001059 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001060 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001061set_var_lval(
1062 lval_T *lp,
1063 char_u *endp,
1064 typval_T *rettv,
1065 int copy,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001066 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar7454a062016-01-30 15:14:10 +01001067 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001068{
1069 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001070 listitem_T *ri;
1071 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001072
1073 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001074 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001075 cc = *endp;
1076 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001077 if (lp->ll_blob != NULL)
1078 {
1079 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001080
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001081 if (op != NULL && *op != '=')
1082 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001083 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001084 return;
1085 }
1086
1087 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1088 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001089 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001090
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001091 if (lp->ll_empty2)
1092 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1093
1094 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001095 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001096 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001097 return;
1098 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001099 if (lp->ll_empty2)
1100 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001101
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001102 ir = 0;
1103 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1104 blob_set(lp->ll_blob, il,
1105 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001106 }
1107 else
1108 {
1109 val = (int)tv_get_number_chk(rettv, &error);
1110 if (!error)
1111 {
1112 garray_T *gap = &lp->ll_blob->bv_ga;
1113
1114 // Allow for appending a byte. Setting a byte beyond
1115 // the end is an error otherwise.
1116 if (lp->ll_n1 < gap->ga_len
1117 || (lp->ll_n1 == gap->ga_len
1118 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1119 {
1120 blob_set(lp->ll_blob, lp->ll_n1, val);
1121 if (lp->ll_n1 == gap->ga_len)
1122 ++gap->ga_len;
1123 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001124 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001125 }
1126 }
1127 }
1128 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001129 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001130 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001131
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001132 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001133 {
1134 emsg(_(e_cannot_mod));
1135 *endp = cc;
1136 return;
1137 }
1138
Bram Moolenaarff697e62019-02-12 22:28:33 +01001139 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001140 di = NULL;
1141 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
1142 &tv, &di, TRUE, FALSE) == OK)
1143 {
1144 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001145 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1146 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001147 && tv_op(&tv, rettv, op) == OK)
1148 set_var(lp->ll_name, &tv, FALSE);
1149 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001150 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001151 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001152 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001153 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001154 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001155 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001156 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001157 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001158 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001159 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001160 else if (lp->ll_range)
1161 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001162 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001163 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001164
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001165 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001166 {
1167 emsg(_("E996: Cannot lock a range"));
1168 return;
1169 }
1170
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001171 /*
1172 * Check whether any of the list items is locked
1173 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001174 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001175 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001176 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001177 return;
1178 ri = ri->li_next;
1179 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1180 break;
1181 ll_li = ll_li->li_next;
1182 ++ll_n1;
1183 }
1184
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001185 /*
1186 * Assign the List values to the list items.
1187 */
1188 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001189 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001190 if (op != NULL && *op != '=')
1191 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1192 else
1193 {
1194 clear_tv(&lp->ll_li->li_tv);
1195 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1196 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001197 ri = ri->li_next;
1198 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1199 break;
1200 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001201 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001202 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001203 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001204 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001205 ri = NULL;
1206 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001207 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001208 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001209 lp->ll_li = lp->ll_li->li_next;
1210 ++lp->ll_n1;
1211 }
1212 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001213 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001214 else if (lp->ll_empty2
1215 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001216 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001217 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001218 }
1219 else
1220 {
1221 /*
1222 * Assign to a List or Dictionary item.
1223 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001224 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001225 {
1226 emsg(_("E996: Cannot lock a list or dict"));
1227 return;
1228 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001229 if (lp->ll_newkey != NULL)
1230 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001231 if (op != NULL && *op != '=')
1232 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001233 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001234 return;
1235 }
1236
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001237 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001238 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001239 if (di == NULL)
1240 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001241 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1242 {
1243 vim_free(di);
1244 return;
1245 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001246 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001247 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001248 else if (op != NULL && *op != '=')
1249 {
1250 tv_op(lp->ll_tv, rettv, op);
1251 return;
1252 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001253 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001254 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001255
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001256 /*
1257 * Assign the value to the variable or list item.
1258 */
1259 if (copy)
1260 copy_tv(rettv, lp->ll_tv);
1261 else
1262 {
1263 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001264 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001265 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001266 }
1267 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001268}
1269
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001270/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001271 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1272 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001273 * Returns OK or FAIL.
1274 */
1275 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001276tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001277{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001278 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001279 char_u numbuf[NUMBUFLEN];
1280 char_u *s;
1281
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001282 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001283 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001284 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001285 {
1286 switch (tv1->v_type)
1287 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001288 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001289 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001290 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001291 case VAR_DICT:
1292 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001293 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001294 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001295 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001296 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001297 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001298 break;
1299
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001300 case VAR_BLOB:
1301 if (*op != '+' || tv2->v_type != VAR_BLOB)
1302 break;
1303 // BLOB += BLOB
1304 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1305 {
1306 blob_T *b1 = tv1->vval.v_blob;
1307 blob_T *b2 = tv2->vval.v_blob;
1308 int i, len = blob_len(b2);
1309 for (i = 0; i < len; i++)
1310 ga_append(&b1->bv_ga, blob_get(b2, i));
1311 }
1312 return OK;
1313
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001314 case VAR_LIST:
1315 if (*op != '+' || tv2->v_type != VAR_LIST)
1316 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001317 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001318 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
1319 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1320 return OK;
1321
1322 case VAR_NUMBER:
1323 case VAR_STRING:
1324 if (tv2->v_type == VAR_LIST)
1325 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001326 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001327 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001328 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001329 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001330#ifdef FEAT_FLOAT
1331 if (tv2->v_type == VAR_FLOAT)
1332 {
1333 float_T f = n;
1334
Bram Moolenaarff697e62019-02-12 22:28:33 +01001335 if (*op == '%')
1336 break;
1337 switch (*op)
1338 {
1339 case '+': f += tv2->vval.v_float; break;
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 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001344 clear_tv(tv1);
1345 tv1->v_type = VAR_FLOAT;
1346 tv1->vval.v_float = f;
1347 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001348 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001349#endif
1350 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001351 switch (*op)
1352 {
1353 case '+': n += tv_get_number(tv2); break;
1354 case '-': n -= tv_get_number(tv2); break;
1355 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001356 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1357 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001358 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001359 clear_tv(tv1);
1360 tv1->v_type = VAR_NUMBER;
1361 tv1->vval.v_number = n;
1362 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001363 }
1364 else
1365 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001366 if (tv2->v_type == VAR_FLOAT)
1367 break;
1368
Bram Moolenaarff697e62019-02-12 22:28:33 +01001369 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001370 s = tv_get_string(tv1);
1371 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001372 clear_tv(tv1);
1373 tv1->v_type = VAR_STRING;
1374 tv1->vval.v_string = s;
1375 }
1376 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001377
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001378 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001379#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001380 {
1381 float_T f;
1382
Bram Moolenaarff697e62019-02-12 22:28:33 +01001383 if (*op == '%' || *op == '.'
1384 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001385 && tv2->v_type != VAR_NUMBER
1386 && tv2->v_type != VAR_STRING))
1387 break;
1388 if (tv2->v_type == VAR_FLOAT)
1389 f = tv2->vval.v_float;
1390 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001391 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001392 switch (*op)
1393 {
1394 case '+': tv1->vval.v_float += f; break;
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 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001399 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001400#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001401 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001402 }
1403 }
1404
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001405 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001406 return FAIL;
1407}
1408
1409/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001410 * Evaluate the expression used in a ":for var in expr" command.
1411 * "arg" points to "var".
1412 * Set "*errp" to TRUE for an error, FALSE otherwise;
1413 * Return a pointer that holds the info. Null when there is an error.
1414 */
1415 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001416eval_for_line(
1417 char_u *arg,
1418 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001419 exarg_T *eap,
Bram Moolenaar7454a062016-01-30 15:14:10 +01001420 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001421{
Bram Moolenaar33570922005-01-25 22:26:29 +00001422 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001423 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001424 typval_T tv;
1425 list_T *l;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001426 evalarg_T evalarg;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001427
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001428 CLEAR_FIELD(evalarg);
1429 evalarg.eval_flags = skip ? 0 : EVAL_EVALUATE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001430 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001431
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001432 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001433 if (fi == NULL)
1434 return NULL;
1435
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001436 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001437 if (expr == NULL)
1438 return fi;
1439
1440 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001441 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001442 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001443 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001444 return fi;
1445 }
1446
1447 if (skip)
1448 ++emsg_skip;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001449 if (eval0(skipwhite(expr + 2), &tv, eap, &evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001450 {
1451 *errp = FALSE;
1452 if (!skip)
1453 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001454 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001455 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001456 l = tv.vval.v_list;
1457 if (l == NULL)
1458 {
1459 // a null list is like an empty list: do nothing
1460 clear_tv(&tv);
1461 }
1462 else
1463 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001464 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001465 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001466
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001467 // No need to increment the refcount, it's already set for
1468 // the list being used in "tv".
1469 fi->fi_list = l;
1470 list_add_watch(l, &fi->fi_lw);
1471 fi->fi_lw.lw_item = l->lv_first;
1472 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001473 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001474 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001475 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001476 fi->fi_bi = 0;
1477 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001478 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001479 typval_T btv;
1480
1481 // Make a copy, so that the iteration still works when the
1482 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001483 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001484 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001485 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001486 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001487 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001488 else
1489 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001490 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001491 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001492 }
1493 }
1494 }
1495 if (skip)
1496 --emsg_skip;
1497
1498 return fi;
1499}
1500
1501/*
1502 * Use the first item in a ":for" list. Advance to the next.
1503 * Assign the values to the variable (list). "arg" points to the first one.
1504 * Return TRUE when a valid item was found, FALSE when at end of list or
1505 * something wrong.
1506 */
1507 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001508next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001509{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001510 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001511 int result;
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001512 int flag = current_sctx.sc_version == SCRIPT_VERSION_VIM9 ?
1513 LET_NO_COMMAND : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001514 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001515
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001516 if (fi->fi_blob != NULL)
1517 {
1518 typval_T tv;
1519
1520 if (fi->fi_bi >= blob_len(fi->fi_blob))
1521 return FALSE;
1522 tv.v_type = VAR_NUMBER;
1523 tv.v_lock = VAR_FIXED;
1524 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1525 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001526 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001527 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001528 }
1529
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001530 item = fi->fi_lw.lw_item;
1531 if (item == NULL)
1532 result = FALSE;
1533 else
1534 {
1535 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001536 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001537 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001538 }
1539 return result;
1540}
1541
1542/*
1543 * Free the structure used to store info used by ":for".
1544 */
1545 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001546free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001547{
Bram Moolenaar33570922005-01-25 22:26:29 +00001548 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001549
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001550 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001551 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001552 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001553 list_unref(fi->fi_list);
1554 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001555 if (fi != NULL && fi->fi_blob != NULL)
1556 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001557 vim_free(fi);
1558}
1559
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001561set_context_for_expression(
1562 expand_T *xp,
1563 char_u *arg,
1564 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565{
1566 int got_eq = FALSE;
1567 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001568 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001570 if (cmdidx == CMD_let || cmdidx == CMD_const)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001571 {
1572 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001573 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001574 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001575 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001576 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001577 {
1578 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001579 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001580 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001581 break;
1582 }
1583 return;
1584 }
1585 }
1586 else
1587 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1588 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 while ((xp->xp_pattern = vim_strpbrk(arg,
1590 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1591 {
1592 c = *xp->xp_pattern;
1593 if (c == '&')
1594 {
1595 c = xp->xp_pattern[1];
1596 if (c == '&')
1597 {
1598 ++xp->xp_pattern;
1599 xp->xp_context = cmdidx != CMD_let || got_eq
1600 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1601 }
1602 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001603 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001605 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1606 xp->xp_pattern += 2;
1607
1608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 }
1610 else if (c == '$')
1611 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001612 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 xp->xp_context = EXPAND_ENV_VARS;
1614 }
1615 else if (c == '=')
1616 {
1617 got_eq = TRUE;
1618 xp->xp_context = EXPAND_EXPRESSION;
1619 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001620 else if (c == '#'
1621 && xp->xp_context == EXPAND_EXPRESSION)
1622 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001623 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001624 break;
1625 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001626 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 && xp->xp_context == EXPAND_FUNCTIONS
1628 && vim_strchr(xp->xp_pattern, '(') == NULL)
1629 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001630 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 break;
1632 }
1633 else if (cmdidx != CMD_let || got_eq)
1634 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001635 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 {
1637 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1638 if (c == '\\' && xp->xp_pattern[1] != NUL)
1639 ++xp->xp_pattern;
1640 xp->xp_context = EXPAND_NOTHING;
1641 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001642 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001644 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1646 /* skip */ ;
1647 xp->xp_context = EXPAND_NOTHING;
1648 }
1649 else if (c == '|')
1650 {
1651 if (xp->xp_pattern[1] == '|')
1652 {
1653 ++xp->xp_pattern;
1654 xp->xp_context = EXPAND_EXPRESSION;
1655 }
1656 else
1657 xp->xp_context = EXPAND_COMMANDS;
1658 }
1659 else
1660 xp->xp_context = EXPAND_EXPRESSION;
1661 }
1662 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001663 // Doesn't look like something valid, expand as an expression
1664 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001665 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 arg = xp->xp_pattern;
1667 if (*arg != NUL)
1668 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1669 /* skip */ ;
1670 }
1671 xp->xp_pattern = arg;
1672}
1673
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001675 * Return TRUE if "pat" matches "text".
1676 * Does not use 'cpo' and always uses 'magic'.
1677 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001678 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001679pattern_match(char_u *pat, char_u *text, int ic)
1680{
1681 int matches = FALSE;
1682 char_u *save_cpo;
1683 regmatch_T regmatch;
1684
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001685 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001686 save_cpo = p_cpo;
1687 p_cpo = (char_u *)"";
1688 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1689 if (regmatch.regprog != NULL)
1690 {
1691 regmatch.rm_ic = ic;
1692 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1693 vim_regfree(regmatch.regprog);
1694 }
1695 p_cpo = save_cpo;
1696 return matches;
1697}
1698
1699/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001700 * Handle a name followed by "(". Both for just "name(arg)" and for
1701 * "expr->name(arg)".
1702 * Returns OK or FAIL.
1703 */
1704 static int
1705eval_func(
1706 char_u **arg, // points to "(", will be advanced
1707 char_u *name,
1708 int name_len,
1709 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001710 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001711 typval_T *basetv) // "expr" for "expr->name(arg)"
1712{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001713 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001714 char_u *s = name;
1715 int len = name_len;
1716 partial_T *partial;
1717 int ret = OK;
1718
1719 if (!evaluate)
1720 check_vars(s, len);
1721
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001722 // If "s" is the name of a variable of type VAR_FUNC
1723 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001724 s = deref_func_name(s, &len, &partial, !evaluate);
1725
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001726 // Need to make a copy, in case evaluating the arguments makes
1727 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001728 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001729 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001730 ret = FAIL;
1731 else
1732 {
1733 funcexe_T funcexe;
1734
1735 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001736 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001737 funcexe.firstline = curwin->w_cursor.lnum;
1738 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001739 funcexe.evaluate = evaluate;
1740 funcexe.partial = partial;
1741 funcexe.basetv = basetv;
1742 ret = get_func_tv(s, len, rettv, arg, &funcexe);
1743 }
1744 vim_free(s);
1745
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001746 // If evaluate is FALSE rettv->v_type was not set in
1747 // get_func_tv, but it's needed in handle_subscript() to parse
1748 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001749 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
1750 {
1751 rettv->vval.v_string = NULL;
1752 rettv->v_type = VAR_FUNC;
1753 }
1754
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001755 // Stop the expression evaluation when immediately
1756 // aborting on error, or when an interrupt occurred or
1757 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001758 if (evaluate && aborting())
1759 {
1760 if (ret == OK)
1761 clear_tv(rettv);
1762 ret = FAIL;
1763 }
1764 return ret;
1765}
1766
1767/*
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001768 * If inside Vim9 script, "arg" points to the end of a line (ignoring comments)
1769 * and there is a next line, return the next line (skipping blanks) and set
1770 * "getnext".
1771 * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
1772 * "arg" must point somewhere inside a line, not at the start.
1773 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001774 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001775eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
1776{
1777 *getnext = FALSE;
1778 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9
1779 && evalarg != NULL
1780 && evalarg->eval_cookie != NULL
1781 && (*arg == NUL || (VIM_ISWHITE(arg[-1])
1782 && (*arg == '"' || *arg == '#')))
1783 && source_nextline(evalarg->eval_cookie) != NULL)
1784 {
1785 char_u *p = source_nextline(evalarg->eval_cookie);
1786
1787 if (p != NULL)
1788 {
1789 *getnext = TRUE;
1790 return skipwhite(p);
1791 }
1792 }
1793 return arg;
1794}
1795
1796/*
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001797 * To be called when eval_next_non_blank() sets "getnext" to TRUE.
1798 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001799 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001800eval_next_line(evalarg_T *evalarg)
1801{
1802 vim_free(evalarg->eval_tofree);
1803 evalarg->eval_tofree = getsourceline(0, evalarg->eval_cookie, 0, TRUE);
1804 return skipwhite(evalarg->eval_tofree);
1805}
1806
1807/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001809 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
1811 */
1812
1813/*
1814 * Handle zero level expression.
1815 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001816 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001817 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001818 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 * Return OK or FAIL.
1820 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001821 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001822eval0(
1823 char_u *arg,
1824 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001825 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001826 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827{
1828 int ret;
1829 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001830 int did_emsg_before = did_emsg;
1831 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001832 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001834 if (evalarg != NULL)
1835 evalarg->eval_tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001837 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001838
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001839 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 {
1841 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001842 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 /*
1844 * Report the invalid expression unless the expression evaluation has
1845 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001846 * exception, or we already gave a more specific error.
1847 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02001849 if (!aborting()
1850 && did_emsg == did_emsg_before
1851 && called_emsg == called_emsg_before
1852 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001853 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 ret = FAIL;
1855 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001856
1857 if (eap != NULL)
1858 eap->nextcmd = check_nextcmd(p);
1859
1860 if (evalarg != NULL)
1861 {
1862 if (eap != NULL)
1863 {
1864 if (evalarg->eval_tofree != NULL)
1865 {
1866 // We may need to keep the original command line, e.g. for
1867 // ":let" it has the variable names. But we may also need the
1868 // new one, "nextcmd" points into it. Keep both.
1869 vim_free(eap->cmdline_tofree);
1870 eap->cmdline_tofree = *eap->cmdlinep;
1871 *eap->cmdlinep = evalarg->eval_tofree;
1872 }
1873 }
1874 else
1875 vim_free(evalarg->eval_tofree);
1876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877
1878 return ret;
1879}
1880
1881/*
1882 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00001883 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 *
1885 * "arg" must point to the first non-white of the expression.
1886 * "arg" is advanced to the next non-white after the recognized expression.
1887 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00001888 * Note: "rettv.v_lock" is not set.
1889 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 * Return OK or FAIL.
1891 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02001892 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001893eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894{
Bram Moolenaar793648f2020-06-26 21:28:25 +02001895 char_u *p;
1896 int getnext;
1897
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 /*
1899 * Get the first variable.
1900 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001901 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 return FAIL;
1903
Bram Moolenaar793648f2020-06-26 21:28:25 +02001904 p = eval_next_non_blank(*arg, evalarg, &getnext);
1905 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001907 int result;
1908 typval_T var2;
1909 evalarg_T nested_evalarg;
1910 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02001911 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001912
Bram Moolenaar793648f2020-06-26 21:28:25 +02001913 if (getnext)
1914 *arg = eval_next_line(evalarg);
1915
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001916 if (evalarg == NULL)
1917 {
1918 CLEAR_FIELD(nested_evalarg);
1919 orig_flags = 0;
1920 }
1921 else
1922 {
1923 nested_evalarg = *evalarg;
1924 orig_flags = evalarg->eval_flags;
1925 }
1926
Bram Moolenaar7acde512020-06-24 23:02:40 +02001927 evaluate = nested_evalarg.eval_flags & EVAL_EVALUATE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001929 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001931 int error = FALSE;
1932
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001933 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001935 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001936 if (error)
1937 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 }
1939
1940 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02001941 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 */
1943 *arg = skipwhite(*arg + 1);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001944 nested_evalarg.eval_flags = result ? orig_flags
1945 : orig_flags & ~EVAL_EVALUATE;
1946 if (eval1(arg, rettv, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 return FAIL;
1948
1949 /*
1950 * Check for the ":".
1951 */
Bram Moolenaar793648f2020-06-26 21:28:25 +02001952 p = eval_next_non_blank(*arg, evalarg, &getnext);
1953 if (*p != ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001955 emsg(_(e_missing_colon));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001957 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 return FAIL;
1959 }
Bram Moolenaar793648f2020-06-26 21:28:25 +02001960 if (getnext)
1961 *arg = eval_next_line(evalarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962
1963 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02001964 * Get the third variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 */
1966 *arg = skipwhite(*arg + 1);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001967 nested_evalarg.eval_flags = !result ? orig_flags
1968 : orig_flags & ~EVAL_EVALUATE;
1969 if (eval1(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 {
1971 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001972 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 return FAIL;
1974 }
1975 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001976 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 }
1978
1979 return OK;
1980}
1981
1982/*
1983 * Handle first level expression:
1984 * expr2 || expr2 || expr2 logical OR
1985 *
1986 * "arg" must point to the first non-white of the expression.
1987 * "arg" is advanced to the next non-white after the recognized expression.
1988 *
1989 * Return OK or FAIL.
1990 */
1991 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001992eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02001994 char_u *p;
1995 int getnext;
Bram Moolenaar33570922005-01-25 22:26:29 +00001996 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 long result;
1998 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001999 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000
2001 /*
2002 * Get the first variable.
2003 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002004 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 return FAIL;
2006
2007 /*
2008 * Repeat until there is no following "||".
2009 */
2010 first = TRUE;
2011 result = FALSE;
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002012 p = eval_next_non_blank(*arg, evalarg, &getnext);
2013 while (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002015 evalarg_T nested_evalarg;
2016 int evaluate;
2017 int orig_flags;
2018
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002019 if (getnext)
2020 *arg = eval_next_line(evalarg);
2021
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002022 if (evalarg == NULL)
2023 {
2024 CLEAR_FIELD(nested_evalarg);
2025 orig_flags = 0;
2026 evaluate = FALSE;
2027 }
2028 else
2029 {
2030 nested_evalarg = *evalarg;
2031 orig_flags = evalarg->eval_flags;
2032 evaluate = orig_flags & EVAL_EVALUATE;
2033 }
Bram Moolenaar32e35112020-05-14 22:41:15 +02002034
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 if (evaluate && first)
2036 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002037 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002039 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002040 if (error)
2041 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 first = FALSE;
2043 }
2044
2045 /*
2046 * Get the second variable.
2047 */
2048 *arg = skipwhite(*arg + 2);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002049 nested_evalarg.eval_flags = !result ? orig_flags
2050 : orig_flags & ~EVAL_EVALUATE;
2051 if (eval3(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 return FAIL;
2053
2054 /*
2055 * Compute the result.
2056 */
2057 if (evaluate && !result)
2058 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002059 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002061 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002062 if (error)
2063 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 }
2065 if (evaluate)
2066 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002067 rettv->v_type = VAR_NUMBER;
2068 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002070
2071 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 }
2073
2074 return OK;
2075}
2076
2077/*
2078 * Handle second level expression:
2079 * expr3 && expr3 && expr3 logical AND
2080 *
2081 * "arg" must point to the first non-white of the expression.
2082 * "arg" is advanced to the next non-white after the recognized expression.
2083 *
2084 * Return OK or FAIL.
2085 */
2086 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002087eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002089 char_u *p;
2090 int getnext;
Bram Moolenaar33570922005-01-25 22:26:29 +00002091 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 long result;
2093 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002094 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095
2096 /*
2097 * Get the first variable.
2098 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002099 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 return FAIL;
2101
2102 /*
2103 * Repeat until there is no following "&&".
2104 */
2105 first = TRUE;
2106 result = TRUE;
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002107 p = eval_next_non_blank(*arg, evalarg, &getnext);
2108 while (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002110 evalarg_T nested_evalarg;
2111 int orig_flags;
2112 int evaluate;
Bram Moolenaar32e35112020-05-14 22:41:15 +02002113
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002114 if (getnext)
2115 *arg = eval_next_line(evalarg);
2116
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002117 if (evalarg == NULL)
2118 {
2119 CLEAR_FIELD(nested_evalarg);
2120 orig_flags = 0;
2121 evaluate = FALSE;
2122 }
2123 else
2124 {
2125 nested_evalarg = *evalarg;
2126 orig_flags = evalarg->eval_flags;
2127 evaluate = orig_flags & EVAL_EVALUATE;
2128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 if (evaluate && first)
2130 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002131 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002133 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002134 if (error)
2135 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 first = FALSE;
2137 }
2138
2139 /*
2140 * Get the second variable.
2141 */
2142 *arg = skipwhite(*arg + 2);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002143 nested_evalarg.eval_flags = result ? orig_flags
2144 : orig_flags & ~EVAL_EVALUATE;
2145 if (eval4(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 return FAIL;
2147
2148 /*
2149 * Compute the result.
2150 */
2151 if (evaluate && result)
2152 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002153 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002155 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002156 if (error)
2157 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 }
2159 if (evaluate)
2160 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002161 rettv->v_type = VAR_NUMBER;
2162 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002164
2165 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 }
2167
2168 return OK;
2169}
2170
2171/*
2172 * Handle third level expression:
2173 * var1 == var2
2174 * var1 =~ var2
2175 * var1 != var2
2176 * var1 !~ var2
2177 * var1 > var2
2178 * var1 >= var2
2179 * var1 < var2
2180 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002181 * var1 is var2
2182 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 *
2184 * "arg" must point to the first non-white of the expression.
2185 * "arg" is advanced to the next non-white after the recognized expression.
2186 *
2187 * Return OK or FAIL.
2188 */
2189 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002190eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191{
Bram Moolenaar33570922005-01-25 22:26:29 +00002192 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002194 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 int i;
Bram Moolenaar87396072019-12-31 22:36:18 +01002196 exptype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199
2200 /*
2201 * Get the first variable.
2202 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002203 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 return FAIL;
2205
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002206 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 switch (p[0])
2208 {
2209 case '=': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002210 type = EXPR_EQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002212 type = EXPR_MATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 break;
2214 case '!': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002215 type = EXPR_NEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002217 type = EXPR_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 break;
2219 case '>': if (p[1] != '=')
2220 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002221 type = EXPR_GREATER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 len = 1;
2223 }
2224 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002225 type = EXPR_GEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 break;
2227 case '<': if (p[1] != '=')
2228 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002229 type = EXPR_SMALLER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 len = 1;
2231 }
2232 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002233 type = EXPR_SEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002235 case 'i': if (p[1] == 's')
2236 {
2237 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2238 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02002239 i = p[len];
2240 if (!isalnum(i) && i != '_')
Bram Moolenaar87396072019-12-31 22:36:18 +01002241 type = len == 2 ? EXPR_IS : EXPR_ISNOT;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002242 }
2243 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 }
2245
2246 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002247 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002249 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002251 if (getnext)
2252 *arg = eval_next_line(evalarg);
2253
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002254 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 if (p[len] == '?')
2256 {
2257 ic = TRUE;
2258 ++len;
2259 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002260 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 else if (p[len] == '#')
2262 {
2263 ic = FALSE;
2264 ++len;
2265 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002266 // nothing appended: use 'ignorecase'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 else
2268 ic = p_ic;
2269
2270 /*
2271 * Get the second variable.
2272 */
2273 *arg = skipwhite(p + len);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002274 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002276 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 return FAIL;
2278 }
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002279 if (evalarg != NULL && (evalarg->eval_flags & EVAL_EVALUATE))
Bram Moolenaar31988702018-02-13 12:57:42 +01002280 {
Bram Moolenaar07a3db82019-12-25 18:14:14 +01002281 int ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002282
2283 clear_tv(&var2);
2284 return ret;
2285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 }
2287
2288 return OK;
2289}
2290
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002291 void
2292eval_addblob(typval_T *tv1, typval_T *tv2)
2293{
2294 blob_T *b1 = tv1->vval.v_blob;
2295 blob_T *b2 = tv2->vval.v_blob;
2296 blob_T *b = blob_alloc();
2297 int i;
2298
2299 if (b != NULL)
2300 {
2301 for (i = 0; i < blob_len(b1); i++)
2302 ga_append(&b->bv_ga, blob_get(b1, i));
2303 for (i = 0; i < blob_len(b2); i++)
2304 ga_append(&b->bv_ga, blob_get(b2, i));
2305
2306 clear_tv(tv1);
2307 rettv_blob_set(tv1, b);
2308 }
2309}
2310
2311 int
2312eval_addlist(typval_T *tv1, typval_T *tv2)
2313{
2314 typval_T var3;
2315
2316 // concatenate Lists
2317 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2318 {
2319 clear_tv(tv1);
2320 clear_tv(tv2);
2321 return FAIL;
2322 }
2323 clear_tv(tv1);
2324 *tv1 = var3;
2325 return OK;
2326}
2327
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328/*
2329 * Handle fourth level expression:
2330 * + number addition
2331 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002332 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002333 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 *
2335 * "arg" must point to the first non-white of the expression.
2336 * "arg" is advanced to the next non-white after the recognized expression.
2337 *
2338 * Return OK or FAIL.
2339 */
2340 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002341eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002343 int evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344
2345 /*
2346 * Get the first variable.
2347 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002348 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 return FAIL;
2350
2351 /*
2352 * Repeat computing, until no '+', '-' or '.' is following.
2353 */
2354 for (;;)
2355 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002356 int getnext;
2357 char_u *p;
2358 int op;
2359 int concat;
2360 typval_T var2;
2361
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002362 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002363 p = eval_next_non_blank(*arg, evalarg, &getnext);
2364 op = *p;
2365 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002366 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 break;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002368 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002369 *arg = eval_next_line(evalarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002371 if ((op != '+' || (rettv->v_type != VAR_LIST
2372 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002373#ifdef FEAT_FLOAT
2374 && (op == '.' || rettv->v_type != VAR_FLOAT)
2375#endif
2376 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002377 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002378 // For "list + ...", an illegal use of the first operand as
2379 // a number cannot be determined before evaluating the 2nd
2380 // operand: if this is also a list, all is ok.
2381 // For "something . ...", "something - ..." or "non-list + ...",
2382 // we know that the first operand needs to be a string or number
2383 // without evaluating the 2nd operand. So check before to avoid
2384 // side effects after an error.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002385 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002386 {
2387 clear_tv(rettv);
2388 return FAIL;
2389 }
2390 }
2391
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 /*
2393 * Get the second variable.
2394 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002395 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
2396 ++*arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 *arg = skipwhite(*arg + 1);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002398 if (eval6(arg, &var2, evalarg, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002400 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 return FAIL;
2402 }
2403
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002404 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 {
2406 /*
2407 * Compute the result.
2408 */
2409 if (op == '.')
2410 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002411 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2412 char_u *s1 = tv_get_string_buf(rettv, buf1);
2413 char_u *s2 = tv_get_string_buf_chk(&var2, buf2);
2414
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002415 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002416 {
2417 clear_tv(rettv);
2418 clear_tv(&var2);
2419 return FAIL;
2420 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002421 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002422 clear_tv(rettv);
2423 rettv->v_type = VAR_STRING;
2424 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002426 else if (op == '+' && rettv->v_type == VAR_BLOB
2427 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002428 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002429 else if (op == '+' && rettv->v_type == VAR_LIST
2430 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002431 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002432 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002433 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 else
2436 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002437 int error = FALSE;
2438 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002439#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002440 float_T f1 = 0, f2 = 0;
2441
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002442 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002443 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002444 f1 = rettv->vval.v_float;
2445 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002446 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002447 else
2448#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002449 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002450 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002451 if (error)
2452 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002453 // This can only happen for "list + non-list". For
2454 // "non-list + ..." or "something - ...", we returned
2455 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002456 clear_tv(rettv);
2457 return FAIL;
2458 }
2459#ifdef FEAT_FLOAT
2460 if (var2.v_type == VAR_FLOAT)
2461 f1 = n1;
2462#endif
2463 }
2464#ifdef FEAT_FLOAT
2465 if (var2.v_type == VAR_FLOAT)
2466 {
2467 f2 = var2.vval.v_float;
2468 n2 = 0;
2469 }
2470 else
2471#endif
2472 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002473 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002474 if (error)
2475 {
2476 clear_tv(rettv);
2477 clear_tv(&var2);
2478 return FAIL;
2479 }
2480#ifdef FEAT_FLOAT
2481 if (rettv->v_type == VAR_FLOAT)
2482 f2 = n2;
2483#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002484 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002485 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002486
2487#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002488 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002489 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2490 {
2491 if (op == '+')
2492 f1 = f1 + f2;
2493 else
2494 f1 = f1 - f2;
2495 rettv->v_type = VAR_FLOAT;
2496 rettv->vval.v_float = f1;
2497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002499#endif
2500 {
2501 if (op == '+')
2502 n1 = n1 + n2;
2503 else
2504 n1 = n1 - n2;
2505 rettv->v_type = VAR_NUMBER;
2506 rettv->vval.v_number = n1;
2507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002509 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 }
2511 }
2512 return OK;
2513}
2514
2515/*
2516 * Handle fifth level expression:
2517 * * number multiplication
2518 * / number division
2519 * % number modulo
2520 *
2521 * "arg" must point to the first non-white of the expression.
2522 * "arg" is advanced to the next non-white after the recognized expression.
2523 *
2524 * Return OK or FAIL.
2525 */
2526 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002527eval6(
2528 char_u **arg,
2529 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002530 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002531 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532{
Bram Moolenaar33570922005-01-25 22:26:29 +00002533 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002535 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002536#ifdef FEAT_FLOAT
2537 int use_float = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02002538 float_T f1 = 0, f2 = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002539#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002540 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541
2542 /*
2543 * Get the first variable.
2544 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002545 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 return FAIL;
2547
2548 /*
2549 * Repeat computing, until no '*', '/' or '%' is following.
2550 */
2551 for (;;)
2552 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002553 int evaluate = evalarg == NULL ? 0
2554 : (evalarg->eval_flags & EVAL_EVALUATE);
2555 int getnext;
2556
2557 op = *eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002558 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 break;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002560 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002561 *arg = eval_next_line(evalarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002563 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002565#ifdef FEAT_FLOAT
2566 if (rettv->v_type == VAR_FLOAT)
2567 {
2568 f1 = rettv->vval.v_float;
2569 use_float = TRUE;
2570 n1 = 0;
2571 }
2572 else
2573#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002574 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002575 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002576 if (error)
2577 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 }
2579 else
2580 n1 = 0;
2581
2582 /*
2583 * Get the second variable.
2584 */
2585 *arg = skipwhite(*arg + 1);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002586 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 return FAIL;
2588
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002589 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002591#ifdef FEAT_FLOAT
2592 if (var2.v_type == VAR_FLOAT)
2593 {
2594 if (!use_float)
2595 {
2596 f1 = n1;
2597 use_float = TRUE;
2598 }
2599 f2 = var2.vval.v_float;
2600 n2 = 0;
2601 }
2602 else
2603#endif
2604 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002605 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002606 clear_tv(&var2);
2607 if (error)
2608 return FAIL;
2609#ifdef FEAT_FLOAT
2610 if (use_float)
2611 f2 = n2;
2612#endif
2613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614
2615 /*
2616 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002617 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002619#ifdef FEAT_FLOAT
2620 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002622 if (op == '*')
2623 f1 = f1 * f2;
2624 else if (op == '/')
2625 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002626# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002627 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002628 if (f2 == 0.0)
2629 {
2630 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002631 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002632 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002633 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002634 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002635 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002636 }
2637 else
2638 f1 = f1 / f2;
2639# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002640 // We rely on the floating point library to handle divide
2641 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002642 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002643# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002646 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002647 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002648 return FAIL;
2649 }
2650 rettv->v_type = VAR_FLOAT;
2651 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 }
2653 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002654#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002656 if (op == '*')
2657 n1 = n1 * n2;
2658 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01002659 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01002661 n1 = num_modulus(n1, n2);
2662
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002663 rettv->v_type = VAR_NUMBER;
2664 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 }
2667 }
2668
2669 return OK;
2670}
2671
2672/*
2673 * Handle sixth level expression:
2674 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002675 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002676 * "string" string constant
2677 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 * &option-name option value
2679 * @r register contents
2680 * identifier variable value
2681 * function() function call
2682 * $VAR environment variable
2683 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002684 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002685 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002686 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002687 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 *
2689 * Also handle:
2690 * ! in front logical NOT
2691 * - in front unary minus
2692 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 * trailing [] subscript in String or List
2694 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02002695 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 *
2697 * "arg" must point to the first non-white of the expression.
2698 * "arg" is advanced to the next non-white after the recognized expression.
2699 *
2700 * Return OK or FAIL.
2701 */
2702 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002703eval7(
2704 char_u **arg,
2705 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002706 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002707 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002709 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
2710 int evaluate = evalarg != NULL
2711 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 int len;
2713 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 char_u *start_leader, *end_leader;
2715 int ret = OK;
2716 char_u *alias;
2717
2718 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002719 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002720 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002722 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723
2724 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02002725 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 */
2727 start_leader = *arg;
2728 while (**arg == '!' || **arg == '-' || **arg == '+')
2729 *arg = skipwhite(*arg + 1);
2730 end_leader = *arg;
2731
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002732 if (**arg == '.' && (!isdigit(*(*arg + 1))
2733#ifdef FEAT_FLOAT
2734 || current_sctx.sc_version < 2
2735#endif
2736 ))
2737 {
2738 semsg(_(e_invexpr2), *arg);
2739 ++*arg;
2740 return FAIL;
2741 }
2742
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 switch (**arg)
2744 {
2745 /*
2746 * Number constant.
2747 */
2748 case '0':
2749 case '1':
2750 case '2':
2751 case '3':
2752 case '4':
2753 case '5':
2754 case '6':
2755 case '7':
2756 case '8':
2757 case '9':
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002758 case '.': ret = get_number_tv(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02002759
2760 // Apply prefixed "-" and "+" now. Matters especially when
2761 // "->" follows.
2762 if (ret == OK && evaluate && end_leader > start_leader)
2763 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 break;
2765
2766 /*
2767 * String constant: "string".
2768 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002769 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 break;
2771
2772 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002773 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002775 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002776 break;
2777
2778 /*
2779 * List: [expr, expr]
2780 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002781 case '[': ret = get_list_tv(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 break;
2783
2784 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002785 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002786 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002787 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002788 {
2789 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002790 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002791 }
2792 else
2793 ret = NOTDONE;
2794 break;
2795
2796 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002797 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002798 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00002799 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002800 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
2801 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002802 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002803 break;
2804
2805 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00002806 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00002808 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 break;
2810
2811 /*
2812 * Environment variable: $VAR.
2813 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002814 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 break;
2816
2817 /*
2818 * Register contents: @r.
2819 */
2820 case '@': ++*arg;
2821 if (evaluate)
2822 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002823 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002824 rettv->vval.v_string = get_reg_contents(**arg,
2825 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 }
2827 if (**arg != NUL)
2828 ++*arg;
2829 break;
2830
2831 /*
2832 * nested expression: (expression).
2833 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002834 case '(': {
2835 *arg = skipwhite(*arg + 1);
2836 ret = eval1(arg, rettv, evalarg); // recursive!
2837 if (**arg == ')')
2838 ++*arg;
2839 else if (ret == OK)
2840 {
2841 emsg(_(e_missing_close));
2842 clear_tv(rettv);
2843 ret = FAIL;
2844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 }
2846 break;
2847
Bram Moolenaar8c711452005-01-14 21:53:12 +00002848 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 break;
2850 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002851
2852 if (ret == NOTDONE)
2853 {
2854 /*
2855 * Must be a variable or function name.
2856 * Can also be a curly-braces kind of name: {expr}.
2857 */
2858 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002859 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002860 if (alias != NULL)
2861 s = alias;
2862
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002863 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002864 ret = FAIL;
2865 else
2866 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002867 if (**arg == '(') // recursive!
Bram Moolenaar32e35112020-05-14 22:41:15 +02002868 ret = eval_func(arg, s, len, rettv, flags, NULL);
Bram Moolenaar227a69d2020-05-15 18:17:28 +02002869 else if (flags & EVAL_CONSTANT)
2870 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002871 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002872 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002873 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002874 {
2875 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002876 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002877 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002878 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01002879 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002880 }
2881
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 *arg = skipwhite(*arg);
2883
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002884 // Handle following '[', '(' and '.' for expr[expr], expr.name,
2885 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002886 if (ret == OK)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02002887 ret = handle_subscript(arg, rettv, flags, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888
2889 /*
2890 * Apply logical NOT and unary '-', from right to left, ignore '+'.
2891 */
2892 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02002893 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002894 return ret;
2895}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002896
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002897/*
2898 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02002899 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002900 * Adjusts "end_leaderp" until it is at "start_leader".
2901 */
2902 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02002903eval7_leader(
2904 typval_T *rettv,
2905 int numeric_only,
2906 char_u *start_leader,
2907 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002908{
2909 char_u *end_leader = *end_leaderp;
2910 int ret = OK;
2911 int error = FALSE;
2912 varnumber_T val = 0;
2913#ifdef FEAT_FLOAT
2914 float_T f = 0.0;
2915
2916 if (rettv->v_type == VAR_FLOAT)
2917 f = rettv->vval.v_float;
2918 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002919#endif
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002920 val = tv_get_number_chk(rettv, &error);
2921 if (error)
2922 {
2923 clear_tv(rettv);
2924 ret = FAIL;
2925 }
2926 else
2927 {
2928 while (end_leader > start_leader)
2929 {
2930 --end_leader;
2931 if (*end_leader == '!')
2932 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02002933 if (numeric_only)
2934 {
2935 ++end_leader;
2936 break;
2937 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002938#ifdef FEAT_FLOAT
2939 if (rettv->v_type == VAR_FLOAT)
2940 f = !f;
2941 else
2942#endif
2943 val = !val;
2944 }
2945 else if (*end_leader == '-')
2946 {
2947#ifdef FEAT_FLOAT
2948 if (rettv->v_type == VAR_FLOAT)
2949 f = -f;
2950 else
2951#endif
2952 val = -val;
2953 }
2954 }
2955#ifdef FEAT_FLOAT
2956 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002958 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002959 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002961 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002962#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002963 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002964 clear_tv(rettv);
2965 rettv->v_type = VAR_NUMBER;
2966 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002969 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 return ret;
2971}
2972
2973/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002974 * Call the function referred to in "rettv".
2975 */
2976 static int
2977call_func_rettv(
2978 char_u **arg,
2979 typval_T *rettv,
2980 int evaluate,
2981 dict_T *selfdict,
2982 typval_T *basetv)
2983{
2984 partial_T *pt = NULL;
2985 funcexe_T funcexe;
2986 typval_T functv;
2987 char_u *s;
2988 int ret;
2989
2990 // need to copy the funcref so that we can clear rettv
2991 if (evaluate)
2992 {
2993 functv = *rettv;
2994 rettv->v_type = VAR_UNKNOWN;
2995
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002996 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002997 if (functv.v_type == VAR_PARTIAL)
2998 {
2999 pt = functv.vval.v_partial;
3000 s = partial_name(pt);
3001 }
3002 else
3003 s = functv.vval.v_string;
3004 }
3005 else
3006 s = (char_u *)"";
3007
Bram Moolenaara80faa82020-04-12 19:37:17 +02003008 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003009 funcexe.firstline = curwin->w_cursor.lnum;
3010 funcexe.lastline = curwin->w_cursor.lnum;
3011 funcexe.evaluate = evaluate;
3012 funcexe.partial = pt;
3013 funcexe.selfdict = selfdict;
3014 funcexe.basetv = basetv;
3015 ret = get_func_tv(s, -1, rettv, arg, &funcexe);
3016
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003017 // Clear the funcref afterwards, so that deleting it while
3018 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003019 if (evaluate)
3020 clear_tv(&functv);
3021
3022 return ret;
3023}
3024
3025/*
3026 * Evaluate "->method()".
3027 * "*arg" points to the '-'.
3028 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3029 */
3030 static int
3031eval_lambda(
3032 char_u **arg,
3033 typval_T *rettv,
3034 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003035 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003036{
3037 typval_T base = *rettv;
3038 int ret;
3039
3040 // Skip over the ->.
3041 *arg += 2;
3042 rettv->v_type = VAR_UNKNOWN;
3043
3044 ret = get_lambda_tv(arg, rettv, evaluate);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003045 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003046 return FAIL;
3047 else if (**arg != '(')
3048 {
3049 if (verbose)
3050 {
3051 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003052 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003053 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003054 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003055 }
3056 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003057 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003058 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003059 else
3060 ret = call_func_rettv(arg, rettv, evaluate, NULL, &base);
3061
3062 // Clear the funcref afterwards, so that deleting it while
3063 // evaluating the arguments is possible (see test55).
3064 if (evaluate)
3065 clear_tv(&base);
3066
3067 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003068}
3069
3070/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003071 * Evaluate "->method()".
3072 * "*arg" points to the '-'.
3073 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3074 */
3075 static int
3076eval_method(
3077 char_u **arg,
3078 typval_T *rettv,
3079 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003080 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003081{
3082 char_u *name;
3083 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003084 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003085 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003086 int ret;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003087
3088 // Skip over the ->.
3089 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003090 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003091
Bram Moolenaarac92e252019-08-03 21:58:38 +02003092 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003093 len = get_name_len(arg, &alias, evaluate, TRUE);
3094 if (alias != NULL)
3095 name = alias;
3096
3097 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003098 {
3099 if (verbose)
3100 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003101 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003102 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003103 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003104 {
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003105 if (**arg != '(')
3106 {
3107 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003108 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003109 ret = FAIL;
3110 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003111 else if (VIM_ISWHITE((*arg)[-1]))
3112 {
3113 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003114 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003115 ret = FAIL;
3116 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003117 else
Bram Moolenaar32e35112020-05-14 22:41:15 +02003118 ret = eval_func(arg, name, len, rettv,
3119 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003120 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003121
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003122 // Clear the funcref afterwards, so that deleting it while
3123 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003124 if (evaluate)
3125 clear_tv(&base);
3126
Bram Moolenaarac92e252019-08-03 21:58:38 +02003127 return ret;
3128}
3129
3130/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003131 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3132 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003133 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3134 */
3135 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003136eval_index(
3137 char_u **arg,
3138 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02003139 int flags,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003140 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003141{
Bram Moolenaar32e35112020-05-14 22:41:15 +02003142 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003143 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003144 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003145 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003146 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003147 long len = -1;
3148 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003149 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003150 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003151
Bram Moolenaara03f2332016-02-06 18:09:59 +01003152 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003153 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003154 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003155 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003156 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003157 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003158 return FAIL;
3159 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003160#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01003161 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003162 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003163 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003164#endif
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003165 case VAR_BOOL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003166 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003167 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003168 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003169 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003170 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003171 return FAIL;
3172 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003173 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003174 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003175 if (evaluate)
3176 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003177 // FALLTHROUGH
Bram Moolenaara03f2332016-02-06 18:09:59 +01003178
3179 case VAR_STRING:
3180 case VAR_NUMBER:
3181 case VAR_LIST:
3182 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003183 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003184 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003185 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003186
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003187 init_tv(&var1);
3188 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003189 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003190 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003191 /*
3192 * dict.name
3193 */
3194 key = *arg + 1;
3195 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
3196 ;
3197 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003198 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003199 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003200 }
3201 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003202 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003203 evalarg_T evalarg;
3204
3205 CLEAR_FIELD(evalarg);
3206 evalarg.eval_flags = flags;
3207
Bram Moolenaar8c711452005-01-14 21:53:12 +00003208 /*
3209 * something[idx]
3210 *
3211 * Get the (first) variable from inside the [].
3212 */
3213 *arg = skipwhite(*arg + 1);
3214 if (**arg == ':')
3215 empty1 = TRUE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003216 else if (eval1(arg, &var1, &evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003217 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003218 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003219 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003220 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003221 clear_tv(&var1);
3222 return FAIL;
3223 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003224
3225 /*
3226 * Get the second variable from inside the [:].
3227 */
3228 if (**arg == ':')
3229 {
3230 range = TRUE;
3231 *arg = skipwhite(*arg + 1);
3232 if (**arg == ']')
3233 empty2 = TRUE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003234 else if (eval1(arg, &var2, &evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003235 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003236 if (!empty1)
3237 clear_tv(&var1);
3238 return FAIL;
3239 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003240 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003241 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003242 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003243 if (!empty1)
3244 clear_tv(&var1);
3245 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003246 return FAIL;
3247 }
3248 }
3249
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003250 // Check for the ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00003251 if (**arg != ']')
3252 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003253 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003254 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003255 clear_tv(&var1);
3256 if (range)
3257 clear_tv(&var2);
3258 return FAIL;
3259 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003260 *arg = skipwhite(*arg + 1); // skip the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003261 }
3262
3263 if (evaluate)
3264 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003265 n1 = 0;
3266 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003267 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003268 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003269 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003270 }
3271 if (range)
3272 {
3273 if (empty2)
3274 n2 = -1;
3275 else
3276 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003277 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003278 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003279 }
3280 }
3281
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003282 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003283 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003284 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003285 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003286 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003287 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003288 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003289 case VAR_FLOAT:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003290 case VAR_BOOL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003291 case VAR_SPECIAL:
3292 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003293 case VAR_CHANNEL:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003294 break; // not evaluating, skipping over subscript
Bram Moolenaara03f2332016-02-06 18:09:59 +01003295
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003296 case VAR_NUMBER:
3297 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003298 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003299 len = (long)STRLEN(s);
3300 if (range)
3301 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003302 // The resulting variable is a substring. If the indexes
3303 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003304 if (n1 < 0)
3305 {
3306 n1 = len + n1;
3307 if (n1 < 0)
3308 n1 = 0;
3309 }
3310 if (n2 < 0)
3311 n2 = len + n2;
3312 else if (n2 >= len)
3313 n2 = len;
3314 if (n1 >= len || n2 < 0 || n1 > n2)
3315 s = NULL;
3316 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02003317 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003318 }
3319 else
3320 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003321 // The resulting variable is a string of a single
3322 // character. If the index is too big or negative the
3323 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003324 if (n1 >= len || n1 < 0)
3325 s = NULL;
3326 else
3327 s = vim_strnsave(s + n1, 1);
3328 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003329 clear_tv(rettv);
3330 rettv->v_type = VAR_STRING;
3331 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003332 break;
3333
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003334 case VAR_BLOB:
3335 len = blob_len(rettv->vval.v_blob);
3336 if (range)
3337 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003338 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003339 // are out of range the result is empty.
3340 if (n1 < 0)
3341 {
3342 n1 = len + n1;
3343 if (n1 < 0)
3344 n1 = 0;
3345 }
3346 if (n2 < 0)
3347 n2 = len + n2;
3348 else if (n2 >= len)
3349 n2 = len - 1;
3350 if (n1 >= len || n2 < 0 || n1 > n2)
3351 {
3352 clear_tv(rettv);
3353 rettv->v_type = VAR_BLOB;
3354 rettv->vval.v_blob = NULL;
3355 }
3356 else
3357 {
3358 blob_T *blob = blob_alloc();
3359
3360 if (blob != NULL)
3361 {
3362 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
3363 {
3364 blob_free(blob);
3365 return FAIL;
3366 }
3367 blob->bv_ga.ga_len = n2 - n1 + 1;
3368 for (i = n1; i <= n2; i++)
3369 blob_set(blob, i - n1,
3370 blob_get(rettv->vval.v_blob, i));
3371
3372 clear_tv(rettv);
3373 rettv_blob_set(rettv, blob);
3374 }
3375 }
3376 }
3377 else
3378 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003379 // The resulting variable is a byte value.
3380 // If the index is too big or negative that is an error.
3381 if (n1 < 0)
3382 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003383 if (n1 < len && n1 >= 0)
3384 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003385 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003386
3387 clear_tv(rettv);
3388 rettv->v_type = VAR_NUMBER;
3389 rettv->vval.v_number = v;
3390 }
3391 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003392 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003393 }
3394 break;
3395
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003396 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003397 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003398 if (n1 < 0)
3399 n1 = len + n1;
3400 if (!empty1 && (n1 < 0 || n1 >= len))
3401 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003402 // For a range we allow invalid values and return an empty
3403 // list. A list index out of range is an error.
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003404 if (!range)
3405 {
3406 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003407 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003408 return FAIL;
3409 }
3410 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003411 }
3412 if (range)
3413 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003414 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003415
3416 if (n2 < 0)
3417 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003418 else if (n2 >= len)
3419 n2 = len - 1;
3420 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003421 n2 = -1;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003422 l = list_slice(rettv->vval.v_list, n1, n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003423 if (l == NULL)
3424 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003425 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003426 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003427 }
3428 else
3429 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003430 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003431 clear_tv(rettv);
3432 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003433 }
3434 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003435
3436 case VAR_DICT:
3437 if (range)
3438 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003439 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003440 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003441 if (len == -1)
3442 clear_tv(&var1);
3443 return FAIL;
3444 }
3445 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003446 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003447
3448 if (len == -1)
3449 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003450 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02003451 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003452 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003453 clear_tv(&var1);
3454 return FAIL;
3455 }
3456 }
3457
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003458 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003459
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003460 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003461 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003462 if (len == -1)
3463 clear_tv(&var1);
3464 if (item == NULL)
3465 return FAIL;
3466
3467 copy_tv(&item->di_tv, &var1);
3468 clear_tv(rettv);
3469 *rettv = var1;
3470 }
3471 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003472 }
3473 }
3474
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003475 return OK;
3476}
3477
3478/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02003479 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003480 */
3481 char_u *
3482partial_name(partial_T *pt)
3483{
3484 if (pt->pt_name != NULL)
3485 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02003486 if (pt->pt_func != NULL)
3487 return pt->pt_func->uf_name;
3488 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003489}
3490
Bram Moolenaarddecc252016-04-06 22:59:37 +02003491 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003492partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003493{
3494 int i;
3495
3496 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003497 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003498 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003499 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003500 if (pt->pt_name != NULL)
3501 {
3502 func_unref(pt->pt_name);
3503 vim_free(pt->pt_name);
3504 }
3505 else
3506 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003507
3508 if (pt->pt_funcstack != NULL)
3509 {
3510 // Decrease the reference count for the context of a closure. If down
3511 // to zero free it and clear the variables on the stack.
3512 if (--pt->pt_funcstack->fs_refcount == 0)
3513 {
3514 garray_T *gap = &pt->pt_funcstack->fs_ga;
3515 typval_T *stack = gap->ga_data;
3516
3517 for (i = 0; i < gap->ga_len; ++i)
3518 clear_tv(stack + i);
3519 ga_clear(gap);
3520 vim_free(pt->pt_funcstack);
3521 }
3522 pt->pt_funcstack = NULL;
3523 }
3524
Bram Moolenaarddecc252016-04-06 22:59:37 +02003525 vim_free(pt);
3526}
3527
3528/*
3529 * Unreference a closure: decrement the reference count and free it when it
3530 * becomes zero.
3531 */
3532 void
3533partial_unref(partial_T *pt)
3534{
3535 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003536 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003537}
3538
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003539/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003540 * Return the next (unique) copy ID.
3541 * Used for serializing nested structures.
3542 */
3543 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003544get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003545{
3546 current_copyID += COPYID_INC;
3547 return current_copyID;
3548}
3549
3550/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003551 * Garbage collection for lists and dictionaries.
3552 *
3553 * We use reference counts to be able to free most items right away when they
3554 * are no longer used. But for composite items it's possible that it becomes
3555 * unused while the reference count is > 0: When there is a recursive
3556 * reference. Example:
3557 * :let l = [1, 2, 3]
3558 * :let d = {9: l}
3559 * :let l[1] = d
3560 *
3561 * Since this is quite unusual we handle this with garbage collection: every
3562 * once in a while find out which lists and dicts are not referenced from any
3563 * variable.
3564 *
3565 * Here is a good reference text about garbage collection (refers to Python
3566 * but it applies to all reference-counting mechanisms):
3567 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00003568 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00003569
3570/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003571 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02003572 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003573 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00003574 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003575 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003576garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003577{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003578 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003579 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003580 buf_T *buf;
3581 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01003582 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003583 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003584
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003585 if (!testing)
3586 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003587 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003588 want_garbage_collect = FALSE;
3589 may_garbage_collect = FALSE;
3590 garbage_collect_at_exit = FALSE;
3591 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00003592
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01003593 // The execution stack can grow big, limit the size.
3594 if (exestack.ga_maxlen - exestack.ga_len > 500)
3595 {
3596 size_t new_len;
3597 char_u *pp;
3598 int n;
3599
3600 // Keep 150% of the current size, with a minimum of the growth size.
3601 n = exestack.ga_len / 2;
3602 if (n < exestack.ga_growsize)
3603 n = exestack.ga_growsize;
3604
3605 // Don't make it bigger though.
3606 if (exestack.ga_len + n < exestack.ga_maxlen)
3607 {
3608 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
3609 pp = vim_realloc(exestack.ga_data, new_len);
3610 if (pp == NULL)
3611 return FAIL;
3612 exestack.ga_maxlen = exestack.ga_len + n;
3613 exestack.ga_data = pp;
3614 }
3615 }
3616
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003617 // We advance by two because we add one for items referenced through
3618 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003619 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003620
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003621 /*
3622 * 1. Go through all accessible variables and mark all lists and dicts
3623 * with copyID.
3624 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003625
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003626 // Don't free variables in the previous_funccal list unless they are only
3627 // referenced through previous_funccal. This must be first, because if
3628 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003629 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003630
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003631 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003632 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003633
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003634 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003635 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003636 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
3637 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003638
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003639 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003640 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003641 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3642 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02003643 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003644 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
3645 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003646#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003647 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003648 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3649 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003650 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003651 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003652 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3653 NULL, NULL);
3654#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003655
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003656 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003657 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003658 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
3659 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003660 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003661 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003662
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003663 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003664 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003665
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003666 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003667 abort = abort || set_ref_in_functions(copyID);
3668
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003669 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003670 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003671
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003672 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003673 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00003674
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003675 // callbacks in buffers
3676 abort = abort || set_ref_in_buffers(copyID);
3677
Bram Moolenaar1dced572012-04-05 16:54:08 +02003678#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003679 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02003680#endif
3681
Bram Moolenaardb913952012-06-29 12:54:53 +02003682#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003683 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003684#endif
3685
3686#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003687 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003688#endif
3689
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003690#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02003691 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003692 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003693#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02003694#ifdef FEAT_NETBEANS_INTG
3695 abort = abort || set_ref_in_nb_channel(copyID);
3696#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003697
Bram Moolenaare3188e22016-05-31 21:13:04 +02003698#ifdef FEAT_TIMERS
3699 abort = abort || set_ref_in_timer(copyID);
3700#endif
3701
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02003702#ifdef FEAT_QUICKFIX
3703 abort = abort || set_ref_in_quickfix(copyID);
3704#endif
3705
Bram Moolenaara2c45a12017-07-27 22:14:59 +02003706#ifdef FEAT_TERMINAL
3707 abort = abort || set_ref_in_term(copyID);
3708#endif
3709
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003710#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003711 abort = abort || set_ref_in_popups(copyID);
3712#endif
3713
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003714 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003715 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003716 /*
3717 * 2. Free lists and dictionaries that are not referenced.
3718 */
3719 did_free = free_unref_items(copyID);
3720
3721 /*
3722 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003723 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003724 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003725 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003726 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003727 else if (p_verbose > 0)
3728 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003729 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003730 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003731
3732 return did_free;
3733}
3734
3735/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003736 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003737 */
3738 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003739free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003740{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003741 int did_free = FALSE;
3742
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003743 // Let all "free" functions know that we are here. This means no
3744 // dictionaries, lists, channels or jobs are to be freed, because we will
3745 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003746 in_free_unref_items = TRUE;
3747
3748 /*
3749 * PASS 1: free the contents of the items. We don't free the items
3750 * themselves yet, so that it is possible to decrement refcount counters
3751 */
3752
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003753 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02003754 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003755
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003756 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02003757 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003758
3759#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003760 // Go through the list of jobs and free items without the copyID. This
3761 // must happen before doing channels, because jobs refer to channels, but
3762 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003763 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
3764
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003765 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003766 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
3767#endif
3768
3769 /*
3770 * PASS 2: free the items themselves.
3771 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003772 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02003773 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01003774
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003775#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003776 // Go through the list of jobs and free items without the copyID. This
3777 // must happen before doing channels, because jobs refer to channels, but
3778 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003779 free_unused_jobs(copyID, COPYID_MASK);
3780
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003781 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003782 free_unused_channels(copyID, COPYID_MASK);
3783#endif
3784
3785 in_free_unref_items = FALSE;
3786
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003787 return did_free;
3788}
3789
3790/*
3791 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003792 * "list_stack" is used to add lists to be marked. Can be NULL.
3793 *
3794 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003795 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003796 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003797set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003798{
3799 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003800 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003801 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003802 hashtab_T *cur_ht;
3803 ht_stack_T *ht_stack = NULL;
3804 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003805
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003806 cur_ht = ht;
3807 for (;;)
3808 {
3809 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003810 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003811 // Mark each item in the hashtab. If the item contains a hashtab
3812 // it is added to ht_stack, if it contains a list it is added to
3813 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003814 todo = (int)cur_ht->ht_used;
3815 for (hi = cur_ht->ht_array; todo > 0; ++hi)
3816 if (!HASHITEM_EMPTY(hi))
3817 {
3818 --todo;
3819 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
3820 &ht_stack, list_stack);
3821 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003822 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003823
3824 if (ht_stack == NULL)
3825 break;
3826
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003827 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003828 cur_ht = ht_stack->ht;
3829 tempitem = ht_stack;
3830 ht_stack = ht_stack->prev;
3831 free(tempitem);
3832 }
3833
3834 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003835}
3836
3837/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003838 * Mark a dict and its items with "copyID".
3839 * Returns TRUE if setting references failed somehow.
3840 */
3841 int
3842set_ref_in_dict(dict_T *d, int copyID)
3843{
3844 if (d != NULL && d->dv_copyID != copyID)
3845 {
3846 d->dv_copyID = copyID;
3847 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
3848 }
3849 return FALSE;
3850}
3851
3852/*
3853 * Mark a list and its items with "copyID".
3854 * Returns TRUE if setting references failed somehow.
3855 */
3856 int
3857set_ref_in_list(list_T *ll, int copyID)
3858{
3859 if (ll != NULL && ll->lv_copyID != copyID)
3860 {
3861 ll->lv_copyID = copyID;
3862 return set_ref_in_list_items(ll, copyID, NULL);
3863 }
3864 return FALSE;
3865}
3866
3867/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003868 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003869 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
3870 *
3871 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003872 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003873 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003874set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003875{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003876 listitem_T *li;
3877 int abort = FALSE;
3878 list_T *cur_l;
3879 list_stack_T *list_stack = NULL;
3880 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003881
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003882 cur_l = l;
3883 for (;;)
3884 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003885 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003886 // Mark each item in the list. If the item contains a hashtab
3887 // it is added to ht_stack, if it contains a list it is added to
3888 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003889 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
3890 abort = abort || set_ref_in_item(&li->li_tv, copyID,
3891 ht_stack, &list_stack);
3892 if (list_stack == NULL)
3893 break;
3894
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003895 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003896 cur_l = list_stack->list;
3897 tempitem = list_stack;
3898 list_stack = list_stack->prev;
3899 free(tempitem);
3900 }
3901
3902 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003903}
3904
3905/*
3906 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003907 * "list_stack" is used to add lists to be marked. Can be NULL.
3908 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
3909 *
3910 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003911 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003912 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003913set_ref_in_item(
3914 typval_T *tv,
3915 int copyID,
3916 ht_stack_T **ht_stack,
3917 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003918{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003919 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00003920
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003921 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003922 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003923 dict_T *dd = tv->vval.v_dict;
3924
Bram Moolenaara03f2332016-02-06 18:09:59 +01003925 if (dd != NULL && dd->dv_copyID != copyID)
3926 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003927 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01003928 dd->dv_copyID = copyID;
3929 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003930 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003931 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
3932 }
3933 else
3934 {
3935 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
3936 if (newitem == NULL)
3937 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003938 else
3939 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003940 newitem->ht = &dd->dv_hashtab;
3941 newitem->prev = *ht_stack;
3942 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003943 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00003944 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01003945 }
3946 }
3947 else if (tv->v_type == VAR_LIST)
3948 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003949 list_T *ll = tv->vval.v_list;
3950
Bram Moolenaara03f2332016-02-06 18:09:59 +01003951 if (ll != NULL && ll->lv_copyID != copyID)
3952 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003953 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01003954 ll->lv_copyID = copyID;
3955 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003956 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003957 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01003958 }
3959 else
3960 {
3961 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003962 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003963 if (newitem == NULL)
3964 abort = TRUE;
3965 else
3966 {
3967 newitem->list = ll;
3968 newitem->prev = *list_stack;
3969 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003970 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00003971 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01003972 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00003973 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003974 else if (tv->v_type == VAR_FUNC)
3975 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003976 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003977 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003978 else if (tv->v_type == VAR_PARTIAL)
3979 {
3980 partial_T *pt = tv->vval.v_partial;
3981 int i;
3982
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003983 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003984 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003985 // Didn't see this partial yet.
3986 pt->pt_copyID = copyID;
3987
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003988 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003989
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003990 if (pt->pt_dict != NULL)
3991 {
3992 typval_T dtv;
3993
3994 dtv.v_type = VAR_DICT;
3995 dtv.vval.v_dict = pt->pt_dict;
3996 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
3997 }
3998
3999 for (i = 0; i < pt->pt_argc; ++i)
4000 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4001 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004002 if (pt->pt_funcstack != NULL)
4003 {
4004 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4005
4006 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4007 abort = abort || set_ref_in_item(stack + i, copyID,
4008 ht_stack, list_stack);
4009 }
4010
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004011 }
4012 }
4013#ifdef FEAT_JOB_CHANNEL
4014 else if (tv->v_type == VAR_JOB)
4015 {
4016 job_T *job = tv->vval.v_job;
4017 typval_T dtv;
4018
4019 if (job != NULL && job->jv_copyID != copyID)
4020 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004021 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004022 if (job->jv_channel != NULL)
4023 {
4024 dtv.v_type = VAR_CHANNEL;
4025 dtv.vval.v_channel = job->jv_channel;
4026 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4027 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004028 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004029 {
4030 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004031 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004032 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4033 }
4034 }
4035 }
4036 else if (tv->v_type == VAR_CHANNEL)
4037 {
4038 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004039 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004040 typval_T dtv;
4041 jsonq_T *jq;
4042 cbq_T *cq;
4043
4044 if (ch != NULL && ch->ch_copyID != copyID)
4045 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004046 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004047 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004048 {
4049 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4050 jq = jq->jq_next)
4051 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4052 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4053 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004054 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004055 {
4056 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004057 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004058 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4059 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004060 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004061 {
4062 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004063 dtv.vval.v_partial =
4064 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004065 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4066 }
4067 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004068 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004069 {
4070 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004071 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004072 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4073 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004074 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004075 {
4076 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004077 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004078 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4079 }
4080 }
4081 }
4082#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004083 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004084}
4085
Bram Moolenaar8c711452005-01-14 21:53:12 +00004086/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004087 * Return a string with the string representation of a variable.
4088 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004089 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004090 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004091 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4092 * stings as "string()", otherwise does not put quotes around strings, as
4093 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004094 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4095 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004096 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004097 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004098 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004099echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004100 typval_T *tv,
4101 char_u **tofree,
4102 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004103 int copyID,
4104 int echo_style,
4105 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004106 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004107{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004108 static int recurse = 0;
4109 char_u *r = NULL;
4110
Bram Moolenaar33570922005-01-25 22:26:29 +00004111 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004112 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004113 if (!did_echo_string_emsg)
4114 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004115 // Only give this message once for a recursive call to avoid
4116 // flooding the user with errors. And stop iterating over lists
4117 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004118 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004119 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004120 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004121 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004122 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004123 }
4124 ++recurse;
4125
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004126 switch (tv->v_type)
4127 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004128 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004129 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004130 {
4131 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004132 r = tv->vval.v_string;
4133 if (r == NULL)
4134 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004135 }
4136 else
4137 {
4138 *tofree = string_quote(tv->vval.v_string, FALSE);
4139 r = *tofree;
4140 }
4141 break;
4142
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004143 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004144 if (echo_style)
4145 {
4146 *tofree = NULL;
4147 r = tv->vval.v_string;
4148 }
4149 else
4150 {
4151 *tofree = string_quote(tv->vval.v_string, TRUE);
4152 r = *tofree;
4153 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004154 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004155
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004156 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004157 {
4158 partial_T *pt = tv->vval.v_partial;
4159 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004160 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004161 garray_T ga;
4162 int i;
4163 char_u *tf;
4164
4165 ga_init2(&ga, 1, 100);
4166 ga_concat(&ga, (char_u *)"function(");
4167 if (fname != NULL)
4168 {
4169 ga_concat(&ga, fname);
4170 vim_free(fname);
4171 }
4172 if (pt != NULL && pt->pt_argc > 0)
4173 {
4174 ga_concat(&ga, (char_u *)", [");
4175 for (i = 0; i < pt->pt_argc; ++i)
4176 {
4177 if (i > 0)
4178 ga_concat(&ga, (char_u *)", ");
4179 ga_concat(&ga,
4180 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4181 vim_free(tf);
4182 }
4183 ga_concat(&ga, (char_u *)"]");
4184 }
4185 if (pt != NULL && pt->pt_dict != NULL)
4186 {
4187 typval_T dtv;
4188
4189 ga_concat(&ga, (char_u *)", ");
4190 dtv.v_type = VAR_DICT;
4191 dtv.vval.v_dict = pt->pt_dict;
4192 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4193 vim_free(tf);
4194 }
4195 ga_concat(&ga, (char_u *)")");
4196
4197 *tofree = ga.ga_data;
4198 r = *tofree;
4199 break;
4200 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004201
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004202 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004203 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004204 break;
4205
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004206 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004207 if (tv->vval.v_list == NULL)
4208 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004209 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004210 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004211 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004212 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004213 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4214 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004215 {
4216 *tofree = NULL;
4217 r = (char_u *)"[...]";
4218 }
4219 else
4220 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004221 int old_copyID = tv->vval.v_list->lv_copyID;
4222
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004223 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004224 *tofree = list2string(tv, copyID, restore_copyID);
4225 if (restore_copyID)
4226 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004227 r = *tofree;
4228 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004229 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004230
Bram Moolenaar8c711452005-01-14 21:53:12 +00004231 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004232 if (tv->vval.v_dict == NULL)
4233 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004234 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004235 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004236 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004237 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004238 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4239 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004240 {
4241 *tofree = NULL;
4242 r = (char_u *)"{...}";
4243 }
4244 else
4245 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004246 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004247
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004248 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004249 *tofree = dict2string(tv, copyID, restore_copyID);
4250 if (restore_copyID)
4251 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004252 r = *tofree;
4253 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004254 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004255
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004256 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004257 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004258 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004259 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004260 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004261 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004262 break;
4263
Bram Moolenaar835dc632016-02-07 14:27:38 +01004264 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004265 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004266 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004267 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004268 if (composite_val)
4269 {
4270 *tofree = string_quote(r, FALSE);
4271 r = *tofree;
4272 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004273 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004274
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004275 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004276#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004277 *tofree = NULL;
4278 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4279 r = numbuf;
4280 break;
4281#endif
4282
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004283 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004284 case VAR_SPECIAL:
4285 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004286 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004287 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004288 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004289
Bram Moolenaar8502c702014-06-17 12:51:16 +02004290 if (--recurse == 0)
4291 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004292 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004293}
4294
4295/*
4296 * Return a string with the string representation of a variable.
4297 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4298 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004299 * Does not put quotes around strings, as ":echo" displays values.
4300 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4301 * May return NULL.
4302 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004303 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004304echo_string(
4305 typval_T *tv,
4306 char_u **tofree,
4307 char_u *numbuf,
4308 int copyID)
4309{
4310 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4311}
4312
4313/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004314 * Return string "str" in ' quotes, doubling ' characters.
4315 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004316 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004317 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004318 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004319string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004320{
Bram Moolenaar33570922005-01-25 22:26:29 +00004321 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004322 char_u *p, *r, *s;
4323
Bram Moolenaar33570922005-01-25 22:26:29 +00004324 len = (function ? 13 : 3);
4325 if (str != NULL)
4326 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004327 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004328 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004329 if (*p == '\'')
4330 ++len;
4331 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004332 s = r = alloc(len);
4333 if (r != NULL)
4334 {
4335 if (function)
4336 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004337 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004338 r += 10;
4339 }
4340 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004341 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004342 if (str != NULL)
4343 for (p = str; *p != NUL; )
4344 {
4345 if (*p == '\'')
4346 *r++ = '\'';
4347 MB_COPY_CHAR(p, r);
4348 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004349 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004350 if (function)
4351 *r++ = ')';
4352 *r++ = NUL;
4353 }
4354 return s;
4355}
4356
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004357#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004358/*
4359 * Convert the string "text" to a floating point number.
4360 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4361 * this always uses a decimal point.
4362 * Returns the length of the text that was consumed.
4363 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004364 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004365string2float(
4366 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004367 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004368{
4369 char *s = (char *)text;
4370 float_T f;
4371
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004372 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004373 if (STRNICMP(text, "inf", 3) == 0)
4374 {
4375 *value = INFINITY;
4376 return 3;
4377 }
4378 if (STRNICMP(text, "-inf", 3) == 0)
4379 {
4380 *value = -INFINITY;
4381 return 4;
4382 }
4383 if (STRNICMP(text, "nan", 3) == 0)
4384 {
4385 *value = NAN;
4386 return 3;
4387 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004388 f = strtod(s, &s);
4389 *value = f;
4390 return (int)((char_u *)s - text);
4391}
4392#endif
4393
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004394/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004396 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004398 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004399var2fpos(
4400 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004401 int dollar_lnum, // TRUE when $ is last line
4402 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004404 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004406 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004408 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004409 if (varp->v_type == VAR_LIST)
4410 {
4411 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004412 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004413 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004414 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004415
4416 l = varp->vval.v_list;
4417 if (l == NULL)
4418 return NULL;
4419
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004420 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004421 pos.lnum = list_find_nr(l, 0L, &error);
4422 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004423 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004424
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004425 // Get the column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004426 pos.col = list_find_nr(l, 1L, &error);
4427 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004428 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004429 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004430
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004431 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004432 li = list_find(l, 1L);
4433 if (li != NULL && li->li_tv.v_type == VAR_STRING
4434 && li->li_tv.vval.v_string != NULL
4435 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
4436 pos.col = len + 1;
4437
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004438 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004439 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004440 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004441 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004442
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004443 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004444 pos.coladd = list_find_nr(l, 2L, &error);
4445 if (error)
4446 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004447
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004448 return &pos;
4449 }
4450
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004451 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004452 if (name == NULL)
4453 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004454 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004456 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004457 {
4458 if (VIsual_active)
4459 return &VIsual;
4460 return &curwin->w_cursor;
4461 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004462 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004464 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4466 return NULL;
4467 return pp;
4468 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004469
Bram Moolenaara5525202006-03-02 22:52:09 +00004470 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004471
Bram Moolenaar477933c2007-07-17 14:32:23 +00004472 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004473 {
4474 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004475 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004476 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004477 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004478 // In silent Ex mode topline is zero, but that's not a valid line
4479 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004480 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004481 return &pos;
4482 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004483 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004484 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004485 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004486 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004487 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004488 return &pos;
4489 }
4490 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004491 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004493 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 {
4495 pos.lnum = curbuf->b_ml.ml_line_count;
4496 pos.col = 0;
4497 }
4498 else
4499 {
4500 pos.lnum = curwin->w_cursor.lnum;
4501 pos.col = (colnr_T)STRLEN(ml_get_curline());
4502 }
4503 return &pos;
4504 }
4505 return NULL;
4506}
4507
4508/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004509 * Convert list in "arg" into a position and optional file number.
4510 * When "fnump" is NULL there is no file number, only 3 items.
4511 * Note that the column is passed on as-is, the caller may want to decrement
4512 * it to use 1 for the first column.
4513 * Return FAIL when conversion is not possible, doesn't check the position for
4514 * validity.
4515 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004516 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004517list2fpos(
4518 typval_T *arg,
4519 pos_T *posp,
4520 int *fnump,
4521 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004522{
4523 list_T *l = arg->vval.v_list;
4524 long i = 0;
4525 long n;
4526
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004527 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
4528 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00004529 if (arg->v_type != VAR_LIST
4530 || l == NULL
4531 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02004532 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004533 return FAIL;
4534
4535 if (fnump != NULL)
4536 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004537 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004538 if (n < 0)
4539 return FAIL;
4540 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004541 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004542 *fnump = n;
4543 }
4544
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004545 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004546 if (n < 0)
4547 return FAIL;
4548 posp->lnum = n;
4549
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004550 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004551 if (n < 0)
4552 return FAIL;
4553 posp->col = n;
4554
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004555 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004556 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00004557 posp->coladd = 0;
4558 else
4559 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004560
Bram Moolenaar493c1782014-05-28 14:34:46 +02004561 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004562 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02004563
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004564 return OK;
4565}
4566
4567/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 * Get the length of an environment variable name.
4569 * Advance "arg" to the first character after the name.
4570 * Return 0 for error.
4571 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004572 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004573get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574{
4575 char_u *p;
4576 int len;
4577
4578 for (p = *arg; vim_isIDc(*p); ++p)
4579 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004580 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 return 0;
4582
4583 len = (int)(p - *arg);
4584 *arg = p;
4585 return len;
4586}
4587
4588/*
4589 * Get the length of the name of a function or internal variable.
4590 * "arg" is advanced to the first non-white character after the name.
4591 * Return 0 if something is wrong.
4592 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004593 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004594get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595{
4596 char_u *p;
4597 int len;
4598
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004599 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004601 {
4602 if (*p == ':')
4603 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004604 // "s:" is start of "s:var", but "n:" is not and can be used in
4605 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004606 len = (int)(p - *arg);
4607 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
4608 || len > 1)
4609 break;
4610 }
4611 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004612 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 return 0;
4614
4615 len = (int)(p - *arg);
4616 *arg = skipwhite(p);
4617
4618 return len;
4619}
4620
4621/*
Bram Moolenaara7043832005-01-21 11:56:39 +00004622 * Get the length of the name of a variable or function.
4623 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004625 * Return -1 if curly braces expansion failed.
4626 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 * If the name contains 'magic' {}'s, expand them and return the
4628 * expanded name in an allocated string via 'alias' - caller must free.
4629 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004630 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004631get_name_len(
4632 char_u **arg,
4633 char_u **alias,
4634 int evaluate,
4635 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636{
4637 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 char_u *p;
4639 char_u *expr_start;
4640 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004642 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643
4644 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
4645 && (*arg)[2] == (int)KE_SNR)
4646 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004647 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 *arg += 3;
4649 return get_id_len(arg) + 3;
4650 }
4651 len = eval_fname_script(*arg);
4652 if (len > 0)
4653 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004654 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 *arg += len;
4656 }
4657
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004659 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004661 p = find_name_end(*arg, &expr_start, &expr_end,
4662 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 if (expr_start != NULL)
4664 {
4665 char_u *temp_string;
4666
4667 if (!evaluate)
4668 {
4669 len += (int)(p - *arg);
4670 *arg = skipwhite(p);
4671 return len;
4672 }
4673
4674 /*
4675 * Include any <SID> etc in the expanded string:
4676 * Thus the -len here.
4677 */
4678 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
4679 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004680 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 *alias = temp_string;
4682 *arg = skipwhite(p);
4683 return (int)STRLEN(temp_string);
4684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685
4686 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01004687 // Only give an error when there is something, otherwise it will be
4688 // reported at a higher level.
4689 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004690 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691
4692 return len;
4693}
4694
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004695/*
4696 * Find the end of a variable or function name, taking care of magic braces.
4697 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
4698 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004699 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004700 * Return a pointer to just after the name. Equal to "arg" if there is no
4701 * valid name.
4702 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004703 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004704find_name_end(
4705 char_u *arg,
4706 char_u **expr_start,
4707 char_u **expr_end,
4708 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004710 int mb_nest = 0;
4711 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004713 int len;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004714 int vim9script = current_sctx.sc_version == SCRIPT_VERSION_VIM9;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004716 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004718 *expr_start = NULL;
4719 *expr_end = NULL;
4720 }
4721
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004722 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004723 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
4724 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004725 return arg;
4726
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004727 for (p = arg; *p != NUL
4728 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004729 || (*p == '{' && !vim9script)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004730 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004731 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004732 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004733 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00004734 if (*p == '\'')
4735 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004736 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004737 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00004738 ;
4739 if (*p == NUL)
4740 break;
4741 }
4742 else if (*p == '"')
4743 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004744 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004745 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00004746 if (*p == '\\' && p[1] != NUL)
4747 ++p;
4748 if (*p == NUL)
4749 break;
4750 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004751 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
4752 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004753 // "s:" is start of "s:var", but "n:" is not and can be used in
4754 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004755 len = (int)(p - arg);
4756 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01004757 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004758 break;
4759 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00004760
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004761 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004763 if (*p == '[')
4764 ++br_nest;
4765 else if (*p == ']')
4766 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00004768
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004769 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004771 if (*p == '{')
4772 {
4773 mb_nest++;
4774 if (expr_start != NULL && *expr_start == NULL)
4775 *expr_start = p;
4776 }
4777 else if (*p == '}')
4778 {
4779 mb_nest--;
4780 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
4781 *expr_end = p;
4782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 }
4785
4786 return p;
4787}
4788
4789/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004790 * Expands out the 'magic' {}'s in a variable/function name.
4791 * Note that this can call itself recursively, to deal with
4792 * constructs like foo{bar}{baz}{bam}
4793 * The four pointer arguments point to "foo{expre}ss{ion}bar"
4794 * "in_start" ^
4795 * "expr_start" ^
4796 * "expr_end" ^
4797 * "in_end" ^
4798 *
4799 * Returns a new allocated string, which the caller must free.
4800 * Returns NULL for failure.
4801 */
4802 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004803make_expanded_name(
4804 char_u *in_start,
4805 char_u *expr_start,
4806 char_u *expr_end,
4807 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004808{
4809 char_u c1;
4810 char_u *retval = NULL;
4811 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004812
4813 if (expr_end == NULL || in_end == NULL)
4814 return NULL;
4815 *expr_start = NUL;
4816 *expr_end = NUL;
4817 c1 = *in_end;
4818 *in_end = NUL;
4819
Bram Moolenaarb171fb12020-06-24 20:34:03 +02004820 temp_result = eval_to_string(expr_start + 1, FALSE);
4821 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004822 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02004823 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
4824 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004825 if (retval != NULL)
4826 {
4827 STRCPY(retval, in_start);
4828 STRCAT(retval, temp_result);
4829 STRCAT(retval, expr_end + 1);
4830 }
4831 }
4832 vim_free(temp_result);
4833
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004834 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004835 *expr_start = '{';
4836 *expr_end = '}';
4837
4838 if (retval != NULL)
4839 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004840 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004841 if (expr_start != NULL)
4842 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004843 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004844 temp_result = make_expanded_name(retval, expr_start,
4845 expr_end, temp_result);
4846 vim_free(retval);
4847 retval = temp_result;
4848 }
4849 }
4850
4851 return retval;
4852}
4853
4854/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00004856 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004858 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004859eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004861 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
4862}
4863
4864/*
4865 * Return TRUE if character "c" can be used as the first character in a
4866 * variable or function name (excluding '{' and '}').
4867 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004868 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004869eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004870{
4871 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872}
4873
4874/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004875 * Handle:
4876 * - expr[expr], expr[expr:expr] subscript
4877 * - ".name" lookup
4878 * - function call with Funcref variable: func(expr)
4879 * - method call: var->method()
4880 *
4881 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004882 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004883 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004884handle_subscript(
4885 char_u **arg,
4886 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004887 int flags, // do more than finding the end
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004888 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004889{
Bram Moolenaar32e35112020-05-14 22:41:15 +02004890 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004891 int ret = OK;
4892 dict_T *selfdict = NULL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004893
Bram Moolenaar61343f02019-07-20 21:11:13 +02004894 // "." is ".name" lookup when we found a dict or when evaluating and
4895 // scriptversion is at least 2, where string concatenation is "..".
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004896 while (ret == OK
Bram Moolenaarac92e252019-08-03 21:58:38 +02004897 && (((**arg == '['
4898 || (**arg == '.' && (rettv->v_type == VAR_DICT
Bram Moolenaar61343f02019-07-20 21:11:13 +02004899 || (!evaluate
4900 && (*arg)[1] != '.'
4901 && current_sctx.sc_version >= 2)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02004902 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004903 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02004904 && !VIM_ISWHITE(*(*arg - 1)))
4905 || (**arg == '-' && (*arg)[1] == '>')))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004906 {
4907 if (**arg == '(')
4908 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004909 ret = call_func_rettv(arg, rettv, evaluate, selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01004910
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004911 // Stop the expression evaluation when immediately aborting on
4912 // error, or when an interrupt occurred or an exception was thrown
4913 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004914 if (aborting())
4915 {
4916 if (ret == OK)
4917 clear_tv(rettv);
4918 ret = FAIL;
4919 }
4920 dict_unref(selfdict);
4921 selfdict = NULL;
4922 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004923 else if (**arg == '-')
4924 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004925 if (ret == OK)
4926 {
4927 if ((*arg)[2] == '{')
4928 // expr->{lambda}()
4929 ret = eval_lambda(arg, rettv, evaluate, verbose);
4930 else
4931 // expr->name()
4932 ret = eval_method(arg, rettv, evaluate, verbose);
4933 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004934 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004935 else // **arg == '[' || **arg == '.'
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004936 {
4937 dict_unref(selfdict);
4938 if (rettv->v_type == VAR_DICT)
4939 {
4940 selfdict = rettv->vval.v_dict;
4941 if (selfdict != NULL)
4942 ++selfdict->dv_refcount;
4943 }
4944 else
4945 selfdict = NULL;
Bram Moolenaar32e35112020-05-14 22:41:15 +02004946 if (eval_index(arg, rettv, flags, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004947 {
4948 clear_tv(rettv);
4949 ret = FAIL;
4950 }
4951 }
4952 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01004953
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004954 // Turn "dict.Func" into a partial for "Func" bound to "dict".
4955 // Don't do this when "Func" is already a partial that was bound
4956 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02004957 if (selfdict != NULL
4958 && (rettv->v_type == VAR_FUNC
4959 || (rettv->v_type == VAR_PARTIAL
4960 && (rettv->vval.v_partial->pt_auto
4961 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004962 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01004963
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004964 dict_unref(selfdict);
4965 return ret;
4966}
4967
4968/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004969 * Make a copy of an item.
4970 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004971 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
4972 * reference to an already copied list/dict can be used.
4973 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00004974 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004975 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004976item_copy(
4977 typval_T *from,
4978 typval_T *to,
4979 int deep,
4980 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004981{
4982 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004983 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004984
Bram Moolenaar33570922005-01-25 22:26:29 +00004985 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004986 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004987 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004988 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004989 }
4990 ++recurse;
4991
4992 switch (from->v_type)
4993 {
4994 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004995 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004996 case VAR_STRING:
4997 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004998 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004999 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005000 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005001 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005002 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005003 copy_tv(from, to);
5004 break;
5005 case VAR_LIST:
5006 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005007 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005008 if (from->vval.v_list == NULL)
5009 to->vval.v_list = NULL;
5010 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5011 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005012 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005013 to->vval.v_list = from->vval.v_list->lv_copylist;
5014 ++to->vval.v_list->lv_refcount;
5015 }
5016 else
5017 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5018 if (to->vval.v_list == NULL)
5019 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005020 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005021 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005022 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005023 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005024 case VAR_DICT:
5025 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005026 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005027 if (from->vval.v_dict == NULL)
5028 to->vval.v_dict = NULL;
5029 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5030 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005031 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005032 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5033 ++to->vval.v_dict->dv_refcount;
5034 }
5035 else
5036 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5037 if (to->vval.v_dict == NULL)
5038 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005039 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005040 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005041 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005042 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005043 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005044 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005045 }
5046 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005047 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005048}
5049
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005050 void
5051echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5052{
5053 char_u *tofree;
5054 char_u numbuf[NUMBUFLEN];
5055 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5056
5057 if (*atstart)
5058 {
5059 *atstart = FALSE;
5060 // Call msg_start() after eval1(), evaluating the expression
5061 // may cause a message to appear.
5062 if (with_space)
5063 {
5064 // Mark the saved text as finishing the line, so that what
5065 // follows is displayed on a new line when scrolling back
5066 // at the more prompt.
5067 msg_sb_eol();
5068 msg_start();
5069 }
5070 }
5071 else if (with_space)
5072 msg_puts_attr(" ", echo_attr);
5073
5074 if (p != NULL)
5075 for ( ; *p != NUL && !got_int; ++p)
5076 {
5077 if (*p == '\n' || *p == '\r' || *p == TAB)
5078 {
5079 if (*p != TAB && *needclr)
5080 {
5081 // remove any text still there from the command
5082 msg_clr_eos();
5083 *needclr = FALSE;
5084 }
5085 msg_putchar_attr(*p, echo_attr);
5086 }
5087 else
5088 {
5089 if (has_mbyte)
5090 {
5091 int i = (*mb_ptr2len)(p);
5092
5093 (void)msg_outtrans_len_attr(p, i, echo_attr);
5094 p += i - 1;
5095 }
5096 else
5097 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5098 }
5099 }
5100 vim_free(tofree);
5101}
5102
Bram Moolenaare9a41262005-01-15 22:18:47 +00005103/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 * ":echo expr1 ..." print each argument separated with a space, add a
5105 * newline at the end.
5106 * ":echon expr1 ..." print each argument plain.
5107 */
5108 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005109ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110{
5111 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005112 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 char_u *p;
5114 int needclr = TRUE;
5115 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01005116 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005117 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005118 evalarg_T evalarg;
5119
5120 CLEAR_FIELD(evalarg);
5121 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122
5123 if (eap->skip)
5124 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005125 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005127 // If eval1() causes an error message the text from the command may
5128 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005129 need_clr_eos = needclr;
5130
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005132 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 {
5134 /*
5135 * Report the invalid expression unless the expression evaluation
5136 * has been cancelled due to an aborting error, an interrupt, or an
5137 * exception.
5138 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005139 if (!aborting() && did_emsg == did_emsg_before
5140 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005141 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005142 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 break;
5144 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005145 need_clr_eos = FALSE;
5146
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005148 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005149
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005150 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 arg = skipwhite(arg);
5152 }
5153 eap->nextcmd = check_nextcmd(arg);
5154
5155 if (eap->skip)
5156 --emsg_skip;
5157 else
5158 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005159 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 if (needclr)
5161 msg_clr_eos();
5162 if (eap->cmdidx == CMD_echo)
5163 msg_end();
5164 }
5165}
5166
5167/*
5168 * ":echohl {name}".
5169 */
5170 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005171ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005173 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174}
5175
5176/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005177 * Returns the :echo attribute
5178 */
5179 int
5180get_echo_attr(void)
5181{
5182 return echo_attr;
5183}
5184
5185/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 * ":execute expr1 ..." execute the result of an expression.
5187 * ":echomsg expr1 ..." Print a message
5188 * ":echoerr expr1 ..." Print an error
5189 * Each gets spaces around each argument and a newline at the end for
5190 * echo commands
5191 */
5192 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005193ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194{
5195 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005196 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 int ret = OK;
5198 char_u *p;
5199 garray_T ga;
5200 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201
5202 ga_init2(&ga, 1, 80);
5203
5204 if (eap->skip)
5205 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02005206 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01005208 ret = eval1_emsg(&arg, &rettv, !eap->skip);
5209 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211
5212 if (!eap->skip)
5213 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005214 char_u buf[NUMBUFLEN];
5215
5216 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01005217 {
5218 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
5219 {
5220 emsg(_(e_inval_string));
5221 p = NULL;
5222 }
5223 else
5224 p = tv_get_string_buf(&rettv, buf);
5225 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005226 else
5227 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005228 if (p == NULL)
5229 {
5230 clear_tv(&rettv);
5231 ret = FAIL;
5232 break;
5233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 len = (int)STRLEN(p);
5235 if (ga_grow(&ga, len + 2) == FAIL)
5236 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005237 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 ret = FAIL;
5239 break;
5240 }
5241 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 ga.ga_len += len;
5245 }
5246
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005247 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 arg = skipwhite(arg);
5249 }
5250
5251 if (ret != FAIL && ga.ga_data != NULL)
5252 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005253 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
5254 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005255 // Mark the already saved text as finishing the line, so that what
5256 // follows is displayed on a new line when scrolling back at the
5257 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005258 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005259 }
5260
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005262 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005263 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005264 out_flush();
5265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 else if (eap->cmdidx == CMD_echoerr)
5267 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005268 int save_did_emsg = did_emsg;
5269
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005270 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005271 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 if (!force_abort)
5273 did_emsg = save_did_emsg;
5274 }
5275 else if (eap->cmdidx == CMD_execute)
5276 do_cmdline((char_u *)ga.ga_data,
5277 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
5278 }
5279
5280 ga_clear(&ga);
5281
5282 if (eap->skip)
5283 --emsg_skip;
5284
5285 eap->nextcmd = check_nextcmd(arg);
5286}
5287
5288/*
5289 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
5290 * "arg" points to the "&" or '+' when called, to "option" when returning.
5291 * Returns NULL when no option name found. Otherwise pointer to the char
5292 * after the option name.
5293 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005294 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005295find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296{
5297 char_u *p = *arg;
5298
5299 ++p;
5300 if (*p == 'g' && p[1] == ':')
5301 {
5302 *opt_flags = OPT_GLOBAL;
5303 p += 2;
5304 }
5305 else if (*p == 'l' && p[1] == ':')
5306 {
5307 *opt_flags = OPT_LOCAL;
5308 p += 2;
5309 }
5310 else
5311 *opt_flags = 0;
5312
5313 if (!ASCII_ISALPHA(*p))
5314 return NULL;
5315 *arg = p;
5316
5317 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005318 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 else
5320 while (ASCII_ISALPHA(*p))
5321 ++p;
5322 return p;
5323}
5324
5325/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00005326 * Display script name where an item was last set.
5327 * Should only be invoked when 'verbose' is non-zero.
5328 */
5329 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005330last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005331{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005332 char_u *p;
5333
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005334 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005335 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005336 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005337 if (p != NULL)
5338 {
5339 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01005340 msg_puts(_("\n\tLast set from "));
5341 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005342 if (script_ctx.sc_lnum > 0)
5343 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01005344 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005345 msg_outnum((long)script_ctx.sc_lnum);
5346 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005347 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005348 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005349 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00005350 }
5351}
5352
Bram Moolenaarb005cd82019-09-04 15:54:55 +02005353#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354
5355/*
5356 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005357 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 * "flags" can be "g" to do a global substitute.
5359 * Returns an allocated string, NULL for error.
5360 */
5361 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005362do_string_sub(
5363 char_u *str,
5364 char_u *pat,
5365 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005366 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01005367 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368{
5369 int sublen;
5370 regmatch_T regmatch;
5371 int i;
5372 int do_all;
5373 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005374 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 garray_T ga;
5376 char_u *ret;
5377 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005378 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005380 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005382 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383
5384 ga_init2(&ga, 1, 200);
5385
5386 do_all = (flags[0] == 'g');
5387
5388 regmatch.rm_ic = p_ic;
5389 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
5390 if (regmatch.regprog != NULL)
5391 {
5392 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005393 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
5395 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005396 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01005397 if (regmatch.startp[0] == regmatch.endp[0])
5398 {
5399 if (zero_width == regmatch.startp[0])
5400 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005401 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02005402 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02005403 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
5404 (size_t)i);
5405 ga.ga_len += i;
5406 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005407 continue;
5408 }
5409 zero_width = regmatch.startp[0];
5410 }
5411
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 /*
5413 * Get some space for a temporary buffer to do the substitution
5414 * into. It will contain:
5415 * - The text up to where the match is.
5416 * - The substituted text.
5417 * - The text after the match.
5418 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005419 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01005420 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
5422 {
5423 ga_clear(&ga);
5424 break;
5425 }
5426
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005427 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 i = (int)(regmatch.startp[0] - tail);
5429 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005430 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005431 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 + ga.ga_len + i, TRUE, TRUE, FALSE);
5433 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02005434 tail = regmatch.endp[0];
5435 if (*tail == NUL)
5436 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 if (!do_all)
5438 break;
5439 }
5440
5441 if (ga.ga_data != NULL)
5442 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
5443
Bram Moolenaar473de612013-06-08 18:19:48 +02005444 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 }
5446
5447 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
5448 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005449 if (p_cpo == empty_option)
5450 p_cpo = save_cpo;
5451 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005452 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005453 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454
5455 return ret;
5456}