blob: 86d81fa09e44e382f19d15ae895c918bdc9d77d2 [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 Moolenaare40fbc22020-06-27 18:06:45 +0200328 if (eval0(arg, &tv, eap, skip ? NULL : &EVALARG_EVALUATE) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 retval = NULL;
330 else
331 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100332 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000333 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 }
335 if (skip)
336 --emsg_skip;
337
338 return retval;
339}
340
341/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000342 * Skip over an expression at "*pp".
343 * Return FAIL for an error, OK otherwise.
344 */
345 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100346skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000347{
Bram Moolenaar33570922005-01-25 22:26:29 +0000348 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000349
350 *pp = skipwhite(*pp);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200351 return eval1(pp, &rettv, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000352}
353
354/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200355 * Skip over an expression at "*pp".
356 * If in Vim9 script and line breaks are encountered, the lines are
357 * concatenated. "evalarg->eval_tofree" will be set accordingly.
358 * Return FAIL for an error, OK otherwise.
359 */
360 int
361skip_expr_concatenate(char_u **start, char_u **end, evalarg_T *evalarg)
362{
363 typval_T rettv;
364 int res;
365 int vim9script = current_sctx.sc_version == SCRIPT_VERSION_VIM9;
366 garray_T *gap = &evalarg->eval_ga;
367 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
368
369 if (vim9script && evalarg->eval_cookie != NULL)
370 {
371 ga_init2(gap, sizeof(char_u *), 10);
372 if (ga_grow(gap, 1) == OK)
373 // leave room for "start"
374 ++gap->ga_len;
375 }
376
377 // Don't evaluate the expression.
378 if (evalarg != NULL)
379 evalarg->eval_flags &= ~EVAL_EVALUATE;
380 *end = skipwhite(*end);
381 res = eval1(end, &rettv, evalarg);
382 if (evalarg != NULL)
383 evalarg->eval_flags = save_flags;
384
385 if (vim9script && evalarg->eval_cookie != NULL
386 && evalarg->eval_ga.ga_len > 1)
387 {
388 char_u *p;
389 size_t endoff = STRLEN(*end);
390
391 // Line breaks encountered, concatenate all the lines.
392 *((char_u **)gap->ga_data) = *start;
393 p = ga_concat_strings(gap, "");
394 *((char_u **)gap->ga_data) = NULL;
395 ga_clear_strings(gap);
396 gap->ga_itemsize = 0;
397 if (p == NULL)
398 return FAIL;
399 *start = p;
400 vim_free(evalarg->eval_tofree);
401 evalarg->eval_tofree = p;
402 // Compute "end" relative to the end.
403 *end = *start + STRLEN(*start) - endoff;
404 }
405
406 return res;
407}
408
409/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000411 * When "convert" is TRUE convert a List into a sequence of lines and convert
412 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413 * Return pointer to allocated memory, or NULL for failure.
414 */
415 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100416eval_to_string(
417 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100418 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419{
Bram Moolenaar33570922005-01-25 22:26:29 +0000420 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000422 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000423#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000424 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200427 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 retval = NULL;
429 else
430 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000431 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000432 {
433 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000434 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200435 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200436 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200437 if (tv.vval.v_list->lv_len > 0)
438 ga_append(&ga, NL);
439 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000440 ga_append(&ga, NUL);
441 retval = (char_u *)ga.ga_data;
442 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000443#ifdef FEAT_FLOAT
444 else if (convert && tv.v_type == VAR_FLOAT)
445 {
446 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
447 retval = vim_strsave(numbuf);
448 }
449#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000450 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100451 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000452 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 }
454
455 return retval;
456}
457
458/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000459 * Call eval_to_string() without using current local variables and using
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200460 * textwinlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461 */
462 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100463eval_to_string_safe(
464 char_u *arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +0100465 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466{
467 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200468 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200470 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000471 if (use_sandbox)
472 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200473 ++textwinlock;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200474 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000475 if (use_sandbox)
476 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200477 --textwinlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200478 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479 return retval;
480}
481
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482/*
483 * Top level evaluation function, returning a number.
484 * Evaluates "expr" silently.
485 * Returns -1 for an error.
486 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200487 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100488eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489{
Bram Moolenaar33570922005-01-25 22:26:29 +0000490 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200491 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000492 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493
494 ++emsg_off;
495
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200496 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 retval = -1;
498 else
499 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100500 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000501 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 }
503 --emsg_off;
504
505 return retval;
506}
507
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000508/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000509 * Top level evaluation function.
510 * Returns an allocated typval_T with the result.
511 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000512 */
513 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200514eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000515{
516 typval_T *tv;
517
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200518 tv = ALLOC_ONE(typval_T);
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200519 if (tv != NULL && eval0(arg, tv, eap, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100520 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000521
522 return tv;
523}
524
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100526 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200527 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
528 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000529 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200531 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100532call_vim_function(
533 char_u *func,
534 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200535 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200536 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000538 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200539 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100541 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200542 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200543 funcexe.firstline = curwin->w_cursor.lnum;
544 funcexe.lastline = curwin->w_cursor.lnum;
545 funcexe.evaluate = TRUE;
546 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000547 if (ret == FAIL)
548 clear_tv(rettv);
549
550 return ret;
551}
552
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100553/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100554 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100555 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200556 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
557 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100558 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200559 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100560call_func_retnr(
561 char_u *func,
562 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200563 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100564{
565 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200566 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100567
Bram Moolenaarded27a12018-08-01 19:06:03 +0200568 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100569 return -1;
570
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100571 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100572 clear_tv(&rettv);
573 return retval;
574}
575
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000576/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100577 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000578 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200579 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
580 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000581 */
582 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100583call_func_retstr(
584 char_u *func,
585 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200586 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000587{
588 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000589 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000590
Bram Moolenaarded27a12018-08-01 19:06:03 +0200591 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000592 return NULL;
593
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100594 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000595 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 return retval;
597}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000598
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000599/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100600 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200601 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
602 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000603 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000604 */
605 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100606call_func_retlist(
607 char_u *func,
608 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200609 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000610{
611 typval_T rettv;
612
Bram Moolenaarded27a12018-08-01 19:06:03 +0200613 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000614 return NULL;
615
616 if (rettv.v_type != VAR_LIST)
617 {
618 clear_tv(&rettv);
619 return NULL;
620 }
621
622 return rettv.vval.v_list;
623}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625#ifdef FEAT_FOLDING
626/*
627 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
628 * it in "*cp". Doesn't give error messages.
629 */
630 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100631eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632{
Bram Moolenaar33570922005-01-25 22:26:29 +0000633 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200634 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000636 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
637 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638
639 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000640 if (use_sandbox)
641 ++sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200642 ++textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200644 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 retval = 0;
646 else
647 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100648 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000649 if (tv.v_type == VAR_NUMBER)
650 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000651 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 retval = 0;
653 else
654 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100655 // If the result is a string, check if there is a non-digit before
656 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000657 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 if (!VIM_ISDIGIT(*s) && *s != '-')
659 *cp = *s++;
660 retval = atol((char *)s);
661 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000662 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 }
664 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000665 if (use_sandbox)
666 --sandbox;
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +0200667 --textwinlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200669 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670}
671#endif
672
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000674 * Get an lval: variable, Dict item or List item that can be assigned a value
675 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
676 * "name.key", "name.key[expr]" etc.
677 * Indexing only works if "name" is an existing List or Dictionary.
678 * "name" points to the start of the name.
679 * If "rettv" is not NULL it points to the value to be assigned.
680 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
681 * wrong; must end in space or cmd separator.
682 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100683 * flags:
684 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100685 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100686 * GLV_NO_AUTOLOAD: do not use script autoloading
687 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000688 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000689 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000690 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000691 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200692 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100693get_lval(
694 char_u *name,
695 typval_T *rettv,
696 lval_T *lp,
697 int unlet,
698 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100699 int flags, // GLV_ values
700 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000701{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000702 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000703 char_u *expr_start, *expr_end;
704 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000705 dictitem_T *v;
706 typval_T var1;
707 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000708 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000709 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000710 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000711 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +0000712 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100713 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000714
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100715 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200716 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000717
718 if (skip)
719 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100720 // When skipping just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000721 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000722 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000723 }
724
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100725 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000726 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100727 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000728 if (expr_start != NULL)
729 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100730 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100731 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000732 && *p != '[' && *p != '.')
733 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100734 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000735 return NULL;
736 }
737
738 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
739 if (lp->ll_exp_name == NULL)
740 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100741 // Report an invalid expression in braces, unless the
742 // expression evaluation has been cancelled due to an
743 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000744 if (!aborting() && !quiet)
745 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000746 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100747 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000748 return NULL;
749 }
750 }
751 lp->ll_name = lp->ll_exp_name;
752 }
753 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100754 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000755 lp->ll_name = name;
756
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100757 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9 && *p == ':')
758 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100759 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100760 char_u *tp = skipwhite(p + 1);
761
762 // parse the type after the name
763 lp->ll_type = parse_type(&tp, &si->sn_type_list);
764 lp->ll_name_end = tp;
765 }
766 }
767
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100768 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000769 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
770 return p;
771
772 cc = *p;
773 *p = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100774 // Only pass &ht when we would write to the variable, it prevents autoload
775 // as well.
Bram Moolenaar6e65d592017-12-07 22:11:27 +0100776 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
777 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000778 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100779 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000780 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000781 if (v == NULL)
782 return NULL;
783
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000784 /*
785 * Loop until no more [idx] or .key is following.
786 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000787 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100788 var1.v_type = VAR_UNKNOWN;
789 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000790 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000791 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000792 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
793 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100794 && lp->ll_tv->vval.v_dict != NULL)
795 && !(lp->ll_tv->v_type == VAR_BLOB
796 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000797 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000798 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100799 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000800 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000801 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000802 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000803 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000804 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100805 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000806 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000807 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000808
Bram Moolenaar8c711452005-01-14 21:53:12 +0000809 len = -1;
810 if (*p == '.')
811 {
812 key = p + 1;
813 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
814 ;
815 if (len == 0)
816 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000817 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100818 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000819 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000820 }
821 p = key + len;
822 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000823 else
824 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100825 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000826 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000827 if (*p == ':')
828 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000829 else
830 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000831 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200832 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000833 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100834 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000835 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100836 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000837 clear_tv(&var1);
838 return NULL;
839 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000840 }
841
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100842 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000843 if (*p == ':')
844 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000845 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000846 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000847 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100848 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100849 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000850 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000851 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100852 if (rettv != NULL
853 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100854 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100855 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100856 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +0000857 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000858 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100859 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100860 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000861 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000862 }
863 p = skipwhite(p + 1);
864 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000865 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000866 else
867 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000868 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200869 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200870 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000871 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100872 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000873 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000874 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100875 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000876 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100877 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100878 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000879 clear_tv(&var2);
880 return NULL;
881 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000882 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000883 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000884 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000885 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000886 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000887
Bram Moolenaar8c711452005-01-14 21:53:12 +0000888 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000889 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000890 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100891 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100892 clear_tv(&var1);
893 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000894 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000895 }
896
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100897 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000898 ++p;
899 }
900
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000901 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000902 {
903 if (len == -1)
904 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100905 // "[key]": get key from "var1"
906 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +0200907 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000908 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000909 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000910 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000911 }
912 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000913 lp->ll_list = NULL;
914 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000915 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200916
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100917 // When assigning to a scope dictionary check that a function and
918 // variable name is valid (only variable name unless it is l: or
919 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200920 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200921 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200922 int prevval;
923 int wrong;
924
925 if (len != -1)
926 {
927 prevval = key[len];
928 key[len] = NUL;
929 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +0200930 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100931 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200932 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
933 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200934 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200935 || !valid_varname(key);
936 if (len != -1)
937 key[len] = prevval;
938 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200939 {
940 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200941 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200942 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200943 }
944
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000945 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000946 {
Bram Moolenaar31b81602019-02-10 22:14:27 +0100947 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200948 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +0100949 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200950 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100951 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +0100952 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200953 return NULL;
954 }
955
Bram Moolenaar31b81602019-02-10 22:14:27 +0100956 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000957 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000958 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000959 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100960 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100961 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000962 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000963 }
964 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000965 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000966 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000967 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100968 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000969 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000970 p = NULL;
971 break;
972 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100973 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +0100974 else if ((flags & GLV_READ_ONLY) == 0
975 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +0100976 {
977 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200978 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +0100979 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200980
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100981 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000982 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000983 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100984 else if (lp->ll_tv->v_type == VAR_BLOB)
985 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100986 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
987
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100988 /*
989 * Get the number and item for the only or first index of the List.
990 */
991 if (empty1)
992 lp->ll_n1 = 0;
993 else
994 // is number or string
995 lp->ll_n1 = (long)tv_get_number(&var1);
996 clear_tv(&var1);
997
998 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100999 || lp->ll_n1 > bloblen
1000 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001001 {
1002 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001003 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001004 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001005 return NULL;
1006 }
1007 if (lp->ll_range && !lp->ll_empty2)
1008 {
1009 lp->ll_n2 = (long)tv_get_number(&var2);
1010 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001011 if (lp->ll_n2 < 0
1012 || lp->ll_n2 >= bloblen
1013 || lp->ll_n2 < lp->ll_n1)
1014 {
1015 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001016 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001017 return NULL;
1018 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001019 }
1020 lp->ll_blob = lp->ll_tv->vval.v_blob;
1021 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001022 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001023 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001024 else
1025 {
1026 /*
1027 * Get the number and item for the only or first index of the List.
1028 */
1029 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001030 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001031 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001032 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001033 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001034 clear_tv(&var1);
1035
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001036 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001037 lp->ll_list = lp->ll_tv->vval.v_list;
1038 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1039 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001040 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001041 if (lp->ll_n1 < 0)
1042 {
1043 lp->ll_n1 = 0;
1044 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1045 }
1046 }
1047 if (lp->ll_li == NULL)
1048 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001049 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001050 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001051 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001052 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001053 }
1054
1055 /*
1056 * May need to find the item or absolute index for the second
1057 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001058 * When no index given: "lp->ll_empty2" is TRUE.
1059 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001060 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001061 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001062 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001063 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001064 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001065 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001066 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001067 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001068 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001069 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001070 {
1071 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001072 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001073 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001074 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001075 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001076 }
1077
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001078 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001079 if (lp->ll_n1 < 0)
1080 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1081 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001082 {
1083 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001084 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001085 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001086 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001087 }
1088
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001089 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001090 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001091 }
1092
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001093 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001094 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001095 return p;
1096}
1097
1098/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001099 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001100 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001101 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001102clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001103{
1104 vim_free(lp->ll_exp_name);
1105 vim_free(lp->ll_newkey);
1106}
1107
1108/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001109 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001110 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001111 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1112 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001113 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001114 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001115set_var_lval(
1116 lval_T *lp,
1117 char_u *endp,
1118 typval_T *rettv,
1119 int copy,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001120 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar7454a062016-01-30 15:14:10 +01001121 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001122{
1123 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001124 listitem_T *ri;
1125 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001126
1127 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001128 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001129 cc = *endp;
1130 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001131 if (lp->ll_blob != NULL)
1132 {
1133 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001134
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001135 if (op != NULL && *op != '=')
1136 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001137 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001138 return;
1139 }
1140
1141 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1142 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001143 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001144
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001145 if (lp->ll_empty2)
1146 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1147
1148 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001149 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001150 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001151 return;
1152 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001153 if (lp->ll_empty2)
1154 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001155
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001156 ir = 0;
1157 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1158 blob_set(lp->ll_blob, il,
1159 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001160 }
1161 else
1162 {
1163 val = (int)tv_get_number_chk(rettv, &error);
1164 if (!error)
1165 {
1166 garray_T *gap = &lp->ll_blob->bv_ga;
1167
1168 // Allow for appending a byte. Setting a byte beyond
1169 // the end is an error otherwise.
1170 if (lp->ll_n1 < gap->ga_len
1171 || (lp->ll_n1 == gap->ga_len
1172 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1173 {
1174 blob_set(lp->ll_blob, lp->ll_n1, val);
1175 if (lp->ll_n1 == gap->ga_len)
1176 ++gap->ga_len;
1177 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001178 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001179 }
1180 }
1181 }
1182 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001183 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001184 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001185
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001186 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001187 {
1188 emsg(_(e_cannot_mod));
1189 *endp = cc;
1190 return;
1191 }
1192
Bram Moolenaarff697e62019-02-12 22:28:33 +01001193 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001194 di = NULL;
1195 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
1196 &tv, &di, TRUE, FALSE) == OK)
1197 {
1198 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001199 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1200 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001201 && tv_op(&tv, rettv, op) == OK)
1202 set_var(lp->ll_name, &tv, FALSE);
1203 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001204 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001205 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001206 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001207 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001208 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001209 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001210 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001211 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001212 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001213 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001214 else if (lp->ll_range)
1215 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001216 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001217 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001218
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001219 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001220 {
1221 emsg(_("E996: Cannot lock a range"));
1222 return;
1223 }
1224
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001225 /*
1226 * Check whether any of the list items is locked
1227 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001228 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001229 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001230 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001231 return;
1232 ri = ri->li_next;
1233 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1234 break;
1235 ll_li = ll_li->li_next;
1236 ++ll_n1;
1237 }
1238
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001239 /*
1240 * Assign the List values to the list items.
1241 */
1242 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001243 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001244 if (op != NULL && *op != '=')
1245 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1246 else
1247 {
1248 clear_tv(&lp->ll_li->li_tv);
1249 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1250 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001251 ri = ri->li_next;
1252 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1253 break;
1254 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001255 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001256 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001257 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001258 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001259 ri = NULL;
1260 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001261 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001262 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001263 lp->ll_li = lp->ll_li->li_next;
1264 ++lp->ll_n1;
1265 }
1266 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001267 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001268 else if (lp->ll_empty2
1269 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001270 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001271 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001272 }
1273 else
1274 {
1275 /*
1276 * Assign to a List or Dictionary item.
1277 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001278 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001279 {
1280 emsg(_("E996: Cannot lock a list or dict"));
1281 return;
1282 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001283 if (lp->ll_newkey != NULL)
1284 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001285 if (op != NULL && *op != '=')
1286 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001287 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001288 return;
1289 }
1290
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001291 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001292 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001293 if (di == NULL)
1294 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001295 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1296 {
1297 vim_free(di);
1298 return;
1299 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001300 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001301 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001302 else if (op != NULL && *op != '=')
1303 {
1304 tv_op(lp->ll_tv, rettv, op);
1305 return;
1306 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001307 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001308 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001309
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001310 /*
1311 * Assign the value to the variable or list item.
1312 */
1313 if (copy)
1314 copy_tv(rettv, lp->ll_tv);
1315 else
1316 {
1317 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001318 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001319 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001320 }
1321 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001322}
1323
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001324/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001325 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1326 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001327 * Returns OK or FAIL.
1328 */
1329 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001330tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001331{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001332 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001333 char_u numbuf[NUMBUFLEN];
1334 char_u *s;
1335
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001336 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001337 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001338 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001339 {
1340 switch (tv1->v_type)
1341 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001342 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001343 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001344 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001345 case VAR_DICT:
1346 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001347 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001348 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001349 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001350 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001351 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001352 break;
1353
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001354 case VAR_BLOB:
1355 if (*op != '+' || tv2->v_type != VAR_BLOB)
1356 break;
1357 // BLOB += BLOB
1358 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1359 {
1360 blob_T *b1 = tv1->vval.v_blob;
1361 blob_T *b2 = tv2->vval.v_blob;
1362 int i, len = blob_len(b2);
1363 for (i = 0; i < len; i++)
1364 ga_append(&b1->bv_ga, blob_get(b2, i));
1365 }
1366 return OK;
1367
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001368 case VAR_LIST:
1369 if (*op != '+' || tv2->v_type != VAR_LIST)
1370 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001371 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001372 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
1373 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1374 return OK;
1375
1376 case VAR_NUMBER:
1377 case VAR_STRING:
1378 if (tv2->v_type == VAR_LIST)
1379 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001380 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001381 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001382 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001383 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001384#ifdef FEAT_FLOAT
1385 if (tv2->v_type == VAR_FLOAT)
1386 {
1387 float_T f = n;
1388
Bram Moolenaarff697e62019-02-12 22:28:33 +01001389 if (*op == '%')
1390 break;
1391 switch (*op)
1392 {
1393 case '+': f += tv2->vval.v_float; break;
1394 case '-': f -= tv2->vval.v_float; break;
1395 case '*': f *= tv2->vval.v_float; break;
1396 case '/': f /= tv2->vval.v_float; break;
1397 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001398 clear_tv(tv1);
1399 tv1->v_type = VAR_FLOAT;
1400 tv1->vval.v_float = f;
1401 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001402 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001403#endif
1404 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001405 switch (*op)
1406 {
1407 case '+': n += tv_get_number(tv2); break;
1408 case '-': n -= tv_get_number(tv2); break;
1409 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001410 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1411 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001412 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001413 clear_tv(tv1);
1414 tv1->v_type = VAR_NUMBER;
1415 tv1->vval.v_number = n;
1416 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001417 }
1418 else
1419 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001420 if (tv2->v_type == VAR_FLOAT)
1421 break;
1422
Bram Moolenaarff697e62019-02-12 22:28:33 +01001423 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001424 s = tv_get_string(tv1);
1425 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001426 clear_tv(tv1);
1427 tv1->v_type = VAR_STRING;
1428 tv1->vval.v_string = s;
1429 }
1430 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001431
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001432 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001433#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001434 {
1435 float_T f;
1436
Bram Moolenaarff697e62019-02-12 22:28:33 +01001437 if (*op == '%' || *op == '.'
1438 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001439 && tv2->v_type != VAR_NUMBER
1440 && tv2->v_type != VAR_STRING))
1441 break;
1442 if (tv2->v_type == VAR_FLOAT)
1443 f = tv2->vval.v_float;
1444 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001445 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001446 switch (*op)
1447 {
1448 case '+': tv1->vval.v_float += f; break;
1449 case '-': tv1->vval.v_float -= f; break;
1450 case '*': tv1->vval.v_float *= f; break;
1451 case '/': tv1->vval.v_float /= f; break;
1452 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001453 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001454#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001455 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001456 }
1457 }
1458
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001459 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001460 return FAIL;
1461}
1462
1463/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001464 * Evaluate the expression used in a ":for var in expr" command.
1465 * "arg" points to "var".
1466 * Set "*errp" to TRUE for an error, FALSE otherwise;
1467 * Return a pointer that holds the info. Null when there is an error.
1468 */
1469 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001470eval_for_line(
1471 char_u *arg,
1472 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001473 exarg_T *eap,
Bram Moolenaar7454a062016-01-30 15:14:10 +01001474 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001475{
Bram Moolenaar33570922005-01-25 22:26:29 +00001476 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001477 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001478 typval_T tv;
1479 list_T *l;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001480 evalarg_T evalarg;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001481
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001482 CLEAR_FIELD(evalarg);
1483 evalarg.eval_flags = skip ? 0 : EVAL_EVALUATE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001484 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001485
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001486 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001487 if (fi == NULL)
1488 return NULL;
1489
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001490 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon, FALSE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001491 if (expr == NULL)
1492 return fi;
1493
1494 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001495 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001496 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001497 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001498 return fi;
1499 }
1500
1501 if (skip)
1502 ++emsg_skip;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001503 if (eval0(skipwhite(expr + 2), &tv, eap, &evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001504 {
1505 *errp = FALSE;
1506 if (!skip)
1507 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001508 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001509 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001510 l = tv.vval.v_list;
1511 if (l == NULL)
1512 {
1513 // a null list is like an empty list: do nothing
1514 clear_tv(&tv);
1515 }
1516 else
1517 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001518 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001519 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001520
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001521 // No need to increment the refcount, it's already set for
1522 // the list being used in "tv".
1523 fi->fi_list = l;
1524 list_add_watch(l, &fi->fi_lw);
1525 fi->fi_lw.lw_item = l->lv_first;
1526 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001527 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001528 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001529 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001530 fi->fi_bi = 0;
1531 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001532 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001533 typval_T btv;
1534
1535 // Make a copy, so that the iteration still works when the
1536 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001537 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001538 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001539 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001540 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001541 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001542 else
1543 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001544 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001545 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001546 }
1547 }
1548 }
1549 if (skip)
1550 --emsg_skip;
1551
1552 return fi;
1553}
1554
1555/*
1556 * Use the first item in a ":for" list. Advance to the next.
1557 * Assign the values to the variable (list). "arg" points to the first one.
1558 * Return TRUE when a valid item was found, FALSE when at end of list or
1559 * something wrong.
1560 */
1561 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001562next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001563{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001564 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001565 int result;
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001566 int flag = current_sctx.sc_version == SCRIPT_VERSION_VIM9 ?
1567 LET_NO_COMMAND : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001568 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001569
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001570 if (fi->fi_blob != NULL)
1571 {
1572 typval_T tv;
1573
1574 if (fi->fi_bi >= blob_len(fi->fi_blob))
1575 return FALSE;
1576 tv.v_type = VAR_NUMBER;
1577 tv.v_lock = VAR_FIXED;
1578 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1579 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001580 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001581 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001582 }
1583
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001584 item = fi->fi_lw.lw_item;
1585 if (item == NULL)
1586 result = FALSE;
1587 else
1588 {
1589 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001590 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001591 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001592 }
1593 return result;
1594}
1595
1596/*
1597 * Free the structure used to store info used by ":for".
1598 */
1599 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001600free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001601{
Bram Moolenaar33570922005-01-25 22:26:29 +00001602 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001603
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001604 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001605 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001606 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001607 list_unref(fi->fi_list);
1608 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001609 if (fi != NULL && fi->fi_blob != NULL)
1610 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001611 vim_free(fi);
1612}
1613
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001615set_context_for_expression(
1616 expand_T *xp,
1617 char_u *arg,
1618 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619{
1620 int got_eq = FALSE;
1621 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001622 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001624 if (cmdidx == CMD_let || cmdidx == CMD_const)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001625 {
1626 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001627 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001628 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001629 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001630 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001631 {
1632 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001633 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001634 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001635 break;
1636 }
1637 return;
1638 }
1639 }
1640 else
1641 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1642 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 while ((xp->xp_pattern = vim_strpbrk(arg,
1644 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1645 {
1646 c = *xp->xp_pattern;
1647 if (c == '&')
1648 {
1649 c = xp->xp_pattern[1];
1650 if (c == '&')
1651 {
1652 ++xp->xp_pattern;
1653 xp->xp_context = cmdidx != CMD_let || got_eq
1654 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1655 }
1656 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001657 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001659 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1660 xp->xp_pattern += 2;
1661
1662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 }
1664 else if (c == '$')
1665 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001666 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 xp->xp_context = EXPAND_ENV_VARS;
1668 }
1669 else if (c == '=')
1670 {
1671 got_eq = TRUE;
1672 xp->xp_context = EXPAND_EXPRESSION;
1673 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001674 else if (c == '#'
1675 && xp->xp_context == EXPAND_EXPRESSION)
1676 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001677 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001678 break;
1679 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001680 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 && xp->xp_context == EXPAND_FUNCTIONS
1682 && vim_strchr(xp->xp_pattern, '(') == NULL)
1683 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001684 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 break;
1686 }
1687 else if (cmdidx != CMD_let || got_eq)
1688 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001689 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 {
1691 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1692 if (c == '\\' && xp->xp_pattern[1] != NUL)
1693 ++xp->xp_pattern;
1694 xp->xp_context = EXPAND_NOTHING;
1695 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001696 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001698 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1700 /* skip */ ;
1701 xp->xp_context = EXPAND_NOTHING;
1702 }
1703 else if (c == '|')
1704 {
1705 if (xp->xp_pattern[1] == '|')
1706 {
1707 ++xp->xp_pattern;
1708 xp->xp_context = EXPAND_EXPRESSION;
1709 }
1710 else
1711 xp->xp_context = EXPAND_COMMANDS;
1712 }
1713 else
1714 xp->xp_context = EXPAND_EXPRESSION;
1715 }
1716 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001717 // Doesn't look like something valid, expand as an expression
1718 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001719 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 arg = xp->xp_pattern;
1721 if (*arg != NUL)
1722 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1723 /* skip */ ;
1724 }
1725 xp->xp_pattern = arg;
1726}
1727
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001729 * Return TRUE if "pat" matches "text".
1730 * Does not use 'cpo' and always uses 'magic'.
1731 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001732 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001733pattern_match(char_u *pat, char_u *text, int ic)
1734{
1735 int matches = FALSE;
1736 char_u *save_cpo;
1737 regmatch_T regmatch;
1738
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001739 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001740 save_cpo = p_cpo;
1741 p_cpo = (char_u *)"";
1742 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1743 if (regmatch.regprog != NULL)
1744 {
1745 regmatch.rm_ic = ic;
1746 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1747 vim_regfree(regmatch.regprog);
1748 }
1749 p_cpo = save_cpo;
1750 return matches;
1751}
1752
1753/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001754 * Handle a name followed by "(". Both for just "name(arg)" and for
1755 * "expr->name(arg)".
1756 * Returns OK or FAIL.
1757 */
1758 static int
1759eval_func(
1760 char_u **arg, // points to "(", will be advanced
1761 char_u *name,
1762 int name_len,
1763 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02001764 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001765 typval_T *basetv) // "expr" for "expr->name(arg)"
1766{
Bram Moolenaar32e35112020-05-14 22:41:15 +02001767 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001768 char_u *s = name;
1769 int len = name_len;
1770 partial_T *partial;
1771 int ret = OK;
1772
1773 if (!evaluate)
1774 check_vars(s, len);
1775
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001776 // If "s" is the name of a variable of type VAR_FUNC
1777 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001778 s = deref_func_name(s, &len, &partial, !evaluate);
1779
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001780 // Need to make a copy, in case evaluating the arguments makes
1781 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001782 s = vim_strsave(s);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001783 if (s == NULL || (flags & EVAL_CONSTANT))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001784 ret = FAIL;
1785 else
1786 {
1787 funcexe_T funcexe;
1788
1789 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001790 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001791 funcexe.firstline = curwin->w_cursor.lnum;
1792 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001793 funcexe.evaluate = evaluate;
1794 funcexe.partial = partial;
1795 funcexe.basetv = basetv;
1796 ret = get_func_tv(s, len, rettv, arg, &funcexe);
1797 }
1798 vim_free(s);
1799
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001800 // If evaluate is FALSE rettv->v_type was not set in
1801 // get_func_tv, but it's needed in handle_subscript() to parse
1802 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001803 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
1804 {
1805 rettv->vval.v_string = NULL;
1806 rettv->v_type = VAR_FUNC;
1807 }
1808
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001809 // Stop the expression evaluation when immediately
1810 // aborting on error, or when an interrupt occurred or
1811 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001812 if (evaluate && aborting())
1813 {
1814 if (ret == OK)
1815 clear_tv(rettv);
1816 ret = FAIL;
1817 }
1818 return ret;
1819}
1820
1821/*
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001822 * If inside Vim9 script, "arg" points to the end of a line (ignoring comments)
1823 * and there is a next line, return the next line (skipping blanks) and set
1824 * "getnext".
1825 * Otherwise just return "arg" unmodified and set "getnext" to FALSE.
1826 * "arg" must point somewhere inside a line, not at the start.
1827 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001828 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001829eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
1830{
1831 *getnext = FALSE;
1832 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9
1833 && evalarg != NULL
1834 && evalarg->eval_cookie != NULL
1835 && (*arg == NUL || (VIM_ISWHITE(arg[-1])
1836 && (*arg == '"' || *arg == '#')))
1837 && source_nextline(evalarg->eval_cookie) != NULL)
1838 {
1839 char_u *p = source_nextline(evalarg->eval_cookie);
1840
1841 if (p != NULL)
1842 {
1843 *getnext = TRUE;
1844 return skipwhite(p);
1845 }
1846 }
1847 return arg;
1848}
1849
1850/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001851 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001852 */
Bram Moolenaar71478202020-06-26 22:46:27 +02001853 char_u *
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001854eval_next_line(evalarg_T *evalarg)
1855{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001856 garray_T *gap = &evalarg->eval_ga;
1857 char_u *line;
1858
1859 line = getsourceline(0, evalarg->eval_cookie, 0, TRUE);
1860 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
1861 {
1862 // Going to concatenate the lines after parsing.
1863 ((char_u **)gap->ga_data)[gap->ga_len] = line;
1864 ++gap->ga_len;
1865 }
1866 else
1867 {
1868 vim_free(evalarg->eval_tofree);
1869 evalarg->eval_tofree = line;
1870 }
1871 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001872}
1873
1874/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001876 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
1878 */
1879
1880/*
1881 * Handle zero level expression.
1882 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001883 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001884 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001885 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 * Return OK or FAIL.
1887 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001888 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001889eval0(
1890 char_u *arg,
1891 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001892 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001893 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894{
1895 int ret;
1896 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001897 int did_emsg_before = did_emsg;
1898 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001899 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900
1901 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001902 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001903
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001904 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 {
1906 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001907 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 /*
1909 * Report the invalid expression unless the expression evaluation has
1910 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001911 * exception, or we already gave a more specific error.
1912 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02001914 if (!aborting()
1915 && did_emsg == did_emsg_before
1916 && called_emsg == called_emsg_before
1917 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001918 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 ret = FAIL;
1920 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001921
1922 if (eap != NULL)
1923 eap->nextcmd = check_nextcmd(p);
1924
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001925 if (evalarg != NULL && eap != NULL && evalarg->eval_tofree != NULL)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001926 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001927 // We may need to keep the original command line, e.g. for
1928 // ":let" it has the variable names. But we may also need the
1929 // new one, "nextcmd" points into it. Keep both.
1930 vim_free(eap->cmdline_tofree);
1931 eap->cmdline_tofree = *eap->cmdlinep;
1932 *eap->cmdlinep = evalarg->eval_tofree;
1933 evalarg->eval_tofree = NULL;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935
1936 return ret;
1937}
1938
1939/*
1940 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00001941 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 *
1943 * "arg" must point to the first non-white of the expression.
1944 * "arg" is advanced to the next non-white after the recognized expression.
1945 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00001946 * Note: "rettv.v_lock" is not set.
1947 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 * Return OK or FAIL.
1949 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02001950 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001951eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952{
Bram Moolenaar793648f2020-06-26 21:28:25 +02001953 char_u *p;
1954 int getnext;
1955
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 /*
1957 * Get the first variable.
1958 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001959 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 return FAIL;
1961
Bram Moolenaar793648f2020-06-26 21:28:25 +02001962 p = eval_next_non_blank(*arg, evalarg, &getnext);
1963 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001965 int result;
1966 typval_T var2;
1967 evalarg_T nested_evalarg;
1968 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02001969 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001970
Bram Moolenaar793648f2020-06-26 21:28:25 +02001971 if (getnext)
1972 *arg = eval_next_line(evalarg);
1973
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001974 if (evalarg == NULL)
1975 {
1976 CLEAR_FIELD(nested_evalarg);
1977 orig_flags = 0;
1978 }
1979 else
1980 {
1981 nested_evalarg = *evalarg;
1982 orig_flags = evalarg->eval_flags;
1983 }
1984
Bram Moolenaar7acde512020-06-24 23:02:40 +02001985 evaluate = nested_evalarg.eval_flags & EVAL_EVALUATE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001987 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001989 int error = FALSE;
1990
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001991 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001993 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001994 if (error)
1995 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 }
1997
1998 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02001999 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 */
2001 *arg = skipwhite(*arg + 1);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002002 nested_evalarg.eval_flags = result ? orig_flags
2003 : orig_flags & ~EVAL_EVALUATE;
2004 if (eval1(arg, rettv, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 return FAIL;
2006
2007 /*
2008 * Check for the ":".
2009 */
Bram Moolenaar793648f2020-06-26 21:28:25 +02002010 p = eval_next_non_blank(*arg, evalarg, &getnext);
2011 if (*p != ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002013 emsg(_(e_missing_colon));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002015 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 return FAIL;
2017 }
Bram Moolenaar793648f2020-06-26 21:28:25 +02002018 if (getnext)
2019 *arg = eval_next_line(evalarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020
2021 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002022 * Get the third variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 */
2024 *arg = skipwhite(*arg + 1);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002025 nested_evalarg.eval_flags = !result ? orig_flags
2026 : orig_flags & ~EVAL_EVALUATE;
2027 if (eval1(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 {
2029 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002030 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 return FAIL;
2032 }
2033 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002034 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 }
2036
2037 return OK;
2038}
2039
2040/*
2041 * Handle first level expression:
2042 * expr2 || expr2 || expr2 logical OR
2043 *
2044 * "arg" must point to the first non-white of the expression.
2045 * "arg" is advanced to the next non-white after the recognized expression.
2046 *
2047 * Return OK or FAIL.
2048 */
2049 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002050eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002052 char_u *p;
2053 int getnext;
Bram Moolenaar33570922005-01-25 22:26:29 +00002054 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 long result;
2056 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002057 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058
2059 /*
2060 * Get the first variable.
2061 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002062 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 return FAIL;
2064
2065 /*
2066 * Repeat until there is no following "||".
2067 */
2068 first = TRUE;
2069 result = FALSE;
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002070 p = eval_next_non_blank(*arg, evalarg, &getnext);
2071 while (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002073 evalarg_T nested_evalarg;
2074 int evaluate;
2075 int orig_flags;
2076
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002077 if (getnext)
2078 *arg = eval_next_line(evalarg);
2079
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002080 if (evalarg == NULL)
2081 {
2082 CLEAR_FIELD(nested_evalarg);
2083 orig_flags = 0;
2084 evaluate = FALSE;
2085 }
2086 else
2087 {
2088 nested_evalarg = *evalarg;
2089 orig_flags = evalarg->eval_flags;
2090 evaluate = orig_flags & EVAL_EVALUATE;
2091 }
Bram Moolenaar32e35112020-05-14 22:41:15 +02002092
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 if (evaluate && first)
2094 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002095 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002097 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002098 if (error)
2099 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 first = FALSE;
2101 }
2102
2103 /*
2104 * Get the second variable.
2105 */
2106 *arg = skipwhite(*arg + 2);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002107 nested_evalarg.eval_flags = !result ? orig_flags
2108 : orig_flags & ~EVAL_EVALUATE;
2109 if (eval3(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 return FAIL;
2111
2112 /*
2113 * Compute the result.
2114 */
2115 if (evaluate && !result)
2116 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002117 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002119 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002120 if (error)
2121 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 }
2123 if (evaluate)
2124 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002125 rettv->v_type = VAR_NUMBER;
2126 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002128
2129 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 }
2131
2132 return OK;
2133}
2134
2135/*
2136 * Handle second level expression:
2137 * expr3 && expr3 && expr3 logical AND
2138 *
2139 * "arg" must point to the first non-white of the expression.
2140 * "arg" is advanced to the next non-white after the recognized expression.
2141 *
2142 * Return OK or FAIL.
2143 */
2144 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002145eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002147 char_u *p;
2148 int getnext;
Bram Moolenaar33570922005-01-25 22:26:29 +00002149 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 long result;
2151 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002152 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153
2154 /*
2155 * Get the first variable.
2156 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002157 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 return FAIL;
2159
2160 /*
2161 * Repeat until there is no following "&&".
2162 */
2163 first = TRUE;
2164 result = TRUE;
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002165 p = eval_next_non_blank(*arg, evalarg, &getnext);
2166 while (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002168 evalarg_T nested_evalarg;
2169 int orig_flags;
2170 int evaluate;
Bram Moolenaar32e35112020-05-14 22:41:15 +02002171
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002172 if (getnext)
2173 *arg = eval_next_line(evalarg);
2174
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002175 if (evalarg == NULL)
2176 {
2177 CLEAR_FIELD(nested_evalarg);
2178 orig_flags = 0;
2179 evaluate = FALSE;
2180 }
2181 else
2182 {
2183 nested_evalarg = *evalarg;
2184 orig_flags = evalarg->eval_flags;
2185 evaluate = orig_flags & EVAL_EVALUATE;
2186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 if (evaluate && first)
2188 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002189 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002191 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002192 if (error)
2193 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 first = FALSE;
2195 }
2196
2197 /*
2198 * Get the second variable.
2199 */
2200 *arg = skipwhite(*arg + 2);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002201 nested_evalarg.eval_flags = result ? orig_flags
2202 : orig_flags & ~EVAL_EVALUATE;
2203 if (eval4(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 return FAIL;
2205
2206 /*
2207 * Compute the result.
2208 */
2209 if (evaluate && result)
2210 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002211 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002213 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002214 if (error)
2215 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 }
2217 if (evaluate)
2218 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002219 rettv->v_type = VAR_NUMBER;
2220 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002222
2223 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 }
2225
2226 return OK;
2227}
2228
2229/*
2230 * Handle third level expression:
2231 * var1 == var2
2232 * var1 =~ var2
2233 * var1 != var2
2234 * var1 !~ var2
2235 * var1 > var2
2236 * var1 >= var2
2237 * var1 < var2
2238 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002239 * var1 is var2
2240 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 *
2242 * "arg" must point to the first non-white of the expression.
2243 * "arg" is advanced to the next non-white after the recognized expression.
2244 *
2245 * Return OK or FAIL.
2246 */
2247 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002248eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249{
Bram Moolenaar33570922005-01-25 22:26:29 +00002250 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002252 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 int i;
Bram Moolenaar87396072019-12-31 22:36:18 +01002254 exptype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257
2258 /*
2259 * Get the first variable.
2260 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002261 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 return FAIL;
2263
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002264 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 switch (p[0])
2266 {
2267 case '=': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002268 type = EXPR_EQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002270 type = EXPR_MATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 break;
2272 case '!': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002273 type = EXPR_NEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002275 type = EXPR_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 break;
2277 case '>': if (p[1] != '=')
2278 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002279 type = EXPR_GREATER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 len = 1;
2281 }
2282 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002283 type = EXPR_GEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 break;
2285 case '<': if (p[1] != '=')
2286 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002287 type = EXPR_SMALLER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 len = 1;
2289 }
2290 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002291 type = EXPR_SEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002293 case 'i': if (p[1] == 's')
2294 {
2295 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2296 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02002297 i = p[len];
2298 if (!isalnum(i) && i != '_')
Bram Moolenaar87396072019-12-31 22:36:18 +01002299 type = len == 2 ? EXPR_IS : EXPR_ISNOT;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002300 }
2301 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 }
2303
2304 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002305 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002307 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002309 if (getnext)
2310 *arg = eval_next_line(evalarg);
2311
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002312 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 if (p[len] == '?')
2314 {
2315 ic = TRUE;
2316 ++len;
2317 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002318 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 else if (p[len] == '#')
2320 {
2321 ic = FALSE;
2322 ++len;
2323 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002324 // nothing appended: use 'ignorecase'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 else
2326 ic = p_ic;
2327
2328 /*
2329 * Get the second variable.
2330 */
2331 *arg = skipwhite(p + len);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002332 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002334 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 return FAIL;
2336 }
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002337 if (evalarg != NULL && (evalarg->eval_flags & EVAL_EVALUATE))
Bram Moolenaar31988702018-02-13 12:57:42 +01002338 {
Bram Moolenaar07a3db82019-12-25 18:14:14 +01002339 int ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002340
2341 clear_tv(&var2);
2342 return ret;
2343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 }
2345
2346 return OK;
2347}
2348
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002349 void
2350eval_addblob(typval_T *tv1, typval_T *tv2)
2351{
2352 blob_T *b1 = tv1->vval.v_blob;
2353 blob_T *b2 = tv2->vval.v_blob;
2354 blob_T *b = blob_alloc();
2355 int i;
2356
2357 if (b != NULL)
2358 {
2359 for (i = 0; i < blob_len(b1); i++)
2360 ga_append(&b->bv_ga, blob_get(b1, i));
2361 for (i = 0; i < blob_len(b2); i++)
2362 ga_append(&b->bv_ga, blob_get(b2, i));
2363
2364 clear_tv(tv1);
2365 rettv_blob_set(tv1, b);
2366 }
2367}
2368
2369 int
2370eval_addlist(typval_T *tv1, typval_T *tv2)
2371{
2372 typval_T var3;
2373
2374 // concatenate Lists
2375 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2376 {
2377 clear_tv(tv1);
2378 clear_tv(tv2);
2379 return FAIL;
2380 }
2381 clear_tv(tv1);
2382 *tv1 = var3;
2383 return OK;
2384}
2385
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386/*
2387 * Handle fourth level expression:
2388 * + number addition
2389 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002390 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002391 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 *
2393 * "arg" must point to the first non-white of the expression.
2394 * "arg" is advanced to the next non-white after the recognized expression.
2395 *
2396 * Return OK or FAIL.
2397 */
2398 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002399eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002401 int evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402
2403 /*
2404 * Get the first variable.
2405 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002406 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 return FAIL;
2408
2409 /*
2410 * Repeat computing, until no '+', '-' or '.' is following.
2411 */
2412 for (;;)
2413 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002414 int getnext;
2415 char_u *p;
2416 int op;
2417 int concat;
2418 typval_T var2;
2419
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002420 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002421 p = eval_next_non_blank(*arg, evalarg, &getnext);
2422 op = *p;
2423 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002424 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 break;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002426 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002427 *arg = eval_next_line(evalarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002429 if ((op != '+' || (rettv->v_type != VAR_LIST
2430 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002431#ifdef FEAT_FLOAT
2432 && (op == '.' || rettv->v_type != VAR_FLOAT)
2433#endif
2434 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002435 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002436 // For "list + ...", an illegal use of the first operand as
2437 // a number cannot be determined before evaluating the 2nd
2438 // operand: if this is also a list, all is ok.
2439 // For "something . ...", "something - ..." or "non-list + ...",
2440 // we know that the first operand needs to be a string or number
2441 // without evaluating the 2nd operand. So check before to avoid
2442 // side effects after an error.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002443 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002444 {
2445 clear_tv(rettv);
2446 return FAIL;
2447 }
2448 }
2449
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 /*
2451 * Get the second variable.
2452 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002453 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
2454 ++*arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 *arg = skipwhite(*arg + 1);
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02002456 eval_next_non_blank(*arg, evalarg, &getnext);
2457 if (getnext)
2458 *arg = eval_next_line(evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002459 if (eval6(arg, &var2, evalarg, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002461 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 return FAIL;
2463 }
2464
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002465 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 {
2467 /*
2468 * Compute the result.
2469 */
2470 if (op == '.')
2471 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002472 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2473 char_u *s1 = tv_get_string_buf(rettv, buf1);
2474 char_u *s2 = tv_get_string_buf_chk(&var2, buf2);
2475
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002476 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002477 {
2478 clear_tv(rettv);
2479 clear_tv(&var2);
2480 return FAIL;
2481 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002482 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002483 clear_tv(rettv);
2484 rettv->v_type = VAR_STRING;
2485 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002487 else if (op == '+' && rettv->v_type == VAR_BLOB
2488 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002489 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002490 else if (op == '+' && rettv->v_type == VAR_LIST
2491 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002492 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002493 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002494 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 else
2497 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002498 int error = FALSE;
2499 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002500#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002501 float_T f1 = 0, f2 = 0;
2502
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002503 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002504 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002505 f1 = rettv->vval.v_float;
2506 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002507 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002508 else
2509#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002510 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002511 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002512 if (error)
2513 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002514 // This can only happen for "list + non-list". For
2515 // "non-list + ..." or "something - ...", we returned
2516 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002517 clear_tv(rettv);
2518 return FAIL;
2519 }
2520#ifdef FEAT_FLOAT
2521 if (var2.v_type == VAR_FLOAT)
2522 f1 = n1;
2523#endif
2524 }
2525#ifdef FEAT_FLOAT
2526 if (var2.v_type == VAR_FLOAT)
2527 {
2528 f2 = var2.vval.v_float;
2529 n2 = 0;
2530 }
2531 else
2532#endif
2533 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002534 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002535 if (error)
2536 {
2537 clear_tv(rettv);
2538 clear_tv(&var2);
2539 return FAIL;
2540 }
2541#ifdef FEAT_FLOAT
2542 if (rettv->v_type == VAR_FLOAT)
2543 f2 = n2;
2544#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002545 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002546 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002547
2548#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002549 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002550 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2551 {
2552 if (op == '+')
2553 f1 = f1 + f2;
2554 else
2555 f1 = f1 - f2;
2556 rettv->v_type = VAR_FLOAT;
2557 rettv->vval.v_float = f1;
2558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002560#endif
2561 {
2562 if (op == '+')
2563 n1 = n1 + n2;
2564 else
2565 n1 = n1 - n2;
2566 rettv->v_type = VAR_NUMBER;
2567 rettv->vval.v_number = n1;
2568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002570 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 }
2572 }
2573 return OK;
2574}
2575
2576/*
2577 * Handle fifth level expression:
2578 * * number multiplication
2579 * / number division
2580 * % number modulo
2581 *
2582 * "arg" must point to the first non-white of the expression.
2583 * "arg" is advanced to the next non-white after the recognized expression.
2584 *
2585 * Return OK or FAIL.
2586 */
2587 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002588eval6(
2589 char_u **arg,
2590 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002591 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002592 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593{
Bram Moolenaar33570922005-01-25 22:26:29 +00002594 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002596 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002597#ifdef FEAT_FLOAT
2598 int use_float = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02002599 float_T f1 = 0, f2 = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002600#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002601 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602
2603 /*
2604 * Get the first variable.
2605 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002606 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 return FAIL;
2608
2609 /*
2610 * Repeat computing, until no '*', '/' or '%' is following.
2611 */
2612 for (;;)
2613 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002614 int evaluate = evalarg == NULL ? 0
2615 : (evalarg->eval_flags & EVAL_EVALUATE);
2616 int getnext;
2617
2618 op = *eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002619 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 break;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002621 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002622 *arg = eval_next_line(evalarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002624 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002626#ifdef FEAT_FLOAT
2627 if (rettv->v_type == VAR_FLOAT)
2628 {
2629 f1 = rettv->vval.v_float;
2630 use_float = TRUE;
2631 n1 = 0;
2632 }
2633 else
2634#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002635 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002636 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002637 if (error)
2638 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 }
2640 else
2641 n1 = 0;
2642
2643 /*
2644 * Get the second variable.
2645 */
2646 *arg = skipwhite(*arg + 1);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002647 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 return FAIL;
2649
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002650 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002652#ifdef FEAT_FLOAT
2653 if (var2.v_type == VAR_FLOAT)
2654 {
2655 if (!use_float)
2656 {
2657 f1 = n1;
2658 use_float = TRUE;
2659 }
2660 f2 = var2.vval.v_float;
2661 n2 = 0;
2662 }
2663 else
2664#endif
2665 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002666 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002667 clear_tv(&var2);
2668 if (error)
2669 return FAIL;
2670#ifdef FEAT_FLOAT
2671 if (use_float)
2672 f2 = n2;
2673#endif
2674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675
2676 /*
2677 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002678 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002680#ifdef FEAT_FLOAT
2681 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002683 if (op == '*')
2684 f1 = f1 * f2;
2685 else if (op == '/')
2686 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002687# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002688 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002689 if (f2 == 0.0)
2690 {
2691 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002692 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002693 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002694 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002695 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002696 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002697 }
2698 else
2699 f1 = f1 / f2;
2700# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002701 // We rely on the floating point library to handle divide
2702 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002703 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002704# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002707 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002708 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002709 return FAIL;
2710 }
2711 rettv->v_type = VAR_FLOAT;
2712 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 }
2714 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002717 if (op == '*')
2718 n1 = n1 * n2;
2719 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01002720 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01002722 n1 = num_modulus(n1, n2);
2723
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002724 rettv->v_type = VAR_NUMBER;
2725 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 }
2728 }
2729
2730 return OK;
2731}
2732
2733/*
2734 * Handle sixth level expression:
2735 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002736 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002737 * "string" string constant
2738 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 * &option-name option value
2740 * @r register contents
2741 * identifier variable value
2742 * function() function call
2743 * $VAR environment variable
2744 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002745 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002746 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002747 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002748 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 *
2750 * Also handle:
2751 * ! in front logical NOT
2752 * - in front unary minus
2753 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002754 * trailing [] subscript in String or List
2755 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02002756 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 *
2758 * "arg" must point to the first non-white of the expression.
2759 * "arg" is advanced to the next non-white after the recognized expression.
2760 *
2761 * Return OK or FAIL.
2762 */
2763 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002764eval7(
2765 char_u **arg,
2766 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002767 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002768 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002770 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
2771 int evaluate = evalarg != NULL
2772 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 int len;
2774 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 char_u *start_leader, *end_leader;
2776 int ret = OK;
2777 char_u *alias;
2778
2779 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002780 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002781 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002783 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784
2785 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02002786 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 */
2788 start_leader = *arg;
2789 while (**arg == '!' || **arg == '-' || **arg == '+')
2790 *arg = skipwhite(*arg + 1);
2791 end_leader = *arg;
2792
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002793 if (**arg == '.' && (!isdigit(*(*arg + 1))
2794#ifdef FEAT_FLOAT
2795 || current_sctx.sc_version < 2
2796#endif
2797 ))
2798 {
2799 semsg(_(e_invexpr2), *arg);
2800 ++*arg;
2801 return FAIL;
2802 }
2803
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 switch (**arg)
2805 {
2806 /*
2807 * Number constant.
2808 */
2809 case '0':
2810 case '1':
2811 case '2':
2812 case '3':
2813 case '4':
2814 case '5':
2815 case '6':
2816 case '7':
2817 case '8':
2818 case '9':
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002819 case '.': ret = get_number_tv(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02002820
2821 // Apply prefixed "-" and "+" now. Matters especially when
2822 // "->" follows.
2823 if (ret == OK && evaluate && end_leader > start_leader)
2824 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 break;
2826
2827 /*
2828 * String constant: "string".
2829 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002830 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 break;
2832
2833 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002834 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002836 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002837 break;
2838
2839 /*
2840 * List: [expr, expr]
2841 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002842 case '[': ret = get_list_tv(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 break;
2844
2845 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002846 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002847 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002848 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002849 {
2850 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002851 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002852 }
2853 else
2854 ret = NOTDONE;
2855 break;
2856
2857 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002858 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002859 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00002860 */
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002861 case '{': ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002862 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002863 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002864 break;
2865
2866 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00002867 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00002869 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 break;
2871
2872 /*
2873 * Environment variable: $VAR.
2874 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002875 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 break;
2877
2878 /*
2879 * Register contents: @r.
2880 */
2881 case '@': ++*arg;
2882 if (evaluate)
2883 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002884 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002885 rettv->vval.v_string = get_reg_contents(**arg,
2886 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 }
2888 if (**arg != NUL)
2889 ++*arg;
2890 break;
2891
2892 /*
2893 * nested expression: (expression).
2894 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002895 case '(': {
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02002896 int getnext;
2897
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002898 *arg = skipwhite(*arg + 1);
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02002899 eval_next_non_blank(*arg, evalarg, &getnext);
2900 if (getnext)
2901 *arg = eval_next_line(evalarg);
2902
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002903 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02002904
2905 eval_next_non_blank(*arg, evalarg, &getnext);
2906 if (getnext)
2907 *arg = eval_next_line(evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002908 if (**arg == ')')
2909 ++*arg;
2910 else if (ret == OK)
2911 {
2912 emsg(_(e_missing_close));
2913 clear_tv(rettv);
2914 ret = FAIL;
2915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 }
2917 break;
2918
Bram Moolenaar8c711452005-01-14 21:53:12 +00002919 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 break;
2921 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002922
2923 if (ret == NOTDONE)
2924 {
2925 /*
2926 * Must be a variable or function name.
2927 * Can also be a curly-braces kind of name: {expr}.
2928 */
2929 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002930 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002931 if (alias != NULL)
2932 s = alias;
2933
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002934 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002935 ret = FAIL;
2936 else
2937 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002938 if (**arg == '(') // recursive!
Bram Moolenaar32e35112020-05-14 22:41:15 +02002939 ret = eval_func(arg, s, len, rettv, flags, NULL);
Bram Moolenaar227a69d2020-05-15 18:17:28 +02002940 else if (flags & EVAL_CONSTANT)
2941 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002942 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002943 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002944 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002945 {
2946 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002947 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002948 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002949 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01002950 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002951 }
2952
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 *arg = skipwhite(*arg);
2954
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002955 // Handle following '[', '(' and '.' for expr[expr], expr.name,
2956 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002957 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002958 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959
2960 /*
2961 * Apply logical NOT and unary '-', from right to left, ignore '+'.
2962 */
2963 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02002964 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002965 return ret;
2966}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002967
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002968/*
2969 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02002970 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002971 * Adjusts "end_leaderp" until it is at "start_leader".
2972 */
2973 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02002974eval7_leader(
2975 typval_T *rettv,
2976 int numeric_only,
2977 char_u *start_leader,
2978 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002979{
2980 char_u *end_leader = *end_leaderp;
2981 int ret = OK;
2982 int error = FALSE;
2983 varnumber_T val = 0;
2984#ifdef FEAT_FLOAT
2985 float_T f = 0.0;
2986
2987 if (rettv->v_type == VAR_FLOAT)
2988 f = rettv->vval.v_float;
2989 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002990#endif
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002991 val = tv_get_number_chk(rettv, &error);
2992 if (error)
2993 {
2994 clear_tv(rettv);
2995 ret = FAIL;
2996 }
2997 else
2998 {
2999 while (end_leader > start_leader)
3000 {
3001 --end_leader;
3002 if (*end_leader == '!')
3003 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003004 if (numeric_only)
3005 {
3006 ++end_leader;
3007 break;
3008 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003009#ifdef FEAT_FLOAT
3010 if (rettv->v_type == VAR_FLOAT)
3011 f = !f;
3012 else
3013#endif
3014 val = !val;
3015 }
3016 else if (*end_leader == '-')
3017 {
3018#ifdef FEAT_FLOAT
3019 if (rettv->v_type == VAR_FLOAT)
3020 f = -f;
3021 else
3022#endif
3023 val = -val;
3024 }
3025 }
3026#ifdef FEAT_FLOAT
3027 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003029 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003030 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003032 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003033#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003034 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003035 clear_tv(rettv);
3036 rettv->v_type = VAR_NUMBER;
3037 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003040 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 return ret;
3042}
3043
3044/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003045 * Call the function referred to in "rettv".
3046 */
3047 static int
3048call_func_rettv(
3049 char_u **arg,
3050 typval_T *rettv,
3051 int evaluate,
3052 dict_T *selfdict,
3053 typval_T *basetv)
3054{
3055 partial_T *pt = NULL;
3056 funcexe_T funcexe;
3057 typval_T functv;
3058 char_u *s;
3059 int ret;
3060
3061 // need to copy the funcref so that we can clear rettv
3062 if (evaluate)
3063 {
3064 functv = *rettv;
3065 rettv->v_type = VAR_UNKNOWN;
3066
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003067 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003068 if (functv.v_type == VAR_PARTIAL)
3069 {
3070 pt = functv.vval.v_partial;
3071 s = partial_name(pt);
3072 }
3073 else
3074 s = functv.vval.v_string;
3075 }
3076 else
3077 s = (char_u *)"";
3078
Bram Moolenaara80faa82020-04-12 19:37:17 +02003079 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003080 funcexe.firstline = curwin->w_cursor.lnum;
3081 funcexe.lastline = curwin->w_cursor.lnum;
3082 funcexe.evaluate = evaluate;
3083 funcexe.partial = pt;
3084 funcexe.selfdict = selfdict;
3085 funcexe.basetv = basetv;
3086 ret = get_func_tv(s, -1, rettv, arg, &funcexe);
3087
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003088 // Clear the funcref afterwards, so that deleting it while
3089 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003090 if (evaluate)
3091 clear_tv(&functv);
3092
3093 return ret;
3094}
3095
3096/*
3097 * Evaluate "->method()".
3098 * "*arg" points to the '-'.
3099 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3100 */
3101 static int
3102eval_lambda(
3103 char_u **arg,
3104 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003105 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003106 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003107{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003108 int evaluate = evalarg != NULL
3109 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003110 typval_T base = *rettv;
3111 int ret;
3112
3113 // Skip over the ->.
3114 *arg += 2;
3115 rettv->v_type = VAR_UNKNOWN;
3116
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003117 ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003118 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003119 return FAIL;
3120 else if (**arg != '(')
3121 {
3122 if (verbose)
3123 {
3124 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003125 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003126 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003127 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003128 }
3129 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003130 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003131 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003132 else
3133 ret = call_func_rettv(arg, rettv, evaluate, NULL, &base);
3134
3135 // Clear the funcref afterwards, so that deleting it while
3136 // evaluating the arguments is possible (see test55).
3137 if (evaluate)
3138 clear_tv(&base);
3139
3140 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003141}
3142
3143/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003144 * Evaluate "->method()".
3145 * "*arg" points to the '-'.
3146 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3147 */
3148 static int
3149eval_method(
3150 char_u **arg,
3151 typval_T *rettv,
3152 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003153 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003154{
3155 char_u *name;
3156 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003157 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003158 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003159 int ret;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003160
3161 // Skip over the ->.
3162 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003163 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003164
Bram Moolenaarac92e252019-08-03 21:58:38 +02003165 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003166 len = get_name_len(arg, &alias, evaluate, TRUE);
3167 if (alias != NULL)
3168 name = alias;
3169
3170 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003171 {
3172 if (verbose)
3173 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003174 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003175 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003176 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003177 {
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003178 if (**arg != '(')
3179 {
3180 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003181 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003182 ret = FAIL;
3183 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003184 else if (VIM_ISWHITE((*arg)[-1]))
3185 {
3186 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003187 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003188 ret = FAIL;
3189 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003190 else
Bram Moolenaar32e35112020-05-14 22:41:15 +02003191 ret = eval_func(arg, name, len, rettv,
3192 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003193 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003194
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003195 // Clear the funcref afterwards, so that deleting it while
3196 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003197 if (evaluate)
3198 clear_tv(&base);
3199
Bram Moolenaarac92e252019-08-03 21:58:38 +02003200 return ret;
3201}
3202
3203/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003204 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3205 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003206 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3207 */
3208 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003209eval_index(
3210 char_u **arg,
3211 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003212 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003213 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003214{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003215 int evaluate = evalarg != NULL
3216 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003217 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003218 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003219 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003220 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003221 long len = -1;
3222 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003223 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003224 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003225
Bram Moolenaara03f2332016-02-06 18:09:59 +01003226 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003227 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003228 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003229 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003230 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003231 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003232 return FAIL;
3233 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003234#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01003235 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003236 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003237 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003238#endif
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003239 case VAR_BOOL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003240 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003241 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003242 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003243 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003244 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003245 return FAIL;
3246 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003247 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003248 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003249 if (evaluate)
3250 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003251 // FALLTHROUGH
Bram Moolenaara03f2332016-02-06 18:09:59 +01003252
3253 case VAR_STRING:
3254 case VAR_NUMBER:
3255 case VAR_LIST:
3256 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003257 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003258 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003259 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003260
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003261 init_tv(&var1);
3262 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003263 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003264 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003265 /*
3266 * dict.name
3267 */
3268 key = *arg + 1;
3269 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
3270 ;
3271 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003272 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003273 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003274 }
3275 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003276 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003277 /*
3278 * something[idx]
3279 *
3280 * Get the (first) variable from inside the [].
3281 */
3282 *arg = skipwhite(*arg + 1);
3283 if (**arg == ':')
3284 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003285 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003286 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003287 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003288 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003289 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003290 clear_tv(&var1);
3291 return FAIL;
3292 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003293
3294 /*
3295 * Get the second variable from inside the [:].
3296 */
3297 if (**arg == ':')
3298 {
3299 range = TRUE;
3300 *arg = skipwhite(*arg + 1);
3301 if (**arg == ']')
3302 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003303 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003304 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003305 if (!empty1)
3306 clear_tv(&var1);
3307 return FAIL;
3308 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003309 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003310 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003311 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003312 if (!empty1)
3313 clear_tv(&var1);
3314 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003315 return FAIL;
3316 }
3317 }
3318
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003319 // Check for the ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00003320 if (**arg != ']')
3321 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003322 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003323 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003324 clear_tv(&var1);
3325 if (range)
3326 clear_tv(&var2);
3327 return FAIL;
3328 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003329 *arg = skipwhite(*arg + 1); // skip the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003330 }
3331
3332 if (evaluate)
3333 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003334 n1 = 0;
3335 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003336 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003337 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003338 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003339 }
3340 if (range)
3341 {
3342 if (empty2)
3343 n2 = -1;
3344 else
3345 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003346 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003347 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003348 }
3349 }
3350
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003351 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003352 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003353 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003354 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003355 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003356 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003357 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003358 case VAR_FLOAT:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003359 case VAR_BOOL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003360 case VAR_SPECIAL:
3361 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003362 case VAR_CHANNEL:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003363 break; // not evaluating, skipping over subscript
Bram Moolenaara03f2332016-02-06 18:09:59 +01003364
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003365 case VAR_NUMBER:
3366 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003367 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003368 len = (long)STRLEN(s);
3369 if (range)
3370 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003371 // The resulting variable is a substring. If the indexes
3372 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003373 if (n1 < 0)
3374 {
3375 n1 = len + n1;
3376 if (n1 < 0)
3377 n1 = 0;
3378 }
3379 if (n2 < 0)
3380 n2 = len + n2;
3381 else if (n2 >= len)
3382 n2 = len;
3383 if (n1 >= len || n2 < 0 || n1 > n2)
3384 s = NULL;
3385 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02003386 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003387 }
3388 else
3389 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003390 // The resulting variable is a string of a single
3391 // character. If the index is too big or negative the
3392 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003393 if (n1 >= len || n1 < 0)
3394 s = NULL;
3395 else
3396 s = vim_strnsave(s + n1, 1);
3397 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003398 clear_tv(rettv);
3399 rettv->v_type = VAR_STRING;
3400 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003401 break;
3402
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003403 case VAR_BLOB:
3404 len = blob_len(rettv->vval.v_blob);
3405 if (range)
3406 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003407 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003408 // are out of range the result is empty.
3409 if (n1 < 0)
3410 {
3411 n1 = len + n1;
3412 if (n1 < 0)
3413 n1 = 0;
3414 }
3415 if (n2 < 0)
3416 n2 = len + n2;
3417 else if (n2 >= len)
3418 n2 = len - 1;
3419 if (n1 >= len || n2 < 0 || n1 > n2)
3420 {
3421 clear_tv(rettv);
3422 rettv->v_type = VAR_BLOB;
3423 rettv->vval.v_blob = NULL;
3424 }
3425 else
3426 {
3427 blob_T *blob = blob_alloc();
3428
3429 if (blob != NULL)
3430 {
3431 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
3432 {
3433 blob_free(blob);
3434 return FAIL;
3435 }
3436 blob->bv_ga.ga_len = n2 - n1 + 1;
3437 for (i = n1; i <= n2; i++)
3438 blob_set(blob, i - n1,
3439 blob_get(rettv->vval.v_blob, i));
3440
3441 clear_tv(rettv);
3442 rettv_blob_set(rettv, blob);
3443 }
3444 }
3445 }
3446 else
3447 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003448 // The resulting variable is a byte value.
3449 // If the index is too big or negative that is an error.
3450 if (n1 < 0)
3451 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003452 if (n1 < len && n1 >= 0)
3453 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003454 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003455
3456 clear_tv(rettv);
3457 rettv->v_type = VAR_NUMBER;
3458 rettv->vval.v_number = v;
3459 }
3460 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003461 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003462 }
3463 break;
3464
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003465 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003466 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003467 if (n1 < 0)
3468 n1 = len + n1;
3469 if (!empty1 && (n1 < 0 || n1 >= len))
3470 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003471 // For a range we allow invalid values and return an empty
3472 // list. A list index out of range is an error.
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003473 if (!range)
3474 {
3475 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003476 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003477 return FAIL;
3478 }
3479 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003480 }
3481 if (range)
3482 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003483 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003484
3485 if (n2 < 0)
3486 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003487 else if (n2 >= len)
3488 n2 = len - 1;
3489 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003490 n2 = -1;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003491 l = list_slice(rettv->vval.v_list, n1, n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003492 if (l == NULL)
3493 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003494 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003495 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003496 }
3497 else
3498 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003499 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003500 clear_tv(rettv);
3501 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003502 }
3503 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003504
3505 case VAR_DICT:
3506 if (range)
3507 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003508 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003509 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003510 if (len == -1)
3511 clear_tv(&var1);
3512 return FAIL;
3513 }
3514 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003515 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003516
3517 if (len == -1)
3518 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003519 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02003520 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003521 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003522 clear_tv(&var1);
3523 return FAIL;
3524 }
3525 }
3526
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003527 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003528
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003529 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003530 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003531 if (len == -1)
3532 clear_tv(&var1);
3533 if (item == NULL)
3534 return FAIL;
3535
3536 copy_tv(&item->di_tv, &var1);
3537 clear_tv(rettv);
3538 *rettv = var1;
3539 }
3540 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003541 }
3542 }
3543
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003544 return OK;
3545}
3546
3547/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02003548 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003549 */
3550 char_u *
3551partial_name(partial_T *pt)
3552{
3553 if (pt->pt_name != NULL)
3554 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02003555 if (pt->pt_func != NULL)
3556 return pt->pt_func->uf_name;
3557 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003558}
3559
Bram Moolenaarddecc252016-04-06 22:59:37 +02003560 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003561partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003562{
3563 int i;
3564
3565 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003566 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003567 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003568 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003569 if (pt->pt_name != NULL)
3570 {
3571 func_unref(pt->pt_name);
3572 vim_free(pt->pt_name);
3573 }
3574 else
3575 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003576
3577 if (pt->pt_funcstack != NULL)
3578 {
3579 // Decrease the reference count for the context of a closure. If down
3580 // to zero free it and clear the variables on the stack.
3581 if (--pt->pt_funcstack->fs_refcount == 0)
3582 {
3583 garray_T *gap = &pt->pt_funcstack->fs_ga;
3584 typval_T *stack = gap->ga_data;
3585
3586 for (i = 0; i < gap->ga_len; ++i)
3587 clear_tv(stack + i);
3588 ga_clear(gap);
3589 vim_free(pt->pt_funcstack);
3590 }
3591 pt->pt_funcstack = NULL;
3592 }
3593
Bram Moolenaarddecc252016-04-06 22:59:37 +02003594 vim_free(pt);
3595}
3596
3597/*
3598 * Unreference a closure: decrement the reference count and free it when it
3599 * becomes zero.
3600 */
3601 void
3602partial_unref(partial_T *pt)
3603{
3604 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003605 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003606}
3607
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003608/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003609 * Return the next (unique) copy ID.
3610 * Used for serializing nested structures.
3611 */
3612 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003613get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003614{
3615 current_copyID += COPYID_INC;
3616 return current_copyID;
3617}
3618
3619/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003620 * Garbage collection for lists and dictionaries.
3621 *
3622 * We use reference counts to be able to free most items right away when they
3623 * are no longer used. But for composite items it's possible that it becomes
3624 * unused while the reference count is > 0: When there is a recursive
3625 * reference. Example:
3626 * :let l = [1, 2, 3]
3627 * :let d = {9: l}
3628 * :let l[1] = d
3629 *
3630 * Since this is quite unusual we handle this with garbage collection: every
3631 * once in a while find out which lists and dicts are not referenced from any
3632 * variable.
3633 *
3634 * Here is a good reference text about garbage collection (refers to Python
3635 * but it applies to all reference-counting mechanisms):
3636 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00003637 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00003638
3639/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003640 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02003641 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003642 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00003643 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003644 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003645garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003646{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003647 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003648 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003649 buf_T *buf;
3650 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01003651 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003652 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003653
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003654 if (!testing)
3655 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003656 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003657 want_garbage_collect = FALSE;
3658 may_garbage_collect = FALSE;
3659 garbage_collect_at_exit = FALSE;
3660 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00003661
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01003662 // The execution stack can grow big, limit the size.
3663 if (exestack.ga_maxlen - exestack.ga_len > 500)
3664 {
3665 size_t new_len;
3666 char_u *pp;
3667 int n;
3668
3669 // Keep 150% of the current size, with a minimum of the growth size.
3670 n = exestack.ga_len / 2;
3671 if (n < exestack.ga_growsize)
3672 n = exestack.ga_growsize;
3673
3674 // Don't make it bigger though.
3675 if (exestack.ga_len + n < exestack.ga_maxlen)
3676 {
3677 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
3678 pp = vim_realloc(exestack.ga_data, new_len);
3679 if (pp == NULL)
3680 return FAIL;
3681 exestack.ga_maxlen = exestack.ga_len + n;
3682 exestack.ga_data = pp;
3683 }
3684 }
3685
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003686 // We advance by two because we add one for items referenced through
3687 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003688 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003689
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003690 /*
3691 * 1. Go through all accessible variables and mark all lists and dicts
3692 * with copyID.
3693 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003694
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003695 // Don't free variables in the previous_funccal list unless they are only
3696 // referenced through previous_funccal. This must be first, because if
3697 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003698 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003699
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003700 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003701 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003702
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003703 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003704 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003705 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
3706 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003707
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003708 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003709 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003710 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3711 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02003712 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003713 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
3714 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003715#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003716 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003717 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3718 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003719 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003720 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003721 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3722 NULL, NULL);
3723#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003724
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003725 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003726 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003727 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
3728 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003729 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003730 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003731
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003732 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003733 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003734
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003735 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003736 abort = abort || set_ref_in_functions(copyID);
3737
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003738 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003739 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003740
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003741 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003742 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00003743
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003744 // callbacks in buffers
3745 abort = abort || set_ref_in_buffers(copyID);
3746
Bram Moolenaar1dced572012-04-05 16:54:08 +02003747#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003748 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02003749#endif
3750
Bram Moolenaardb913952012-06-29 12:54:53 +02003751#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003752 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003753#endif
3754
3755#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003756 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003757#endif
3758
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003759#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02003760 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003761 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003762#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02003763#ifdef FEAT_NETBEANS_INTG
3764 abort = abort || set_ref_in_nb_channel(copyID);
3765#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003766
Bram Moolenaare3188e22016-05-31 21:13:04 +02003767#ifdef FEAT_TIMERS
3768 abort = abort || set_ref_in_timer(copyID);
3769#endif
3770
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02003771#ifdef FEAT_QUICKFIX
3772 abort = abort || set_ref_in_quickfix(copyID);
3773#endif
3774
Bram Moolenaara2c45a12017-07-27 22:14:59 +02003775#ifdef FEAT_TERMINAL
3776 abort = abort || set_ref_in_term(copyID);
3777#endif
3778
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003779#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003780 abort = abort || set_ref_in_popups(copyID);
3781#endif
3782
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003783 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003784 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003785 /*
3786 * 2. Free lists and dictionaries that are not referenced.
3787 */
3788 did_free = free_unref_items(copyID);
3789
3790 /*
3791 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003792 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003793 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003794 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003795 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003796 else if (p_verbose > 0)
3797 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003798 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003799 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003800
3801 return did_free;
3802}
3803
3804/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003805 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003806 */
3807 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003808free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003809{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003810 int did_free = FALSE;
3811
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003812 // Let all "free" functions know that we are here. This means no
3813 // dictionaries, lists, channels or jobs are to be freed, because we will
3814 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003815 in_free_unref_items = TRUE;
3816
3817 /*
3818 * PASS 1: free the contents of the items. We don't free the items
3819 * themselves yet, so that it is possible to decrement refcount counters
3820 */
3821
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003822 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02003823 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003824
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003825 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02003826 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003827
3828#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003829 // Go through the list of jobs and free items without the copyID. This
3830 // must happen before doing channels, because jobs refer to channels, but
3831 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003832 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
3833
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003834 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003835 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
3836#endif
3837
3838 /*
3839 * PASS 2: free the items themselves.
3840 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003841 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02003842 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01003843
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003844#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003845 // Go through the list of jobs and free items without the copyID. This
3846 // must happen before doing channels, because jobs refer to channels, but
3847 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003848 free_unused_jobs(copyID, COPYID_MASK);
3849
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003850 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003851 free_unused_channels(copyID, COPYID_MASK);
3852#endif
3853
3854 in_free_unref_items = FALSE;
3855
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003856 return did_free;
3857}
3858
3859/*
3860 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003861 * "list_stack" is used to add lists to be marked. Can be NULL.
3862 *
3863 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003864 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003865 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003866set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003867{
3868 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003869 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003870 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003871 hashtab_T *cur_ht;
3872 ht_stack_T *ht_stack = NULL;
3873 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003874
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003875 cur_ht = ht;
3876 for (;;)
3877 {
3878 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003879 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003880 // Mark each item in the hashtab. If the item contains a hashtab
3881 // it is added to ht_stack, if it contains a list it is added to
3882 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003883 todo = (int)cur_ht->ht_used;
3884 for (hi = cur_ht->ht_array; todo > 0; ++hi)
3885 if (!HASHITEM_EMPTY(hi))
3886 {
3887 --todo;
3888 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
3889 &ht_stack, list_stack);
3890 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003891 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003892
3893 if (ht_stack == NULL)
3894 break;
3895
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003896 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003897 cur_ht = ht_stack->ht;
3898 tempitem = ht_stack;
3899 ht_stack = ht_stack->prev;
3900 free(tempitem);
3901 }
3902
3903 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003904}
3905
3906/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003907 * Mark a dict and its items with "copyID".
3908 * Returns TRUE if setting references failed somehow.
3909 */
3910 int
3911set_ref_in_dict(dict_T *d, int copyID)
3912{
3913 if (d != NULL && d->dv_copyID != copyID)
3914 {
3915 d->dv_copyID = copyID;
3916 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
3917 }
3918 return FALSE;
3919}
3920
3921/*
3922 * Mark a list and its items with "copyID".
3923 * Returns TRUE if setting references failed somehow.
3924 */
3925 int
3926set_ref_in_list(list_T *ll, int copyID)
3927{
3928 if (ll != NULL && ll->lv_copyID != copyID)
3929 {
3930 ll->lv_copyID = copyID;
3931 return set_ref_in_list_items(ll, copyID, NULL);
3932 }
3933 return FALSE;
3934}
3935
3936/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003937 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003938 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
3939 *
3940 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003941 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003942 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003943set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003944{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003945 listitem_T *li;
3946 int abort = FALSE;
3947 list_T *cur_l;
3948 list_stack_T *list_stack = NULL;
3949 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003950
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003951 cur_l = l;
3952 for (;;)
3953 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003954 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003955 // Mark each item in the list. If the item contains a hashtab
3956 // it is added to ht_stack, if it contains a list it is added to
3957 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003958 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
3959 abort = abort || set_ref_in_item(&li->li_tv, copyID,
3960 ht_stack, &list_stack);
3961 if (list_stack == NULL)
3962 break;
3963
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003964 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003965 cur_l = list_stack->list;
3966 tempitem = list_stack;
3967 list_stack = list_stack->prev;
3968 free(tempitem);
3969 }
3970
3971 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003972}
3973
3974/*
3975 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003976 * "list_stack" is used to add lists to be marked. Can be NULL.
3977 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
3978 *
3979 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003980 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003981 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003982set_ref_in_item(
3983 typval_T *tv,
3984 int copyID,
3985 ht_stack_T **ht_stack,
3986 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003987{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003988 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00003989
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003990 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003991 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003992 dict_T *dd = tv->vval.v_dict;
3993
Bram Moolenaara03f2332016-02-06 18:09:59 +01003994 if (dd != NULL && dd->dv_copyID != copyID)
3995 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003996 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01003997 dd->dv_copyID = copyID;
3998 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003999 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004000 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4001 }
4002 else
4003 {
4004 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
4005 if (newitem == NULL)
4006 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004007 else
4008 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004009 newitem->ht = &dd->dv_hashtab;
4010 newitem->prev = *ht_stack;
4011 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004012 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004013 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004014 }
4015 }
4016 else if (tv->v_type == VAR_LIST)
4017 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004018 list_T *ll = tv->vval.v_list;
4019
Bram Moolenaara03f2332016-02-06 18:09:59 +01004020 if (ll != NULL && ll->lv_copyID != copyID)
4021 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004022 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004023 ll->lv_copyID = copyID;
4024 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004025 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004026 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004027 }
4028 else
4029 {
4030 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004031 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004032 if (newitem == NULL)
4033 abort = TRUE;
4034 else
4035 {
4036 newitem->list = ll;
4037 newitem->prev = *list_stack;
4038 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004039 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004040 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004041 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004042 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004043 else if (tv->v_type == VAR_FUNC)
4044 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004045 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004046 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004047 else if (tv->v_type == VAR_PARTIAL)
4048 {
4049 partial_T *pt = tv->vval.v_partial;
4050 int i;
4051
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004052 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004053 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004054 // Didn't see this partial yet.
4055 pt->pt_copyID = copyID;
4056
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004057 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004058
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004059 if (pt->pt_dict != NULL)
4060 {
4061 typval_T dtv;
4062
4063 dtv.v_type = VAR_DICT;
4064 dtv.vval.v_dict = pt->pt_dict;
4065 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4066 }
4067
4068 for (i = 0; i < pt->pt_argc; ++i)
4069 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4070 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004071 if (pt->pt_funcstack != NULL)
4072 {
4073 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4074
4075 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4076 abort = abort || set_ref_in_item(stack + i, copyID,
4077 ht_stack, list_stack);
4078 }
4079
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004080 }
4081 }
4082#ifdef FEAT_JOB_CHANNEL
4083 else if (tv->v_type == VAR_JOB)
4084 {
4085 job_T *job = tv->vval.v_job;
4086 typval_T dtv;
4087
4088 if (job != NULL && job->jv_copyID != copyID)
4089 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004090 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004091 if (job->jv_channel != NULL)
4092 {
4093 dtv.v_type = VAR_CHANNEL;
4094 dtv.vval.v_channel = job->jv_channel;
4095 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4096 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004097 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004098 {
4099 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004100 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004101 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4102 }
4103 }
4104 }
4105 else if (tv->v_type == VAR_CHANNEL)
4106 {
4107 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004108 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004109 typval_T dtv;
4110 jsonq_T *jq;
4111 cbq_T *cq;
4112
4113 if (ch != NULL && ch->ch_copyID != copyID)
4114 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004115 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004116 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004117 {
4118 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4119 jq = jq->jq_next)
4120 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4121 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4122 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004123 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004124 {
4125 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004126 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004127 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4128 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004129 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004130 {
4131 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004132 dtv.vval.v_partial =
4133 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004134 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4135 }
4136 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004137 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004138 {
4139 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004140 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004141 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4142 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004143 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004144 {
4145 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004146 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004147 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4148 }
4149 }
4150 }
4151#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004152 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004153}
4154
Bram Moolenaar8c711452005-01-14 21:53:12 +00004155/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004156 * Return a string with the string representation of a variable.
4157 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004158 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004159 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004160 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4161 * stings as "string()", otherwise does not put quotes around strings, as
4162 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004163 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4164 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004165 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004166 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004167 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004168echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004169 typval_T *tv,
4170 char_u **tofree,
4171 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004172 int copyID,
4173 int echo_style,
4174 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004175 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004176{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004177 static int recurse = 0;
4178 char_u *r = NULL;
4179
Bram Moolenaar33570922005-01-25 22:26:29 +00004180 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004181 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004182 if (!did_echo_string_emsg)
4183 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004184 // Only give this message once for a recursive call to avoid
4185 // flooding the user with errors. And stop iterating over lists
4186 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004187 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004188 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004189 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004190 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004191 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004192 }
4193 ++recurse;
4194
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004195 switch (tv->v_type)
4196 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004197 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004198 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004199 {
4200 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004201 r = tv->vval.v_string;
4202 if (r == NULL)
4203 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004204 }
4205 else
4206 {
4207 *tofree = string_quote(tv->vval.v_string, FALSE);
4208 r = *tofree;
4209 }
4210 break;
4211
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004212 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004213 if (echo_style)
4214 {
4215 *tofree = NULL;
4216 r = tv->vval.v_string;
4217 }
4218 else
4219 {
4220 *tofree = string_quote(tv->vval.v_string, TRUE);
4221 r = *tofree;
4222 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004223 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004224
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004225 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004226 {
4227 partial_T *pt = tv->vval.v_partial;
4228 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004229 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004230 garray_T ga;
4231 int i;
4232 char_u *tf;
4233
4234 ga_init2(&ga, 1, 100);
4235 ga_concat(&ga, (char_u *)"function(");
4236 if (fname != NULL)
4237 {
4238 ga_concat(&ga, fname);
4239 vim_free(fname);
4240 }
4241 if (pt != NULL && pt->pt_argc > 0)
4242 {
4243 ga_concat(&ga, (char_u *)", [");
4244 for (i = 0; i < pt->pt_argc; ++i)
4245 {
4246 if (i > 0)
4247 ga_concat(&ga, (char_u *)", ");
4248 ga_concat(&ga,
4249 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4250 vim_free(tf);
4251 }
4252 ga_concat(&ga, (char_u *)"]");
4253 }
4254 if (pt != NULL && pt->pt_dict != NULL)
4255 {
4256 typval_T dtv;
4257
4258 ga_concat(&ga, (char_u *)", ");
4259 dtv.v_type = VAR_DICT;
4260 dtv.vval.v_dict = pt->pt_dict;
4261 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4262 vim_free(tf);
4263 }
4264 ga_concat(&ga, (char_u *)")");
4265
4266 *tofree = ga.ga_data;
4267 r = *tofree;
4268 break;
4269 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004270
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004271 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004272 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004273 break;
4274
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004275 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004276 if (tv->vval.v_list == NULL)
4277 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004278 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004279 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004280 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004281 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004282 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4283 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004284 {
4285 *tofree = NULL;
4286 r = (char_u *)"[...]";
4287 }
4288 else
4289 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004290 int old_copyID = tv->vval.v_list->lv_copyID;
4291
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004292 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004293 *tofree = list2string(tv, copyID, restore_copyID);
4294 if (restore_copyID)
4295 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004296 r = *tofree;
4297 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004298 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004299
Bram Moolenaar8c711452005-01-14 21:53:12 +00004300 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004301 if (tv->vval.v_dict == NULL)
4302 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004303 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004304 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004305 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004306 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004307 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4308 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004309 {
4310 *tofree = NULL;
4311 r = (char_u *)"{...}";
4312 }
4313 else
4314 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004315 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004316
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004317 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004318 *tofree = dict2string(tv, copyID, restore_copyID);
4319 if (restore_copyID)
4320 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004321 r = *tofree;
4322 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004323 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004324
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004325 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004326 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004327 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004328 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004329 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004330 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004331 break;
4332
Bram Moolenaar835dc632016-02-07 14:27:38 +01004333 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004334 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004335 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004336 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004337 if (composite_val)
4338 {
4339 *tofree = string_quote(r, FALSE);
4340 r = *tofree;
4341 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004342 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004343
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004344 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004345#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004346 *tofree = NULL;
4347 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4348 r = numbuf;
4349 break;
4350#endif
4351
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004352 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004353 case VAR_SPECIAL:
4354 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004355 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004356 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004357 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004358
Bram Moolenaar8502c702014-06-17 12:51:16 +02004359 if (--recurse == 0)
4360 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004361 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004362}
4363
4364/*
4365 * Return a string with the string representation of a variable.
4366 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4367 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004368 * Does not put quotes around strings, as ":echo" displays values.
4369 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4370 * May return NULL.
4371 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004372 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004373echo_string(
4374 typval_T *tv,
4375 char_u **tofree,
4376 char_u *numbuf,
4377 int copyID)
4378{
4379 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4380}
4381
4382/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004383 * Return string "str" in ' quotes, doubling ' characters.
4384 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004385 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004386 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004387 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004388string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004389{
Bram Moolenaar33570922005-01-25 22:26:29 +00004390 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004391 char_u *p, *r, *s;
4392
Bram Moolenaar33570922005-01-25 22:26:29 +00004393 len = (function ? 13 : 3);
4394 if (str != NULL)
4395 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004396 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004397 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004398 if (*p == '\'')
4399 ++len;
4400 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004401 s = r = alloc(len);
4402 if (r != NULL)
4403 {
4404 if (function)
4405 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004406 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004407 r += 10;
4408 }
4409 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004410 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004411 if (str != NULL)
4412 for (p = str; *p != NUL; )
4413 {
4414 if (*p == '\'')
4415 *r++ = '\'';
4416 MB_COPY_CHAR(p, r);
4417 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004418 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004419 if (function)
4420 *r++ = ')';
4421 *r++ = NUL;
4422 }
4423 return s;
4424}
4425
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004426#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004427/*
4428 * Convert the string "text" to a floating point number.
4429 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4430 * this always uses a decimal point.
4431 * Returns the length of the text that was consumed.
4432 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004433 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004434string2float(
4435 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004436 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004437{
4438 char *s = (char *)text;
4439 float_T f;
4440
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004441 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004442 if (STRNICMP(text, "inf", 3) == 0)
4443 {
4444 *value = INFINITY;
4445 return 3;
4446 }
4447 if (STRNICMP(text, "-inf", 3) == 0)
4448 {
4449 *value = -INFINITY;
4450 return 4;
4451 }
4452 if (STRNICMP(text, "nan", 3) == 0)
4453 {
4454 *value = NAN;
4455 return 3;
4456 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004457 f = strtod(s, &s);
4458 *value = f;
4459 return (int)((char_u *)s - text);
4460}
4461#endif
4462
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004463/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004465 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004467 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004468var2fpos(
4469 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004470 int dollar_lnum, // TRUE when $ is last line
4471 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004473 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004475 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004477 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004478 if (varp->v_type == VAR_LIST)
4479 {
4480 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004481 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004482 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004483 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004484
4485 l = varp->vval.v_list;
4486 if (l == NULL)
4487 return NULL;
4488
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004489 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004490 pos.lnum = list_find_nr(l, 0L, &error);
4491 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004492 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004493
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004494 // Get the column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004495 pos.col = list_find_nr(l, 1L, &error);
4496 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004497 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004498 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004499
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004500 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004501 li = list_find(l, 1L);
4502 if (li != NULL && li->li_tv.v_type == VAR_STRING
4503 && li->li_tv.vval.v_string != NULL
4504 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
4505 pos.col = len + 1;
4506
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004507 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004508 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004509 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004510 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004511
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004512 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004513 pos.coladd = list_find_nr(l, 2L, &error);
4514 if (error)
4515 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004516
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004517 return &pos;
4518 }
4519
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004520 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004521 if (name == NULL)
4522 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004523 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004525 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004526 {
4527 if (VIsual_active)
4528 return &VIsual;
4529 return &curwin->w_cursor;
4530 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004531 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004533 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4535 return NULL;
4536 return pp;
4537 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004538
Bram Moolenaara5525202006-03-02 22:52:09 +00004539 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004540
Bram Moolenaar477933c2007-07-17 14:32:23 +00004541 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004542 {
4543 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004544 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004545 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004546 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004547 // In silent Ex mode topline is zero, but that's not a valid line
4548 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004549 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004550 return &pos;
4551 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004552 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004553 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004554 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004555 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004556 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004557 return &pos;
4558 }
4559 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004560 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004562 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 {
4564 pos.lnum = curbuf->b_ml.ml_line_count;
4565 pos.col = 0;
4566 }
4567 else
4568 {
4569 pos.lnum = curwin->w_cursor.lnum;
4570 pos.col = (colnr_T)STRLEN(ml_get_curline());
4571 }
4572 return &pos;
4573 }
4574 return NULL;
4575}
4576
4577/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004578 * Convert list in "arg" into a position and optional file number.
4579 * When "fnump" is NULL there is no file number, only 3 items.
4580 * Note that the column is passed on as-is, the caller may want to decrement
4581 * it to use 1 for the first column.
4582 * Return FAIL when conversion is not possible, doesn't check the position for
4583 * validity.
4584 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004585 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004586list2fpos(
4587 typval_T *arg,
4588 pos_T *posp,
4589 int *fnump,
4590 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004591{
4592 list_T *l = arg->vval.v_list;
4593 long i = 0;
4594 long n;
4595
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004596 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
4597 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00004598 if (arg->v_type != VAR_LIST
4599 || l == NULL
4600 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02004601 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004602 return FAIL;
4603
4604 if (fnump != NULL)
4605 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004606 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004607 if (n < 0)
4608 return FAIL;
4609 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004610 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004611 *fnump = n;
4612 }
4613
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004614 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004615 if (n < 0)
4616 return FAIL;
4617 posp->lnum = n;
4618
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004619 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004620 if (n < 0)
4621 return FAIL;
4622 posp->col = n;
4623
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004624 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004625 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00004626 posp->coladd = 0;
4627 else
4628 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004629
Bram Moolenaar493c1782014-05-28 14:34:46 +02004630 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004631 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02004632
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004633 return OK;
4634}
4635
4636/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 * Get the length of an environment variable name.
4638 * Advance "arg" to the first character after the name.
4639 * Return 0 for error.
4640 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004641 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004642get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643{
4644 char_u *p;
4645 int len;
4646
4647 for (p = *arg; vim_isIDc(*p); ++p)
4648 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004649 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 return 0;
4651
4652 len = (int)(p - *arg);
4653 *arg = p;
4654 return len;
4655}
4656
4657/*
4658 * Get the length of the name of a function or internal variable.
4659 * "arg" is advanced to the first non-white character after the name.
4660 * Return 0 if something is wrong.
4661 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004662 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004663get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664{
4665 char_u *p;
4666 int len;
4667
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004668 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004670 {
4671 if (*p == ':')
4672 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004673 // "s:" is start of "s:var", but "n:" is not and can be used in
4674 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004675 len = (int)(p - *arg);
4676 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
4677 || len > 1)
4678 break;
4679 }
4680 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004681 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 return 0;
4683
4684 len = (int)(p - *arg);
4685 *arg = skipwhite(p);
4686
4687 return len;
4688}
4689
4690/*
Bram Moolenaara7043832005-01-21 11:56:39 +00004691 * Get the length of the name of a variable or function.
4692 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004694 * Return -1 if curly braces expansion failed.
4695 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 * If the name contains 'magic' {}'s, expand them and return the
4697 * expanded name in an allocated string via 'alias' - caller must free.
4698 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004699 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004700get_name_len(
4701 char_u **arg,
4702 char_u **alias,
4703 int evaluate,
4704 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705{
4706 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 char_u *p;
4708 char_u *expr_start;
4709 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004711 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712
4713 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
4714 && (*arg)[2] == (int)KE_SNR)
4715 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004716 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 *arg += 3;
4718 return get_id_len(arg) + 3;
4719 }
4720 len = eval_fname_script(*arg);
4721 if (len > 0)
4722 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004723 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 *arg += len;
4725 }
4726
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004728 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004730 p = find_name_end(*arg, &expr_start, &expr_end,
4731 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 if (expr_start != NULL)
4733 {
4734 char_u *temp_string;
4735
4736 if (!evaluate)
4737 {
4738 len += (int)(p - *arg);
4739 *arg = skipwhite(p);
4740 return len;
4741 }
4742
4743 /*
4744 * Include any <SID> etc in the expanded string:
4745 * Thus the -len here.
4746 */
4747 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
4748 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004749 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 *alias = temp_string;
4751 *arg = skipwhite(p);
4752 return (int)STRLEN(temp_string);
4753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754
4755 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01004756 // Only give an error when there is something, otherwise it will be
4757 // reported at a higher level.
4758 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004759 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760
4761 return len;
4762}
4763
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004764/*
4765 * Find the end of a variable or function name, taking care of magic braces.
4766 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
4767 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004768 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004769 * Return a pointer to just after the name. Equal to "arg" if there is no
4770 * valid name.
4771 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004772 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004773find_name_end(
4774 char_u *arg,
4775 char_u **expr_start,
4776 char_u **expr_end,
4777 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004779 int mb_nest = 0;
4780 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004782 int len;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004783 int vim9script = current_sctx.sc_version == SCRIPT_VERSION_VIM9;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004785 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004787 *expr_start = NULL;
4788 *expr_end = NULL;
4789 }
4790
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004791 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004792 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
4793 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004794 return arg;
4795
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004796 for (p = arg; *p != NUL
4797 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004798 || (*p == '{' && !vim9script)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004799 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004800 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004801 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004802 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00004803 if (*p == '\'')
4804 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004805 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004806 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00004807 ;
4808 if (*p == NUL)
4809 break;
4810 }
4811 else if (*p == '"')
4812 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004813 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004814 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00004815 if (*p == '\\' && p[1] != NUL)
4816 ++p;
4817 if (*p == NUL)
4818 break;
4819 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004820 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
4821 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004822 // "s:" is start of "s:var", but "n:" is not and can be used in
4823 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004824 len = (int)(p - arg);
4825 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01004826 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004827 break;
4828 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00004829
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004830 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004832 if (*p == '[')
4833 ++br_nest;
4834 else if (*p == ']')
4835 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00004837
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004838 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004840 if (*p == '{')
4841 {
4842 mb_nest++;
4843 if (expr_start != NULL && *expr_start == NULL)
4844 *expr_start = p;
4845 }
4846 else if (*p == '}')
4847 {
4848 mb_nest--;
4849 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
4850 *expr_end = p;
4851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 }
4854
4855 return p;
4856}
4857
4858/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004859 * Expands out the 'magic' {}'s in a variable/function name.
4860 * Note that this can call itself recursively, to deal with
4861 * constructs like foo{bar}{baz}{bam}
4862 * The four pointer arguments point to "foo{expre}ss{ion}bar"
4863 * "in_start" ^
4864 * "expr_start" ^
4865 * "expr_end" ^
4866 * "in_end" ^
4867 *
4868 * Returns a new allocated string, which the caller must free.
4869 * Returns NULL for failure.
4870 */
4871 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004872make_expanded_name(
4873 char_u *in_start,
4874 char_u *expr_start,
4875 char_u *expr_end,
4876 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004877{
4878 char_u c1;
4879 char_u *retval = NULL;
4880 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004881
4882 if (expr_end == NULL || in_end == NULL)
4883 return NULL;
4884 *expr_start = NUL;
4885 *expr_end = NUL;
4886 c1 = *in_end;
4887 *in_end = NUL;
4888
Bram Moolenaarb171fb12020-06-24 20:34:03 +02004889 temp_result = eval_to_string(expr_start + 1, FALSE);
4890 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004891 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02004892 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
4893 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004894 if (retval != NULL)
4895 {
4896 STRCPY(retval, in_start);
4897 STRCAT(retval, temp_result);
4898 STRCAT(retval, expr_end + 1);
4899 }
4900 }
4901 vim_free(temp_result);
4902
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004903 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004904 *expr_start = '{';
4905 *expr_end = '}';
4906
4907 if (retval != NULL)
4908 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004909 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004910 if (expr_start != NULL)
4911 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004912 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004913 temp_result = make_expanded_name(retval, expr_start,
4914 expr_end, temp_result);
4915 vim_free(retval);
4916 retval = temp_result;
4917 }
4918 }
4919
4920 return retval;
4921}
4922
4923/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00004925 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004927 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004928eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004930 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
4931}
4932
4933/*
4934 * Return TRUE if character "c" can be used as the first character in a
4935 * variable or function name (excluding '{' and '}').
4936 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004937 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004938eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004939{
4940 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941}
4942
4943/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004944 * Handle:
4945 * - expr[expr], expr[expr:expr] subscript
4946 * - ".name" lookup
4947 * - function call with Funcref variable: func(expr)
4948 * - method call: var->method()
4949 *
4950 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004951 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004952 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004953handle_subscript(
4954 char_u **arg,
4955 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004956 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004957 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004958{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004959 int evaluate = evalarg != NULL
4960 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004961 int ret = OK;
4962 dict_T *selfdict = NULL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004963
Bram Moolenaar61343f02019-07-20 21:11:13 +02004964 // "." is ".name" lookup when we found a dict or when evaluating and
4965 // scriptversion is at least 2, where string concatenation is "..".
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004966 while (ret == OK
Bram Moolenaarac92e252019-08-03 21:58:38 +02004967 && (((**arg == '['
4968 || (**arg == '.' && (rettv->v_type == VAR_DICT
Bram Moolenaar61343f02019-07-20 21:11:13 +02004969 || (!evaluate
4970 && (*arg)[1] != '.'
4971 && current_sctx.sc_version >= 2)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02004972 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004973 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02004974 && !VIM_ISWHITE(*(*arg - 1)))
4975 || (**arg == '-' && (*arg)[1] == '>')))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004976 {
4977 if (**arg == '(')
4978 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004979 ret = call_func_rettv(arg, rettv, evaluate, selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01004980
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004981 // Stop the expression evaluation when immediately aborting on
4982 // error, or when an interrupt occurred or an exception was thrown
4983 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004984 if (aborting())
4985 {
4986 if (ret == OK)
4987 clear_tv(rettv);
4988 ret = FAIL;
4989 }
4990 dict_unref(selfdict);
4991 selfdict = NULL;
4992 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004993 else if (**arg == '-')
4994 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004995 if (ret == OK)
4996 {
4997 if ((*arg)[2] == '{')
4998 // expr->{lambda}()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004999 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005000 else
5001 // expr->name()
5002 ret = eval_method(arg, rettv, evaluate, verbose);
5003 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005004 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005005 else // **arg == '[' || **arg == '.'
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005006 {
5007 dict_unref(selfdict);
5008 if (rettv->v_type == VAR_DICT)
5009 {
5010 selfdict = rettv->vval.v_dict;
5011 if (selfdict != NULL)
5012 ++selfdict->dv_refcount;
5013 }
5014 else
5015 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005016 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005017 {
5018 clear_tv(rettv);
5019 ret = FAIL;
5020 }
5021 }
5022 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005023
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005024 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5025 // Don't do this when "Func" is already a partial that was bound
5026 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005027 if (selfdict != NULL
5028 && (rettv->v_type == VAR_FUNC
5029 || (rettv->v_type == VAR_PARTIAL
5030 && (rettv->vval.v_partial->pt_auto
5031 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005032 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005033
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005034 dict_unref(selfdict);
5035 return ret;
5036}
5037
5038/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005039 * Make a copy of an item.
5040 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005041 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5042 * reference to an already copied list/dict can be used.
5043 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005044 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005045 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005046item_copy(
5047 typval_T *from,
5048 typval_T *to,
5049 int deep,
5050 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005051{
5052 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005053 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005054
Bram Moolenaar33570922005-01-25 22:26:29 +00005055 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005056 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005057 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005058 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005059 }
5060 ++recurse;
5061
5062 switch (from->v_type)
5063 {
5064 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005065 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005066 case VAR_STRING:
5067 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005068 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005069 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005070 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005071 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005072 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005073 copy_tv(from, to);
5074 break;
5075 case VAR_LIST:
5076 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005077 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005078 if (from->vval.v_list == NULL)
5079 to->vval.v_list = NULL;
5080 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5081 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005082 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005083 to->vval.v_list = from->vval.v_list->lv_copylist;
5084 ++to->vval.v_list->lv_refcount;
5085 }
5086 else
5087 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5088 if (to->vval.v_list == NULL)
5089 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005090 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005091 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005092 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005093 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005094 case VAR_DICT:
5095 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005096 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005097 if (from->vval.v_dict == NULL)
5098 to->vval.v_dict = NULL;
5099 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5100 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005101 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005102 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5103 ++to->vval.v_dict->dv_refcount;
5104 }
5105 else
5106 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5107 if (to->vval.v_dict == NULL)
5108 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005109 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005110 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005111 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005112 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005113 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005114 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005115 }
5116 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005117 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005118}
5119
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005120 void
5121echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5122{
5123 char_u *tofree;
5124 char_u numbuf[NUMBUFLEN];
5125 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5126
5127 if (*atstart)
5128 {
5129 *atstart = FALSE;
5130 // Call msg_start() after eval1(), evaluating the expression
5131 // may cause a message to appear.
5132 if (with_space)
5133 {
5134 // Mark the saved text as finishing the line, so that what
5135 // follows is displayed on a new line when scrolling back
5136 // at the more prompt.
5137 msg_sb_eol();
5138 msg_start();
5139 }
5140 }
5141 else if (with_space)
5142 msg_puts_attr(" ", echo_attr);
5143
5144 if (p != NULL)
5145 for ( ; *p != NUL && !got_int; ++p)
5146 {
5147 if (*p == '\n' || *p == '\r' || *p == TAB)
5148 {
5149 if (*p != TAB && *needclr)
5150 {
5151 // remove any text still there from the command
5152 msg_clr_eos();
5153 *needclr = FALSE;
5154 }
5155 msg_putchar_attr(*p, echo_attr);
5156 }
5157 else
5158 {
5159 if (has_mbyte)
5160 {
5161 int i = (*mb_ptr2len)(p);
5162
5163 (void)msg_outtrans_len_attr(p, i, echo_attr);
5164 p += i - 1;
5165 }
5166 else
5167 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5168 }
5169 }
5170 vim_free(tofree);
5171}
5172
Bram Moolenaare9a41262005-01-15 22:18:47 +00005173/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 * ":echo expr1 ..." print each argument separated with a space, add a
5175 * newline at the end.
5176 * ":echon expr1 ..." print each argument plain.
5177 */
5178 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005179ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180{
5181 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005182 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 char_u *p;
5184 int needclr = TRUE;
5185 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01005186 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005187 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005188 evalarg_T evalarg;
5189
5190 CLEAR_FIELD(evalarg);
5191 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192
5193 if (eap->skip)
5194 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005195 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005197 // If eval1() causes an error message the text from the command may
5198 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005199 need_clr_eos = needclr;
5200
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005202 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 {
5204 /*
5205 * Report the invalid expression unless the expression evaluation
5206 * has been cancelled due to an aborting error, an interrupt, or an
5207 * exception.
5208 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005209 if (!aborting() && did_emsg == did_emsg_before
5210 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005211 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005212 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 break;
5214 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005215 need_clr_eos = FALSE;
5216
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005218 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005219
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005220 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 arg = skipwhite(arg);
5222 }
5223 eap->nextcmd = check_nextcmd(arg);
5224
5225 if (eap->skip)
5226 --emsg_skip;
5227 else
5228 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005229 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 if (needclr)
5231 msg_clr_eos();
5232 if (eap->cmdidx == CMD_echo)
5233 msg_end();
5234 }
5235}
5236
5237/*
5238 * ":echohl {name}".
5239 */
5240 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005241ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005243 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244}
5245
5246/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005247 * Returns the :echo attribute
5248 */
5249 int
5250get_echo_attr(void)
5251{
5252 return echo_attr;
5253}
5254
5255/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 * ":execute expr1 ..." execute the result of an expression.
5257 * ":echomsg expr1 ..." Print a message
5258 * ":echoerr expr1 ..." Print an error
5259 * Each gets spaces around each argument and a newline at the end for
5260 * echo commands
5261 */
5262 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005263ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264{
5265 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005266 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 int ret = OK;
5268 char_u *p;
5269 garray_T ga;
5270 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271
5272 ga_init2(&ga, 1, 80);
5273
5274 if (eap->skip)
5275 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02005276 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01005278 ret = eval1_emsg(&arg, &rettv, !eap->skip);
5279 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281
5282 if (!eap->skip)
5283 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005284 char_u buf[NUMBUFLEN];
5285
5286 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01005287 {
5288 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
5289 {
5290 emsg(_(e_inval_string));
5291 p = NULL;
5292 }
5293 else
5294 p = tv_get_string_buf(&rettv, buf);
5295 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005296 else
5297 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005298 if (p == NULL)
5299 {
5300 clear_tv(&rettv);
5301 ret = FAIL;
5302 break;
5303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 len = (int)STRLEN(p);
5305 if (ga_grow(&ga, len + 2) == FAIL)
5306 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005307 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 ret = FAIL;
5309 break;
5310 }
5311 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 ga.ga_len += len;
5315 }
5316
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005317 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 arg = skipwhite(arg);
5319 }
5320
5321 if (ret != FAIL && ga.ga_data != NULL)
5322 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005323 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
5324 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005325 // Mark the already saved text as finishing the line, so that what
5326 // follows is displayed on a new line when scrolling back at the
5327 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005328 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005329 }
5330
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005332 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005333 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005334 out_flush();
5335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 else if (eap->cmdidx == CMD_echoerr)
5337 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005338 int save_did_emsg = did_emsg;
5339
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005340 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005341 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 if (!force_abort)
5343 did_emsg = save_did_emsg;
5344 }
5345 else if (eap->cmdidx == CMD_execute)
5346 do_cmdline((char_u *)ga.ga_data,
5347 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
5348 }
5349
5350 ga_clear(&ga);
5351
5352 if (eap->skip)
5353 --emsg_skip;
5354
5355 eap->nextcmd = check_nextcmd(arg);
5356}
5357
5358/*
5359 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
5360 * "arg" points to the "&" or '+' when called, to "option" when returning.
5361 * Returns NULL when no option name found. Otherwise pointer to the char
5362 * after the option name.
5363 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005364 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005365find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366{
5367 char_u *p = *arg;
5368
5369 ++p;
5370 if (*p == 'g' && p[1] == ':')
5371 {
5372 *opt_flags = OPT_GLOBAL;
5373 p += 2;
5374 }
5375 else if (*p == 'l' && p[1] == ':')
5376 {
5377 *opt_flags = OPT_LOCAL;
5378 p += 2;
5379 }
5380 else
5381 *opt_flags = 0;
5382
5383 if (!ASCII_ISALPHA(*p))
5384 return NULL;
5385 *arg = p;
5386
5387 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005388 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 else
5390 while (ASCII_ISALPHA(*p))
5391 ++p;
5392 return p;
5393}
5394
5395/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00005396 * Display script name where an item was last set.
5397 * Should only be invoked when 'verbose' is non-zero.
5398 */
5399 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005400last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005401{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005402 char_u *p;
5403
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005404 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005405 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005406 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005407 if (p != NULL)
5408 {
5409 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01005410 msg_puts(_("\n\tLast set from "));
5411 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005412 if (script_ctx.sc_lnum > 0)
5413 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01005414 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005415 msg_outnum((long)script_ctx.sc_lnum);
5416 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005417 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005418 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005419 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00005420 }
5421}
5422
Bram Moolenaarb005cd82019-09-04 15:54:55 +02005423#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424
5425/*
5426 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005427 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 * "flags" can be "g" to do a global substitute.
5429 * Returns an allocated string, NULL for error.
5430 */
5431 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005432do_string_sub(
5433 char_u *str,
5434 char_u *pat,
5435 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005436 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01005437 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438{
5439 int sublen;
5440 regmatch_T regmatch;
5441 int i;
5442 int do_all;
5443 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005444 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 garray_T ga;
5446 char_u *ret;
5447 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005448 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005450 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005452 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453
5454 ga_init2(&ga, 1, 200);
5455
5456 do_all = (flags[0] == 'g');
5457
5458 regmatch.rm_ic = p_ic;
5459 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
5460 if (regmatch.regprog != NULL)
5461 {
5462 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005463 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
5465 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005466 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01005467 if (regmatch.startp[0] == regmatch.endp[0])
5468 {
5469 if (zero_width == regmatch.startp[0])
5470 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005471 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02005472 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02005473 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
5474 (size_t)i);
5475 ga.ga_len += i;
5476 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005477 continue;
5478 }
5479 zero_width = regmatch.startp[0];
5480 }
5481
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 /*
5483 * Get some space for a temporary buffer to do the substitution
5484 * into. It will contain:
5485 * - The text up to where the match is.
5486 * - The substituted text.
5487 * - The text after the match.
5488 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005489 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01005490 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
5492 {
5493 ga_clear(&ga);
5494 break;
5495 }
5496
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005497 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 i = (int)(regmatch.startp[0] - tail);
5499 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005500 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005501 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 + ga.ga_len + i, TRUE, TRUE, FALSE);
5503 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02005504 tail = regmatch.endp[0];
5505 if (*tail == NUL)
5506 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 if (!do_all)
5508 break;
5509 }
5510
5511 if (ga.ga_data != NULL)
5512 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
5513
Bram Moolenaar473de612013-06-08 18:19:48 +02005514 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 }
5516
5517 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
5518 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005519 if (p_cpo == empty_option)
5520 p_cpo = save_cpo;
5521 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005522 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005523 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524
5525 return ret;
5526}