blob: ca98d2eb0213d0fb60f760b782efb4edb7939758 [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
Bram Moolenaar9215f012020-06-27 21:18:00 +02001874 char_u *
1875skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
1876{
1877 int getnext;
1878 char_u *p = skipwhite(arg);
1879
1880 eval_next_non_blank(p, evalarg, &getnext);
1881 if (getnext)
1882 return eval_next_line(evalarg);
1883 return p;
1884}
1885
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001886/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001888 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
1890 */
1891
1892/*
1893 * Handle zero level expression.
1894 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001895 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001896 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001897 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 * Return OK or FAIL.
1899 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001900 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001901eval0(
1902 char_u *arg,
1903 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001904 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001905 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906{
1907 int ret;
1908 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001909 int did_emsg_before = did_emsg;
1910 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001911 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912
1913 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001914 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001915
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001916 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 {
1918 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001919 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 /*
1921 * Report the invalid expression unless the expression evaluation has
1922 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001923 * exception, or we already gave a more specific error.
1924 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02001926 if (!aborting()
1927 && did_emsg == did_emsg_before
1928 && called_emsg == called_emsg_before
1929 && (flags & EVAL_CONSTANT) == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001930 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 ret = FAIL;
1932 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001933
1934 if (eap != NULL)
1935 eap->nextcmd = check_nextcmd(p);
1936
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001937 if (evalarg != NULL && eap != NULL && evalarg->eval_tofree != NULL)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001938 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001939 // We may need to keep the original command line, e.g. for
1940 // ":let" it has the variable names. But we may also need the
1941 // new one, "nextcmd" points into it. Keep both.
1942 vim_free(eap->cmdline_tofree);
1943 eap->cmdline_tofree = *eap->cmdlinep;
1944 *eap->cmdlinep = evalarg->eval_tofree;
1945 evalarg->eval_tofree = NULL;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947
1948 return ret;
1949}
1950
1951/*
1952 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00001953 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 *
1955 * "arg" must point to the first non-white of the expression.
1956 * "arg" is advanced to the next non-white after the recognized expression.
1957 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00001958 * Note: "rettv.v_lock" is not set.
1959 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 * Return OK or FAIL.
1961 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02001962 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001963eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964{
Bram Moolenaar793648f2020-06-26 21:28:25 +02001965 char_u *p;
1966 int getnext;
1967
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 /*
1969 * Get the first variable.
1970 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001971 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 return FAIL;
1973
Bram Moolenaar793648f2020-06-26 21:28:25 +02001974 p = eval_next_non_blank(*arg, evalarg, &getnext);
1975 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001977 int result;
1978 typval_T var2;
1979 evalarg_T nested_evalarg;
1980 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02001981 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001982
Bram Moolenaar793648f2020-06-26 21:28:25 +02001983 if (getnext)
1984 *arg = eval_next_line(evalarg);
1985
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001986 if (evalarg == NULL)
1987 {
1988 CLEAR_FIELD(nested_evalarg);
1989 orig_flags = 0;
1990 }
1991 else
1992 {
1993 nested_evalarg = *evalarg;
1994 orig_flags = evalarg->eval_flags;
1995 }
1996
Bram Moolenaar7acde512020-06-24 23:02:40 +02001997 evaluate = nested_evalarg.eval_flags & EVAL_EVALUATE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001999 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002001 int error = FALSE;
2002
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002003 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002005 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002006 if (error)
2007 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 }
2009
2010 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002011 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002013 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002014 nested_evalarg.eval_flags = result ? orig_flags
2015 : orig_flags & ~EVAL_EVALUATE;
2016 if (eval1(arg, rettv, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 return FAIL;
2018
2019 /*
2020 * Check for the ":".
2021 */
Bram Moolenaar793648f2020-06-26 21:28:25 +02002022 p = eval_next_non_blank(*arg, evalarg, &getnext);
2023 if (*p != ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002025 emsg(_(e_missing_colon));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002027 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 return FAIL;
2029 }
Bram Moolenaar793648f2020-06-26 21:28:25 +02002030 if (getnext)
2031 *arg = eval_next_line(evalarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032
2033 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002034 * Get the third variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002036 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002037 nested_evalarg.eval_flags = !result ? orig_flags
2038 : orig_flags & ~EVAL_EVALUATE;
2039 if (eval1(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 {
2041 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002042 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 return FAIL;
2044 }
2045 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002046 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 }
2048
2049 return OK;
2050}
2051
2052/*
2053 * Handle first level expression:
2054 * expr2 || expr2 || expr2 logical OR
2055 *
2056 * "arg" must point to the first non-white of the expression.
2057 * "arg" is advanced to the next non-white after the recognized expression.
2058 *
2059 * Return OK or FAIL.
2060 */
2061 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002062eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002064 char_u *p;
2065 int getnext;
Bram Moolenaar33570922005-01-25 22:26:29 +00002066 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 long result;
2068 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002069 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070
2071 /*
2072 * Get the first variable.
2073 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002074 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 return FAIL;
2076
2077 /*
2078 * Repeat until there is no following "||".
2079 */
2080 first = TRUE;
2081 result = FALSE;
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002082 p = eval_next_non_blank(*arg, evalarg, &getnext);
2083 while (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002085 evalarg_T nested_evalarg;
2086 int evaluate;
2087 int orig_flags;
2088
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002089 if (getnext)
2090 *arg = eval_next_line(evalarg);
2091
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002092 if (evalarg == NULL)
2093 {
2094 CLEAR_FIELD(nested_evalarg);
2095 orig_flags = 0;
2096 evaluate = FALSE;
2097 }
2098 else
2099 {
2100 nested_evalarg = *evalarg;
2101 orig_flags = evalarg->eval_flags;
2102 evaluate = orig_flags & EVAL_EVALUATE;
2103 }
Bram Moolenaar32e35112020-05-14 22:41:15 +02002104
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 if (evaluate && first)
2106 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002107 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002109 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002110 if (error)
2111 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 first = FALSE;
2113 }
2114
2115 /*
2116 * Get the second variable.
2117 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002118 *arg = skipwhite_and_linebreak(*arg + 2, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002119 nested_evalarg.eval_flags = !result ? orig_flags
2120 : orig_flags & ~EVAL_EVALUATE;
2121 if (eval3(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 return FAIL;
2123
2124 /*
2125 * Compute the result.
2126 */
2127 if (evaluate && !result)
2128 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002129 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002131 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002132 if (error)
2133 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 }
2135 if (evaluate)
2136 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002137 rettv->v_type = VAR_NUMBER;
2138 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002140
2141 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 }
2143
2144 return OK;
2145}
2146
2147/*
2148 * Handle second level expression:
2149 * expr3 && expr3 && expr3 logical AND
2150 *
2151 * "arg" must point to the first non-white of the expression.
2152 * "arg" is advanced to the next non-white after the recognized expression.
2153 *
2154 * Return OK or FAIL.
2155 */
2156 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002157eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002159 char_u *p;
2160 int getnext;
Bram Moolenaar33570922005-01-25 22:26:29 +00002161 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 long result;
2163 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002164 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165
2166 /*
2167 * Get the first variable.
2168 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002169 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 return FAIL;
2171
2172 /*
2173 * Repeat until there is no following "&&".
2174 */
2175 first = TRUE;
2176 result = TRUE;
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002177 p = eval_next_non_blank(*arg, evalarg, &getnext);
2178 while (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002180 evalarg_T nested_evalarg;
2181 int orig_flags;
2182 int evaluate;
Bram Moolenaar32e35112020-05-14 22:41:15 +02002183
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002184 if (getnext)
2185 *arg = eval_next_line(evalarg);
2186
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002187 if (evalarg == NULL)
2188 {
2189 CLEAR_FIELD(nested_evalarg);
2190 orig_flags = 0;
2191 evaluate = FALSE;
2192 }
2193 else
2194 {
2195 nested_evalarg = *evalarg;
2196 orig_flags = evalarg->eval_flags;
2197 evaluate = orig_flags & EVAL_EVALUATE;
2198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 if (evaluate && first)
2200 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002201 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002203 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002204 if (error)
2205 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 first = FALSE;
2207 }
2208
2209 /*
2210 * Get the second variable.
2211 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002212 *arg = skipwhite_and_linebreak(*arg + 2, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002213 nested_evalarg.eval_flags = result ? orig_flags
2214 : orig_flags & ~EVAL_EVALUATE;
2215 if (eval4(arg, &var2, &nested_evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 return FAIL;
2217
2218 /*
2219 * Compute the result.
2220 */
2221 if (evaluate && result)
2222 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002223 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002225 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002226 if (error)
2227 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 }
2229 if (evaluate)
2230 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002231 rettv->v_type = VAR_NUMBER;
2232 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002234
2235 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 }
2237
2238 return OK;
2239}
2240
2241/*
2242 * Handle third level expression:
2243 * var1 == var2
2244 * var1 =~ var2
2245 * var1 != var2
2246 * var1 !~ var2
2247 * var1 > var2
2248 * var1 >= var2
2249 * var1 < var2
2250 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002251 * var1 is var2
2252 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 *
2254 * "arg" must point to the first non-white of the expression.
2255 * "arg" is advanced to the next non-white after the recognized expression.
2256 *
2257 * Return OK or FAIL.
2258 */
2259 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002260eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261{
Bram Moolenaar33570922005-01-25 22:26:29 +00002262 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002264 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 int i;
Bram Moolenaar87396072019-12-31 22:36:18 +01002266 exptype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269
2270 /*
2271 * Get the first variable.
2272 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002273 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 return FAIL;
2275
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002276 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 switch (p[0])
2278 {
2279 case '=': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002280 type = EXPR_EQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002282 type = EXPR_MATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 break;
2284 case '!': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002285 type = EXPR_NEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002287 type = EXPR_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 break;
2289 case '>': if (p[1] != '=')
2290 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002291 type = EXPR_GREATER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 len = 1;
2293 }
2294 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002295 type = EXPR_GEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 break;
2297 case '<': if (p[1] != '=')
2298 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002299 type = EXPR_SMALLER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 len = 1;
2301 }
2302 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002303 type = EXPR_SEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002305 case 'i': if (p[1] == 's')
2306 {
2307 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2308 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02002309 i = p[len];
2310 if (!isalnum(i) && i != '_')
Bram Moolenaar87396072019-12-31 22:36:18 +01002311 type = len == 2 ? EXPR_IS : EXPR_ISNOT;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002312 }
2313 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 }
2315
2316 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002317 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002319 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 {
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002321 if (getnext)
2322 *arg = eval_next_line(evalarg);
2323
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002324 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 if (p[len] == '?')
2326 {
2327 ic = TRUE;
2328 ++len;
2329 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002330 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 else if (p[len] == '#')
2332 {
2333 ic = FALSE;
2334 ++len;
2335 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002336 // nothing appended: use 'ignorecase'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 else
2338 ic = p_ic;
2339
2340 /*
2341 * Get the second variable.
2342 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002343 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002344 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002346 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 return FAIL;
2348 }
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002349 if (evalarg != NULL && (evalarg->eval_flags & EVAL_EVALUATE))
Bram Moolenaar31988702018-02-13 12:57:42 +01002350 {
Bram Moolenaar07a3db82019-12-25 18:14:14 +01002351 int ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002352
2353 clear_tv(&var2);
2354 return ret;
2355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 }
2357
2358 return OK;
2359}
2360
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002361 void
2362eval_addblob(typval_T *tv1, typval_T *tv2)
2363{
2364 blob_T *b1 = tv1->vval.v_blob;
2365 blob_T *b2 = tv2->vval.v_blob;
2366 blob_T *b = blob_alloc();
2367 int i;
2368
2369 if (b != NULL)
2370 {
2371 for (i = 0; i < blob_len(b1); i++)
2372 ga_append(&b->bv_ga, blob_get(b1, i));
2373 for (i = 0; i < blob_len(b2); i++)
2374 ga_append(&b->bv_ga, blob_get(b2, i));
2375
2376 clear_tv(tv1);
2377 rettv_blob_set(tv1, b);
2378 }
2379}
2380
2381 int
2382eval_addlist(typval_T *tv1, typval_T *tv2)
2383{
2384 typval_T var3;
2385
2386 // concatenate Lists
2387 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2388 {
2389 clear_tv(tv1);
2390 clear_tv(tv2);
2391 return FAIL;
2392 }
2393 clear_tv(tv1);
2394 *tv1 = var3;
2395 return OK;
2396}
2397
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398/*
2399 * Handle fourth level expression:
2400 * + number addition
2401 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002402 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002403 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 *
2405 * "arg" must point to the first non-white of the expression.
2406 * "arg" is advanced to the next non-white after the recognized expression.
2407 *
2408 * Return OK or FAIL.
2409 */
2410 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002411eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002413 int evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414
2415 /*
2416 * Get the first variable.
2417 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002418 if (eval6(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 return FAIL;
2420
2421 /*
2422 * Repeat computing, until no '+', '-' or '.' is following.
2423 */
2424 for (;;)
2425 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002426 int getnext;
2427 char_u *p;
2428 int op;
2429 int concat;
2430 typval_T var2;
2431
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002432 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002433 p = eval_next_non_blank(*arg, evalarg, &getnext);
2434 op = *p;
2435 concat = op == '.' && (*(p + 1) == '.' || current_sctx.sc_version < 2);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002436 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 break;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002438 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002439 *arg = eval_next_line(evalarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002441 if ((op != '+' || (rettv->v_type != VAR_LIST
2442 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002443#ifdef FEAT_FLOAT
2444 && (op == '.' || rettv->v_type != VAR_FLOAT)
2445#endif
2446 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002447 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002448 // For "list + ...", an illegal use of the first operand as
2449 // a number cannot be determined before evaluating the 2nd
2450 // operand: if this is also a list, all is ok.
2451 // For "something . ...", "something - ..." or "non-list + ...",
2452 // we know that the first operand needs to be a string or number
2453 // without evaluating the 2nd operand. So check before to avoid
2454 // side effects after an error.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002455 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002456 {
2457 clear_tv(rettv);
2458 return FAIL;
2459 }
2460 }
2461
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 /*
2463 * Get the second variable.
2464 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002465 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
2466 ++*arg;
Bram Moolenaar9215f012020-06-27 21:18:00 +02002467 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002468 if (eval6(arg, &var2, evalarg, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002470 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 return FAIL;
2472 }
2473
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002474 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 {
2476 /*
2477 * Compute the result.
2478 */
2479 if (op == '.')
2480 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002481 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2482 char_u *s1 = tv_get_string_buf(rettv, buf1);
2483 char_u *s2 = tv_get_string_buf_chk(&var2, buf2);
2484
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002485 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002486 {
2487 clear_tv(rettv);
2488 clear_tv(&var2);
2489 return FAIL;
2490 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002491 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002492 clear_tv(rettv);
2493 rettv->v_type = VAR_STRING;
2494 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002496 else if (op == '+' && rettv->v_type == VAR_BLOB
2497 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002498 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002499 else if (op == '+' && rettv->v_type == VAR_LIST
2500 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002501 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002502 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002503 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 else
2506 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002507 int error = FALSE;
2508 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002509#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002510 float_T f1 = 0, f2 = 0;
2511
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002512 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002513 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002514 f1 = rettv->vval.v_float;
2515 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002516 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002517 else
2518#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002519 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002520 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002521 if (error)
2522 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002523 // This can only happen for "list + non-list". For
2524 // "non-list + ..." or "something - ...", we returned
2525 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002526 clear_tv(rettv);
2527 return FAIL;
2528 }
2529#ifdef FEAT_FLOAT
2530 if (var2.v_type == VAR_FLOAT)
2531 f1 = n1;
2532#endif
2533 }
2534#ifdef FEAT_FLOAT
2535 if (var2.v_type == VAR_FLOAT)
2536 {
2537 f2 = var2.vval.v_float;
2538 n2 = 0;
2539 }
2540 else
2541#endif
2542 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002543 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002544 if (error)
2545 {
2546 clear_tv(rettv);
2547 clear_tv(&var2);
2548 return FAIL;
2549 }
2550#ifdef FEAT_FLOAT
2551 if (rettv->v_type == VAR_FLOAT)
2552 f2 = n2;
2553#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002554 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002555 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002556
2557#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002558 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002559 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2560 {
2561 if (op == '+')
2562 f1 = f1 + f2;
2563 else
2564 f1 = f1 - f2;
2565 rettv->v_type = VAR_FLOAT;
2566 rettv->vval.v_float = f1;
2567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002569#endif
2570 {
2571 if (op == '+')
2572 n1 = n1 + n2;
2573 else
2574 n1 = n1 - n2;
2575 rettv->v_type = VAR_NUMBER;
2576 rettv->vval.v_number = n1;
2577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002579 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 }
2581 }
2582 return OK;
2583}
2584
2585/*
2586 * Handle fifth level expression:
2587 * * number multiplication
2588 * / number division
2589 * % number modulo
2590 *
2591 * "arg" must point to the first non-white of the expression.
2592 * "arg" is advanced to the next non-white after the recognized expression.
2593 *
2594 * Return OK or FAIL.
2595 */
2596 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002597eval6(
2598 char_u **arg,
2599 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002600 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002601 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602{
Bram Moolenaar33570922005-01-25 22:26:29 +00002603 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002605 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002606#ifdef FEAT_FLOAT
2607 int use_float = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02002608 float_T f1 = 0, f2 = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002609#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002610 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611
2612 /*
2613 * Get the first variable.
2614 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002615 if (eval7(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 return FAIL;
2617
2618 /*
2619 * Repeat computing, until no '*', '/' or '%' is following.
2620 */
2621 for (;;)
2622 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002623 int evaluate = evalarg == NULL ? 0
2624 : (evalarg->eval_flags & EVAL_EVALUATE);
2625 int getnext;
2626
2627 op = *eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002628 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 break;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002630 if (getnext)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002631 *arg = eval_next_line(evalarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002633 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002635#ifdef FEAT_FLOAT
2636 if (rettv->v_type == VAR_FLOAT)
2637 {
2638 f1 = rettv->vval.v_float;
2639 use_float = TRUE;
2640 n1 = 0;
2641 }
2642 else
2643#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002644 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002645 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002646 if (error)
2647 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 }
2649 else
2650 n1 = 0;
2651
2652 /*
2653 * Get the second variable.
2654 */
2655 *arg = skipwhite(*arg + 1);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002656 if (eval7(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 return FAIL;
2658
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002659 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002661#ifdef FEAT_FLOAT
2662 if (var2.v_type == VAR_FLOAT)
2663 {
2664 if (!use_float)
2665 {
2666 f1 = n1;
2667 use_float = TRUE;
2668 }
2669 f2 = var2.vval.v_float;
2670 n2 = 0;
2671 }
2672 else
2673#endif
2674 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002675 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002676 clear_tv(&var2);
2677 if (error)
2678 return FAIL;
2679#ifdef FEAT_FLOAT
2680 if (use_float)
2681 f2 = n2;
2682#endif
2683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684
2685 /*
2686 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002687 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002689#ifdef FEAT_FLOAT
2690 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002692 if (op == '*')
2693 f1 = f1 * f2;
2694 else if (op == '/')
2695 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002696# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002697 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002698 if (f2 == 0.0)
2699 {
2700 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002701 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002702 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002703 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002704 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002705 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002706 }
2707 else
2708 f1 = f1 / f2;
2709# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002710 // We rely on the floating point library to handle divide
2711 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002712 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002713# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002716 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002717 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002718 return FAIL;
2719 }
2720 rettv->v_type = VAR_FLOAT;
2721 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 }
2723 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002724#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002726 if (op == '*')
2727 n1 = n1 * n2;
2728 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01002729 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01002731 n1 = num_modulus(n1, n2);
2732
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002733 rettv->v_type = VAR_NUMBER;
2734 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 }
2737 }
2738
2739 return OK;
2740}
2741
2742/*
2743 * Handle sixth level expression:
2744 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002745 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002746 * "string" string constant
2747 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 * &option-name option value
2749 * @r register contents
2750 * identifier variable value
2751 * function() function call
2752 * $VAR environment variable
2753 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002754 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002755 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002756 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002757 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 *
2759 * Also handle:
2760 * ! in front logical NOT
2761 * - in front unary minus
2762 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002763 * trailing [] subscript in String or List
2764 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02002765 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 *
2767 * "arg" must point to the first non-white of the expression.
2768 * "arg" is advanced to the next non-white after the recognized expression.
2769 *
2770 * Return OK or FAIL.
2771 */
2772 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002773eval7(
2774 char_u **arg,
2775 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002776 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002777 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002779 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
2780 int evaluate = evalarg != NULL
2781 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 int len;
2783 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 char_u *start_leader, *end_leader;
2785 int ret = OK;
2786 char_u *alias;
2787
2788 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002789 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002790 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002792 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793
2794 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02002795 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 */
2797 start_leader = *arg;
2798 while (**arg == '!' || **arg == '-' || **arg == '+')
2799 *arg = skipwhite(*arg + 1);
2800 end_leader = *arg;
2801
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002802 if (**arg == '.' && (!isdigit(*(*arg + 1))
2803#ifdef FEAT_FLOAT
2804 || current_sctx.sc_version < 2
2805#endif
2806 ))
2807 {
2808 semsg(_(e_invexpr2), *arg);
2809 ++*arg;
2810 return FAIL;
2811 }
2812
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 switch (**arg)
2814 {
2815 /*
2816 * Number constant.
2817 */
2818 case '0':
2819 case '1':
2820 case '2':
2821 case '3':
2822 case '4':
2823 case '5':
2824 case '6':
2825 case '7':
2826 case '8':
2827 case '9':
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002828 case '.': ret = get_number_tv(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02002829
2830 // Apply prefixed "-" and "+" now. Matters especially when
2831 // "->" follows.
2832 if (ret == OK && evaluate && end_leader > start_leader)
2833 ret = eval7_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 break;
2835
2836 /*
2837 * String constant: "string".
2838 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002839 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 break;
2841
2842 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002843 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002845 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002846 break;
2847
2848 /*
2849 * List: [expr, expr]
2850 */
Bram Moolenaar71478202020-06-26 22:46:27 +02002851 case '[': ret = get_list_tv(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 break;
2853
2854 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002855 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002856 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002857 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002858 {
2859 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002860 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002861 }
2862 else
2863 ret = NOTDONE;
2864 break;
2865
2866 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002867 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002868 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00002869 */
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002870 case '{': ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002871 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02002872 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002873 break;
2874
2875 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00002876 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00002878 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 break;
2880
2881 /*
2882 * Environment variable: $VAR.
2883 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002884 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 break;
2886
2887 /*
2888 * Register contents: @r.
2889 */
2890 case '@': ++*arg;
2891 if (evaluate)
2892 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002893 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002894 rettv->vval.v_string = get_reg_contents(**arg,
2895 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 }
2897 if (**arg != NUL)
2898 ++*arg;
2899 break;
2900
2901 /*
2902 * nested expression: (expression).
2903 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002904 case '(': {
Bram Moolenaar9215f012020-06-27 21:18:00 +02002905 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002906 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02002907
Bram Moolenaar9215f012020-06-27 21:18:00 +02002908 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002909 if (**arg == ')')
2910 ++*arg;
2911 else if (ret == OK)
2912 {
2913 emsg(_(e_missing_close));
2914 clear_tv(rettv);
2915 ret = FAIL;
2916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 }
2918 break;
2919
Bram Moolenaar8c711452005-01-14 21:53:12 +00002920 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 break;
2922 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002923
2924 if (ret == NOTDONE)
2925 {
2926 /*
2927 * Must be a variable or function name.
2928 * Can also be a curly-braces kind of name: {expr}.
2929 */
2930 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002931 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002932 if (alias != NULL)
2933 s = alias;
2934
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002935 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002936 ret = FAIL;
2937 else
2938 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002939 if (**arg == '(') // recursive!
Bram Moolenaar32e35112020-05-14 22:41:15 +02002940 ret = eval_func(arg, s, len, rettv, flags, NULL);
Bram Moolenaar227a69d2020-05-15 18:17:28 +02002941 else if (flags & EVAL_CONSTANT)
2942 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002943 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002944 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002945 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002946 {
2947 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002948 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002949 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002950 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01002951 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002952 }
2953
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 *arg = skipwhite(*arg);
2955
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002956 // Handle following '[', '(' and '.' for expr[expr], expr.name,
2957 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002958 if (ret == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002959 ret = handle_subscript(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960
2961 /*
2962 * Apply logical NOT and unary '-', from right to left, ignore '+'.
2963 */
2964 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02002965 ret = eval7_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002966 return ret;
2967}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002968
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002969/*
2970 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02002971 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002972 * Adjusts "end_leaderp" until it is at "start_leader".
2973 */
2974 static int
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02002975eval7_leader(
2976 typval_T *rettv,
2977 int numeric_only,
2978 char_u *start_leader,
2979 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002980{
2981 char_u *end_leader = *end_leaderp;
2982 int ret = OK;
2983 int error = FALSE;
2984 varnumber_T val = 0;
2985#ifdef FEAT_FLOAT
2986 float_T f = 0.0;
2987
2988 if (rettv->v_type == VAR_FLOAT)
2989 f = rettv->vval.v_float;
2990 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002991#endif
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002992 val = tv_get_number_chk(rettv, &error);
2993 if (error)
2994 {
2995 clear_tv(rettv);
2996 ret = FAIL;
2997 }
2998 else
2999 {
3000 while (end_leader > start_leader)
3001 {
3002 --end_leader;
3003 if (*end_leader == '!')
3004 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003005 if (numeric_only)
3006 {
3007 ++end_leader;
3008 break;
3009 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003010#ifdef FEAT_FLOAT
3011 if (rettv->v_type == VAR_FLOAT)
3012 f = !f;
3013 else
3014#endif
3015 val = !val;
3016 }
3017 else if (*end_leader == '-')
3018 {
3019#ifdef FEAT_FLOAT
3020 if (rettv->v_type == VAR_FLOAT)
3021 f = -f;
3022 else
3023#endif
3024 val = -val;
3025 }
3026 }
3027#ifdef FEAT_FLOAT
3028 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003030 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003031 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003033 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003034#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003035 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003036 clear_tv(rettv);
3037 rettv->v_type = VAR_NUMBER;
3038 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003041 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 return ret;
3043}
3044
3045/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003046 * Call the function referred to in "rettv".
3047 */
3048 static int
3049call_func_rettv(
3050 char_u **arg,
3051 typval_T *rettv,
3052 int evaluate,
3053 dict_T *selfdict,
3054 typval_T *basetv)
3055{
3056 partial_T *pt = NULL;
3057 funcexe_T funcexe;
3058 typval_T functv;
3059 char_u *s;
3060 int ret;
3061
3062 // need to copy the funcref so that we can clear rettv
3063 if (evaluate)
3064 {
3065 functv = *rettv;
3066 rettv->v_type = VAR_UNKNOWN;
3067
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003068 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003069 if (functv.v_type == VAR_PARTIAL)
3070 {
3071 pt = functv.vval.v_partial;
3072 s = partial_name(pt);
3073 }
3074 else
3075 s = functv.vval.v_string;
3076 }
3077 else
3078 s = (char_u *)"";
3079
Bram Moolenaara80faa82020-04-12 19:37:17 +02003080 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003081 funcexe.firstline = curwin->w_cursor.lnum;
3082 funcexe.lastline = curwin->w_cursor.lnum;
3083 funcexe.evaluate = evaluate;
3084 funcexe.partial = pt;
3085 funcexe.selfdict = selfdict;
3086 funcexe.basetv = basetv;
3087 ret = get_func_tv(s, -1, rettv, arg, &funcexe);
3088
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003089 // Clear the funcref afterwards, so that deleting it while
3090 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003091 if (evaluate)
3092 clear_tv(&functv);
3093
3094 return ret;
3095}
3096
3097/*
3098 * Evaluate "->method()".
3099 * "*arg" points to the '-'.
3100 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3101 */
3102 static int
3103eval_lambda(
3104 char_u **arg,
3105 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003106 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003107 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003108{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003109 int evaluate = evalarg != NULL
3110 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003111 typval_T base = *rettv;
3112 int ret;
3113
3114 // Skip over the ->.
3115 *arg += 2;
3116 rettv->v_type = VAR_UNKNOWN;
3117
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003118 ret = get_lambda_tv(arg, rettv, evalarg);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01003119 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003120 return FAIL;
3121 else if (**arg != '(')
3122 {
3123 if (verbose)
3124 {
3125 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003126 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003127 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003128 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003129 }
3130 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02003131 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003132 }
Bram Moolenaar86173482019-10-01 17:02:16 +02003133 else
3134 ret = call_func_rettv(arg, rettv, evaluate, NULL, &base);
3135
3136 // Clear the funcref afterwards, so that deleting it while
3137 // evaluating the arguments is possible (see test55).
3138 if (evaluate)
3139 clear_tv(&base);
3140
3141 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003142}
3143
3144/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003145 * Evaluate "->method()".
3146 * "*arg" points to the '-'.
3147 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3148 */
3149 static int
3150eval_method(
3151 char_u **arg,
3152 typval_T *rettv,
3153 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003154 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02003155{
3156 char_u *name;
3157 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003158 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003159 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003160 int ret;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003161
3162 // Skip over the ->.
3163 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003164 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003165
Bram Moolenaarac92e252019-08-03 21:58:38 +02003166 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003167 len = get_name_len(arg, &alias, evaluate, TRUE);
3168 if (alias != NULL)
3169 name = alias;
3170
3171 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003172 {
3173 if (verbose)
3174 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003175 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003176 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003177 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003178 {
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003179 if (**arg != '(')
3180 {
3181 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003182 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003183 ret = FAIL;
3184 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003185 else if (VIM_ISWHITE((*arg)[-1]))
3186 {
3187 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003188 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003189 ret = FAIL;
3190 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003191 else
Bram Moolenaar32e35112020-05-14 22:41:15 +02003192 ret = eval_func(arg, name, len, rettv,
3193 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003194 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003195
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003196 // Clear the funcref afterwards, so that deleting it while
3197 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003198 if (evaluate)
3199 clear_tv(&base);
3200
Bram Moolenaarac92e252019-08-03 21:58:38 +02003201 return ret;
3202}
3203
3204/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003205 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3206 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003207 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3208 */
3209 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003210eval_index(
3211 char_u **arg,
3212 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003213 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003214 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003215{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003216 int evaluate = evalarg != NULL
3217 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003218 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003219 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003220 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003221 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003222 long len = -1;
3223 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003224 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003225 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003226
Bram Moolenaara03f2332016-02-06 18:09:59 +01003227 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003228 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003229 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003230 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003231 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003232 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003233 return FAIL;
3234 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003235#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01003236 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003237 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003238 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003239#endif
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003240 case VAR_BOOL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003241 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003242 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003243 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003244 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003245 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003246 return FAIL;
3247 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003248 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003249 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003250 if (evaluate)
3251 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003252 // FALLTHROUGH
Bram Moolenaara03f2332016-02-06 18:09:59 +01003253
3254 case VAR_STRING:
3255 case VAR_NUMBER:
3256 case VAR_LIST:
3257 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003258 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003259 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003260 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003261
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003262 init_tv(&var1);
3263 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003264 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003265 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003266 /*
3267 * dict.name
3268 */
3269 key = *arg + 1;
3270 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
3271 ;
3272 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003273 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003274 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003275 }
3276 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003277 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003278 /*
3279 * something[idx]
3280 *
3281 * Get the (first) variable from inside the [].
3282 */
3283 *arg = skipwhite(*arg + 1);
3284 if (**arg == ':')
3285 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003286 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003287 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003288 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003289 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003290 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003291 clear_tv(&var1);
3292 return FAIL;
3293 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003294
3295 /*
3296 * Get the second variable from inside the [:].
3297 */
3298 if (**arg == ':')
3299 {
3300 range = TRUE;
3301 *arg = skipwhite(*arg + 1);
3302 if (**arg == ']')
3303 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003304 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003305 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003306 if (!empty1)
3307 clear_tv(&var1);
3308 return FAIL;
3309 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003310 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003311 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003312 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003313 if (!empty1)
3314 clear_tv(&var1);
3315 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003316 return FAIL;
3317 }
3318 }
3319
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003320 // Check for the ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00003321 if (**arg != ']')
3322 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003323 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003324 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003325 clear_tv(&var1);
3326 if (range)
3327 clear_tv(&var2);
3328 return FAIL;
3329 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003330 *arg = skipwhite(*arg + 1); // skip the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003331 }
3332
3333 if (evaluate)
3334 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003335 n1 = 0;
3336 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003337 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003338 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003339 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003340 }
3341 if (range)
3342 {
3343 if (empty2)
3344 n2 = -1;
3345 else
3346 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003347 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003348 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003349 }
3350 }
3351
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003352 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003353 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003354 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003355 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003356 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003357 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003358 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003359 case VAR_FLOAT:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003360 case VAR_BOOL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003361 case VAR_SPECIAL:
3362 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003363 case VAR_CHANNEL:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003364 break; // not evaluating, skipping over subscript
Bram Moolenaara03f2332016-02-06 18:09:59 +01003365
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003366 case VAR_NUMBER:
3367 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003368 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003369 len = (long)STRLEN(s);
3370 if (range)
3371 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003372 // The resulting variable is a substring. If the indexes
3373 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003374 if (n1 < 0)
3375 {
3376 n1 = len + n1;
3377 if (n1 < 0)
3378 n1 = 0;
3379 }
3380 if (n2 < 0)
3381 n2 = len + n2;
3382 else if (n2 >= len)
3383 n2 = len;
3384 if (n1 >= len || n2 < 0 || n1 > n2)
3385 s = NULL;
3386 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02003387 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003388 }
3389 else
3390 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003391 // The resulting variable is a string of a single
3392 // character. If the index is too big or negative the
3393 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003394 if (n1 >= len || n1 < 0)
3395 s = NULL;
3396 else
3397 s = vim_strnsave(s + n1, 1);
3398 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003399 clear_tv(rettv);
3400 rettv->v_type = VAR_STRING;
3401 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003402 break;
3403
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003404 case VAR_BLOB:
3405 len = blob_len(rettv->vval.v_blob);
3406 if (range)
3407 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003408 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003409 // are out of range the result is empty.
3410 if (n1 < 0)
3411 {
3412 n1 = len + n1;
3413 if (n1 < 0)
3414 n1 = 0;
3415 }
3416 if (n2 < 0)
3417 n2 = len + n2;
3418 else if (n2 >= len)
3419 n2 = len - 1;
3420 if (n1 >= len || n2 < 0 || n1 > n2)
3421 {
3422 clear_tv(rettv);
3423 rettv->v_type = VAR_BLOB;
3424 rettv->vval.v_blob = NULL;
3425 }
3426 else
3427 {
3428 blob_T *blob = blob_alloc();
3429
3430 if (blob != NULL)
3431 {
3432 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
3433 {
3434 blob_free(blob);
3435 return FAIL;
3436 }
3437 blob->bv_ga.ga_len = n2 - n1 + 1;
3438 for (i = n1; i <= n2; i++)
3439 blob_set(blob, i - n1,
3440 blob_get(rettv->vval.v_blob, i));
3441
3442 clear_tv(rettv);
3443 rettv_blob_set(rettv, blob);
3444 }
3445 }
3446 }
3447 else
3448 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003449 // The resulting variable is a byte value.
3450 // If the index is too big or negative that is an error.
3451 if (n1 < 0)
3452 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003453 if (n1 < len && n1 >= 0)
3454 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003455 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003456
3457 clear_tv(rettv);
3458 rettv->v_type = VAR_NUMBER;
3459 rettv->vval.v_number = v;
3460 }
3461 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003462 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003463 }
3464 break;
3465
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003466 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003467 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003468 if (n1 < 0)
3469 n1 = len + n1;
3470 if (!empty1 && (n1 < 0 || n1 >= len))
3471 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003472 // For a range we allow invalid values and return an empty
3473 // list. A list index out of range is an error.
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003474 if (!range)
3475 {
3476 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003477 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003478 return FAIL;
3479 }
3480 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003481 }
3482 if (range)
3483 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003484 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003485
3486 if (n2 < 0)
3487 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003488 else if (n2 >= len)
3489 n2 = len - 1;
3490 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003491 n2 = -1;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003492 l = list_slice(rettv->vval.v_list, n1, n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003493 if (l == NULL)
3494 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003495 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003496 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003497 }
3498 else
3499 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003500 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003501 clear_tv(rettv);
3502 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003503 }
3504 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003505
3506 case VAR_DICT:
3507 if (range)
3508 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003509 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003510 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003511 if (len == -1)
3512 clear_tv(&var1);
3513 return FAIL;
3514 }
3515 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003516 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003517
3518 if (len == -1)
3519 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003520 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02003521 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003522 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003523 clear_tv(&var1);
3524 return FAIL;
3525 }
3526 }
3527
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003528 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003529
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003530 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003531 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003532 if (len == -1)
3533 clear_tv(&var1);
3534 if (item == NULL)
3535 return FAIL;
3536
3537 copy_tv(&item->di_tv, &var1);
3538 clear_tv(rettv);
3539 *rettv = var1;
3540 }
3541 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003542 }
3543 }
3544
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003545 return OK;
3546}
3547
3548/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02003549 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003550 */
3551 char_u *
3552partial_name(partial_T *pt)
3553{
3554 if (pt->pt_name != NULL)
3555 return pt->pt_name;
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02003556 if (pt->pt_func != NULL)
3557 return pt->pt_func->uf_name;
3558 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003559}
3560
Bram Moolenaarddecc252016-04-06 22:59:37 +02003561 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003562partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003563{
3564 int i;
3565
3566 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003567 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003568 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003569 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003570 if (pt->pt_name != NULL)
3571 {
3572 func_unref(pt->pt_name);
3573 vim_free(pt->pt_name);
3574 }
3575 else
3576 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02003577
3578 if (pt->pt_funcstack != NULL)
3579 {
3580 // Decrease the reference count for the context of a closure. If down
3581 // to zero free it and clear the variables on the stack.
3582 if (--pt->pt_funcstack->fs_refcount == 0)
3583 {
3584 garray_T *gap = &pt->pt_funcstack->fs_ga;
3585 typval_T *stack = gap->ga_data;
3586
3587 for (i = 0; i < gap->ga_len; ++i)
3588 clear_tv(stack + i);
3589 ga_clear(gap);
3590 vim_free(pt->pt_funcstack);
3591 }
3592 pt->pt_funcstack = NULL;
3593 }
3594
Bram Moolenaarddecc252016-04-06 22:59:37 +02003595 vim_free(pt);
3596}
3597
3598/*
3599 * Unreference a closure: decrement the reference count and free it when it
3600 * becomes zero.
3601 */
3602 void
3603partial_unref(partial_T *pt)
3604{
3605 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003606 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003607}
3608
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003609/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003610 * Return the next (unique) copy ID.
3611 * Used for serializing nested structures.
3612 */
3613 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003614get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003615{
3616 current_copyID += COPYID_INC;
3617 return current_copyID;
3618}
3619
3620/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003621 * Garbage collection for lists and dictionaries.
3622 *
3623 * We use reference counts to be able to free most items right away when they
3624 * are no longer used. But for composite items it's possible that it becomes
3625 * unused while the reference count is > 0: When there is a recursive
3626 * reference. Example:
3627 * :let l = [1, 2, 3]
3628 * :let d = {9: l}
3629 * :let l[1] = d
3630 *
3631 * Since this is quite unusual we handle this with garbage collection: every
3632 * once in a while find out which lists and dicts are not referenced from any
3633 * variable.
3634 *
3635 * Here is a good reference text about garbage collection (refers to Python
3636 * but it applies to all reference-counting mechanisms):
3637 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00003638 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00003639
3640/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003641 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02003642 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003643 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00003644 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003645 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003646garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003647{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003648 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003649 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003650 buf_T *buf;
3651 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01003652 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003653 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003654
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003655 if (!testing)
3656 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003657 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003658 want_garbage_collect = FALSE;
3659 may_garbage_collect = FALSE;
3660 garbage_collect_at_exit = FALSE;
3661 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00003662
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01003663 // The execution stack can grow big, limit the size.
3664 if (exestack.ga_maxlen - exestack.ga_len > 500)
3665 {
3666 size_t new_len;
3667 char_u *pp;
3668 int n;
3669
3670 // Keep 150% of the current size, with a minimum of the growth size.
3671 n = exestack.ga_len / 2;
3672 if (n < exestack.ga_growsize)
3673 n = exestack.ga_growsize;
3674
3675 // Don't make it bigger though.
3676 if (exestack.ga_len + n < exestack.ga_maxlen)
3677 {
3678 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
3679 pp = vim_realloc(exestack.ga_data, new_len);
3680 if (pp == NULL)
3681 return FAIL;
3682 exestack.ga_maxlen = exestack.ga_len + n;
3683 exestack.ga_data = pp;
3684 }
3685 }
3686
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003687 // We advance by two because we add one for items referenced through
3688 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003689 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003690
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003691 /*
3692 * 1. Go through all accessible variables and mark all lists and dicts
3693 * with copyID.
3694 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003695
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003696 // Don't free variables in the previous_funccal list unless they are only
3697 // referenced through previous_funccal. This must be first, because if
3698 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003699 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003700
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003701 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003702 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003703
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003704 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003705 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003706 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
3707 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003708
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003709 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003710 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003711 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3712 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02003713 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003714 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
3715 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003716#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003717 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003718 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3719 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003720 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003721 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003722 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3723 NULL, NULL);
3724#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003725
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003726 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003727 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003728 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
3729 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003730 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003731 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003732
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003733 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003734 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003735
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003736 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003737 abort = abort || set_ref_in_functions(copyID);
3738
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003739 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003740 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003741
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003742 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003743 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00003744
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003745 // callbacks in buffers
3746 abort = abort || set_ref_in_buffers(copyID);
3747
Bram Moolenaar1dced572012-04-05 16:54:08 +02003748#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003749 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02003750#endif
3751
Bram Moolenaardb913952012-06-29 12:54:53 +02003752#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003753 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003754#endif
3755
3756#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003757 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02003758#endif
3759
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003760#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02003761 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02003762 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003763#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02003764#ifdef FEAT_NETBEANS_INTG
3765 abort = abort || set_ref_in_nb_channel(copyID);
3766#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01003767
Bram Moolenaare3188e22016-05-31 21:13:04 +02003768#ifdef FEAT_TIMERS
3769 abort = abort || set_ref_in_timer(copyID);
3770#endif
3771
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02003772#ifdef FEAT_QUICKFIX
3773 abort = abort || set_ref_in_quickfix(copyID);
3774#endif
3775
Bram Moolenaara2c45a12017-07-27 22:14:59 +02003776#ifdef FEAT_TERMINAL
3777 abort = abort || set_ref_in_term(copyID);
3778#endif
3779
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003780#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003781 abort = abort || set_ref_in_popups(copyID);
3782#endif
3783
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003784 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003785 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003786 /*
3787 * 2. Free lists and dictionaries that are not referenced.
3788 */
3789 did_free = free_unref_items(copyID);
3790
3791 /*
3792 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003793 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003794 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003795 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003796 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003797 else if (p_verbose > 0)
3798 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003799 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003800 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003801
3802 return did_free;
3803}
3804
3805/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003806 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003807 */
3808 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003809free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003810{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003811 int did_free = FALSE;
3812
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003813 // Let all "free" functions know that we are here. This means no
3814 // dictionaries, lists, channels or jobs are to be freed, because we will
3815 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003816 in_free_unref_items = TRUE;
3817
3818 /*
3819 * PASS 1: free the contents of the items. We don't free the items
3820 * themselves yet, so that it is possible to decrement refcount counters
3821 */
3822
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003823 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02003824 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003825
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003826 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02003827 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003828
3829#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003830 // Go through the list of jobs and free items without the copyID. This
3831 // must happen before doing channels, because jobs refer to channels, but
3832 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003833 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
3834
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003835 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003836 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
3837#endif
3838
3839 /*
3840 * PASS 2: free the items themselves.
3841 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003842 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02003843 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01003844
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003845#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003846 // Go through the list of jobs and free items without the copyID. This
3847 // must happen before doing channels, because jobs refer to channels, but
3848 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003849 free_unused_jobs(copyID, COPYID_MASK);
3850
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003851 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003852 free_unused_channels(copyID, COPYID_MASK);
3853#endif
3854
3855 in_free_unref_items = FALSE;
3856
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003857 return did_free;
3858}
3859
3860/*
3861 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003862 * "list_stack" is used to add lists to be marked. Can be NULL.
3863 *
3864 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003865 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003866 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003867set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003868{
3869 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003870 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003871 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003872 hashtab_T *cur_ht;
3873 ht_stack_T *ht_stack = NULL;
3874 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003875
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003876 cur_ht = ht;
3877 for (;;)
3878 {
3879 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003880 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003881 // Mark each item in the hashtab. If the item contains a hashtab
3882 // it is added to ht_stack, if it contains a list it is added to
3883 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003884 todo = (int)cur_ht->ht_used;
3885 for (hi = cur_ht->ht_array; todo > 0; ++hi)
3886 if (!HASHITEM_EMPTY(hi))
3887 {
3888 --todo;
3889 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
3890 &ht_stack, list_stack);
3891 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003892 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003893
3894 if (ht_stack == NULL)
3895 break;
3896
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003897 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003898 cur_ht = ht_stack->ht;
3899 tempitem = ht_stack;
3900 ht_stack = ht_stack->prev;
3901 free(tempitem);
3902 }
3903
3904 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003905}
3906
3907/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003908 * Mark a dict and its items with "copyID".
3909 * Returns TRUE if setting references failed somehow.
3910 */
3911 int
3912set_ref_in_dict(dict_T *d, int copyID)
3913{
3914 if (d != NULL && d->dv_copyID != copyID)
3915 {
3916 d->dv_copyID = copyID;
3917 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
3918 }
3919 return FALSE;
3920}
3921
3922/*
3923 * Mark a list and its items with "copyID".
3924 * Returns TRUE if setting references failed somehow.
3925 */
3926 int
3927set_ref_in_list(list_T *ll, int copyID)
3928{
3929 if (ll != NULL && ll->lv_copyID != copyID)
3930 {
3931 ll->lv_copyID = copyID;
3932 return set_ref_in_list_items(ll, copyID, NULL);
3933 }
3934 return FALSE;
3935}
3936
3937/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003938 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003939 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
3940 *
3941 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003942 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003943 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003944set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003945{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003946 listitem_T *li;
3947 int abort = FALSE;
3948 list_T *cur_l;
3949 list_stack_T *list_stack = NULL;
3950 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003951
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003952 cur_l = l;
3953 for (;;)
3954 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003955 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003956 // Mark each item in the list. If the item contains a hashtab
3957 // it is added to ht_stack, if it contains a list it is added to
3958 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003959 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
3960 abort = abort || set_ref_in_item(&li->li_tv, copyID,
3961 ht_stack, &list_stack);
3962 if (list_stack == NULL)
3963 break;
3964
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003965 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003966 cur_l = list_stack->list;
3967 tempitem = list_stack;
3968 list_stack = list_stack->prev;
3969 free(tempitem);
3970 }
3971
3972 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003973}
3974
3975/*
3976 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003977 * "list_stack" is used to add lists to be marked. Can be NULL.
3978 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
3979 *
3980 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003981 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003982 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003983set_ref_in_item(
3984 typval_T *tv,
3985 int copyID,
3986 ht_stack_T **ht_stack,
3987 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003988{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003989 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00003990
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003991 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003992 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003993 dict_T *dd = tv->vval.v_dict;
3994
Bram Moolenaara03f2332016-02-06 18:09:59 +01003995 if (dd != NULL && dd->dv_copyID != copyID)
3996 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003997 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01003998 dd->dv_copyID = copyID;
3999 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004000 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004001 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4002 }
4003 else
4004 {
4005 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
4006 if (newitem == NULL)
4007 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004008 else
4009 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004010 newitem->ht = &dd->dv_hashtab;
4011 newitem->prev = *ht_stack;
4012 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004013 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004014 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004015 }
4016 }
4017 else if (tv->v_type == VAR_LIST)
4018 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004019 list_T *ll = tv->vval.v_list;
4020
Bram Moolenaara03f2332016-02-06 18:09:59 +01004021 if (ll != NULL && ll->lv_copyID != copyID)
4022 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004023 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004024 ll->lv_copyID = copyID;
4025 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004026 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004027 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004028 }
4029 else
4030 {
4031 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004032 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004033 if (newitem == NULL)
4034 abort = TRUE;
4035 else
4036 {
4037 newitem->list = ll;
4038 newitem->prev = *list_stack;
4039 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004040 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004041 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004042 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004043 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004044 else if (tv->v_type == VAR_FUNC)
4045 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004046 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004047 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004048 else if (tv->v_type == VAR_PARTIAL)
4049 {
4050 partial_T *pt = tv->vval.v_partial;
4051 int i;
4052
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004053 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004054 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004055 // Didn't see this partial yet.
4056 pt->pt_copyID = copyID;
4057
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004058 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004059
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004060 if (pt->pt_dict != NULL)
4061 {
4062 typval_T dtv;
4063
4064 dtv.v_type = VAR_DICT;
4065 dtv.vval.v_dict = pt->pt_dict;
4066 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4067 }
4068
4069 for (i = 0; i < pt->pt_argc; ++i)
4070 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4071 ht_stack, list_stack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004072 if (pt->pt_funcstack != NULL)
4073 {
4074 typval_T *stack = pt->pt_funcstack->fs_ga.ga_data;
4075
4076 for (i = 0; i < pt->pt_funcstack->fs_ga.ga_len; ++i)
4077 abort = abort || set_ref_in_item(stack + i, copyID,
4078 ht_stack, list_stack);
4079 }
4080
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004081 }
4082 }
4083#ifdef FEAT_JOB_CHANNEL
4084 else if (tv->v_type == VAR_JOB)
4085 {
4086 job_T *job = tv->vval.v_job;
4087 typval_T dtv;
4088
4089 if (job != NULL && job->jv_copyID != copyID)
4090 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004091 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004092 if (job->jv_channel != NULL)
4093 {
4094 dtv.v_type = VAR_CHANNEL;
4095 dtv.vval.v_channel = job->jv_channel;
4096 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4097 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004098 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004099 {
4100 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004101 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004102 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4103 }
4104 }
4105 }
4106 else if (tv->v_type == VAR_CHANNEL)
4107 {
4108 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004109 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004110 typval_T dtv;
4111 jsonq_T *jq;
4112 cbq_T *cq;
4113
4114 if (ch != NULL && ch->ch_copyID != copyID)
4115 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004116 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004117 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004118 {
4119 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4120 jq = jq->jq_next)
4121 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4122 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4123 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004124 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004125 {
4126 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004127 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004128 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4129 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004130 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004131 {
4132 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004133 dtv.vval.v_partial =
4134 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004135 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4136 }
4137 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004138 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004139 {
4140 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004141 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004142 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4143 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004144 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004145 {
4146 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004147 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004148 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4149 }
4150 }
4151 }
4152#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004153 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004154}
4155
Bram Moolenaar8c711452005-01-14 21:53:12 +00004156/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004157 * Return a string with the string representation of a variable.
4158 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004159 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004160 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004161 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4162 * stings as "string()", otherwise does not put quotes around strings, as
4163 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004164 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4165 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004166 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004167 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004168 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004169echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004170 typval_T *tv,
4171 char_u **tofree,
4172 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004173 int copyID,
4174 int echo_style,
4175 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004176 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004177{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004178 static int recurse = 0;
4179 char_u *r = NULL;
4180
Bram Moolenaar33570922005-01-25 22:26:29 +00004181 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004182 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004183 if (!did_echo_string_emsg)
4184 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004185 // Only give this message once for a recursive call to avoid
4186 // flooding the user with errors. And stop iterating over lists
4187 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004188 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004189 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004190 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004191 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004192 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004193 }
4194 ++recurse;
4195
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004196 switch (tv->v_type)
4197 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004198 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004199 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004200 {
4201 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004202 r = tv->vval.v_string;
4203 if (r == NULL)
4204 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004205 }
4206 else
4207 {
4208 *tofree = string_quote(tv->vval.v_string, FALSE);
4209 r = *tofree;
4210 }
4211 break;
4212
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004213 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004214 if (echo_style)
4215 {
4216 *tofree = NULL;
4217 r = tv->vval.v_string;
4218 }
4219 else
4220 {
4221 *tofree = string_quote(tv->vval.v_string, TRUE);
4222 r = *tofree;
4223 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004224 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004225
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004226 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004227 {
4228 partial_T *pt = tv->vval.v_partial;
4229 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004230 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004231 garray_T ga;
4232 int i;
4233 char_u *tf;
4234
4235 ga_init2(&ga, 1, 100);
4236 ga_concat(&ga, (char_u *)"function(");
4237 if (fname != NULL)
4238 {
4239 ga_concat(&ga, fname);
4240 vim_free(fname);
4241 }
4242 if (pt != NULL && pt->pt_argc > 0)
4243 {
4244 ga_concat(&ga, (char_u *)", [");
4245 for (i = 0; i < pt->pt_argc; ++i)
4246 {
4247 if (i > 0)
4248 ga_concat(&ga, (char_u *)", ");
4249 ga_concat(&ga,
4250 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4251 vim_free(tf);
4252 }
4253 ga_concat(&ga, (char_u *)"]");
4254 }
4255 if (pt != NULL && pt->pt_dict != NULL)
4256 {
4257 typval_T dtv;
4258
4259 ga_concat(&ga, (char_u *)", ");
4260 dtv.v_type = VAR_DICT;
4261 dtv.vval.v_dict = pt->pt_dict;
4262 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4263 vim_free(tf);
4264 }
4265 ga_concat(&ga, (char_u *)")");
4266
4267 *tofree = ga.ga_data;
4268 r = *tofree;
4269 break;
4270 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004271
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004272 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004273 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004274 break;
4275
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004276 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004277 if (tv->vval.v_list == NULL)
4278 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004279 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004280 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004281 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004282 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004283 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4284 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004285 {
4286 *tofree = NULL;
4287 r = (char_u *)"[...]";
4288 }
4289 else
4290 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004291 int old_copyID = tv->vval.v_list->lv_copyID;
4292
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004293 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004294 *tofree = list2string(tv, copyID, restore_copyID);
4295 if (restore_copyID)
4296 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004297 r = *tofree;
4298 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004299 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004300
Bram Moolenaar8c711452005-01-14 21:53:12 +00004301 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004302 if (tv->vval.v_dict == NULL)
4303 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004304 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004305 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004306 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004307 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004308 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4309 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004310 {
4311 *tofree = NULL;
4312 r = (char_u *)"{...}";
4313 }
4314 else
4315 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004316 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004317
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004318 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004319 *tofree = dict2string(tv, copyID, restore_copyID);
4320 if (restore_copyID)
4321 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004322 r = *tofree;
4323 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004324 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004325
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004326 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004327 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004328 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004329 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004330 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004331 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004332 break;
4333
Bram Moolenaar835dc632016-02-07 14:27:38 +01004334 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004335 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004336 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004337 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004338 if (composite_val)
4339 {
4340 *tofree = string_quote(r, FALSE);
4341 r = *tofree;
4342 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004343 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004344
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004345 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004346#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004347 *tofree = NULL;
4348 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4349 r = numbuf;
4350 break;
4351#endif
4352
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004353 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004354 case VAR_SPECIAL:
4355 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004356 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004357 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004358 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004359
Bram Moolenaar8502c702014-06-17 12:51:16 +02004360 if (--recurse == 0)
4361 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004362 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004363}
4364
4365/*
4366 * Return a string with the string representation of a variable.
4367 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4368 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004369 * Does not put quotes around strings, as ":echo" displays values.
4370 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4371 * May return NULL.
4372 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004373 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004374echo_string(
4375 typval_T *tv,
4376 char_u **tofree,
4377 char_u *numbuf,
4378 int copyID)
4379{
4380 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4381}
4382
4383/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004384 * Return string "str" in ' quotes, doubling ' characters.
4385 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004386 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004387 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004388 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004389string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004390{
Bram Moolenaar33570922005-01-25 22:26:29 +00004391 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004392 char_u *p, *r, *s;
4393
Bram Moolenaar33570922005-01-25 22:26:29 +00004394 len = (function ? 13 : 3);
4395 if (str != NULL)
4396 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004397 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004398 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004399 if (*p == '\'')
4400 ++len;
4401 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004402 s = r = alloc(len);
4403 if (r != NULL)
4404 {
4405 if (function)
4406 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004407 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004408 r += 10;
4409 }
4410 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004411 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004412 if (str != NULL)
4413 for (p = str; *p != NUL; )
4414 {
4415 if (*p == '\'')
4416 *r++ = '\'';
4417 MB_COPY_CHAR(p, r);
4418 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004419 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004420 if (function)
4421 *r++ = ')';
4422 *r++ = NUL;
4423 }
4424 return s;
4425}
4426
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004427#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004428/*
4429 * Convert the string "text" to a floating point number.
4430 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4431 * this always uses a decimal point.
4432 * Returns the length of the text that was consumed.
4433 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004434 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004435string2float(
4436 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004437 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004438{
4439 char *s = (char *)text;
4440 float_T f;
4441
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004442 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004443 if (STRNICMP(text, "inf", 3) == 0)
4444 {
4445 *value = INFINITY;
4446 return 3;
4447 }
4448 if (STRNICMP(text, "-inf", 3) == 0)
4449 {
4450 *value = -INFINITY;
4451 return 4;
4452 }
4453 if (STRNICMP(text, "nan", 3) == 0)
4454 {
4455 *value = NAN;
4456 return 3;
4457 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004458 f = strtod(s, &s);
4459 *value = f;
4460 return (int)((char_u *)s - text);
4461}
4462#endif
4463
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004464/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004466 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004468 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004469var2fpos(
4470 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004471 int dollar_lnum, // TRUE when $ is last line
4472 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004474 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004476 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004478 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004479 if (varp->v_type == VAR_LIST)
4480 {
4481 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004482 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004483 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004484 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004485
4486 l = varp->vval.v_list;
4487 if (l == NULL)
4488 return NULL;
4489
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004490 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004491 pos.lnum = list_find_nr(l, 0L, &error);
4492 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004493 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004494
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004495 // Get the column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004496 pos.col = list_find_nr(l, 1L, &error);
4497 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004498 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004499 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004500
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004501 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004502 li = list_find(l, 1L);
4503 if (li != NULL && li->li_tv.v_type == VAR_STRING
4504 && li->li_tv.vval.v_string != NULL
4505 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
4506 pos.col = len + 1;
4507
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004508 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004509 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004510 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004511 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004512
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004513 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004514 pos.coladd = list_find_nr(l, 2L, &error);
4515 if (error)
4516 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004517
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004518 return &pos;
4519 }
4520
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004521 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004522 if (name == NULL)
4523 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004524 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004526 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004527 {
4528 if (VIsual_active)
4529 return &VIsual;
4530 return &curwin->w_cursor;
4531 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004532 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004534 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4536 return NULL;
4537 return pp;
4538 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004539
Bram Moolenaara5525202006-03-02 22:52:09 +00004540 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004541
Bram Moolenaar477933c2007-07-17 14:32:23 +00004542 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004543 {
4544 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004545 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004546 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004547 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004548 // In silent Ex mode topline is zero, but that's not a valid line
4549 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004550 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004551 return &pos;
4552 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004553 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004554 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004555 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004556 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004557 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004558 return &pos;
4559 }
4560 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004561 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004563 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 {
4565 pos.lnum = curbuf->b_ml.ml_line_count;
4566 pos.col = 0;
4567 }
4568 else
4569 {
4570 pos.lnum = curwin->w_cursor.lnum;
4571 pos.col = (colnr_T)STRLEN(ml_get_curline());
4572 }
4573 return &pos;
4574 }
4575 return NULL;
4576}
4577
4578/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004579 * Convert list in "arg" into a position and optional file number.
4580 * When "fnump" is NULL there is no file number, only 3 items.
4581 * Note that the column is passed on as-is, the caller may want to decrement
4582 * it to use 1 for the first column.
4583 * Return FAIL when conversion is not possible, doesn't check the position for
4584 * validity.
4585 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004586 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004587list2fpos(
4588 typval_T *arg,
4589 pos_T *posp,
4590 int *fnump,
4591 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004592{
4593 list_T *l = arg->vval.v_list;
4594 long i = 0;
4595 long n;
4596
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004597 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
4598 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00004599 if (arg->v_type != VAR_LIST
4600 || l == NULL
4601 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02004602 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004603 return FAIL;
4604
4605 if (fnump != NULL)
4606 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004607 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004608 if (n < 0)
4609 return FAIL;
4610 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004611 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004612 *fnump = n;
4613 }
4614
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004615 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004616 if (n < 0)
4617 return FAIL;
4618 posp->lnum = n;
4619
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004620 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004621 if (n < 0)
4622 return FAIL;
4623 posp->col = n;
4624
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004625 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004626 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00004627 posp->coladd = 0;
4628 else
4629 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004630
Bram Moolenaar493c1782014-05-28 14:34:46 +02004631 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004632 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02004633
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004634 return OK;
4635}
4636
4637/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 * Get the length of an environment variable name.
4639 * Advance "arg" to the first character after the name.
4640 * Return 0 for error.
4641 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004642 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004643get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644{
4645 char_u *p;
4646 int len;
4647
4648 for (p = *arg; vim_isIDc(*p); ++p)
4649 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004650 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 return 0;
4652
4653 len = (int)(p - *arg);
4654 *arg = p;
4655 return len;
4656}
4657
4658/*
4659 * Get the length of the name of a function or internal variable.
4660 * "arg" is advanced to the first non-white character after the name.
4661 * Return 0 if something is wrong.
4662 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004663 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004664get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665{
4666 char_u *p;
4667 int len;
4668
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004669 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004671 {
4672 if (*p == ':')
4673 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004674 // "s:" is start of "s:var", but "n:" is not and can be used in
4675 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004676 len = (int)(p - *arg);
4677 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
4678 || len > 1)
4679 break;
4680 }
4681 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004682 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 return 0;
4684
4685 len = (int)(p - *arg);
4686 *arg = skipwhite(p);
4687
4688 return len;
4689}
4690
4691/*
Bram Moolenaara7043832005-01-21 11:56:39 +00004692 * Get the length of the name of a variable or function.
4693 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004695 * Return -1 if curly braces expansion failed.
4696 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 * If the name contains 'magic' {}'s, expand them and return the
4698 * expanded name in an allocated string via 'alias' - caller must free.
4699 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004700 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004701get_name_len(
4702 char_u **arg,
4703 char_u **alias,
4704 int evaluate,
4705 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706{
4707 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 char_u *p;
4709 char_u *expr_start;
4710 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004712 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713
4714 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
4715 && (*arg)[2] == (int)KE_SNR)
4716 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004717 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 *arg += 3;
4719 return get_id_len(arg) + 3;
4720 }
4721 len = eval_fname_script(*arg);
4722 if (len > 0)
4723 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004724 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 *arg += len;
4726 }
4727
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004729 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004731 p = find_name_end(*arg, &expr_start, &expr_end,
4732 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 if (expr_start != NULL)
4734 {
4735 char_u *temp_string;
4736
4737 if (!evaluate)
4738 {
4739 len += (int)(p - *arg);
4740 *arg = skipwhite(p);
4741 return len;
4742 }
4743
4744 /*
4745 * Include any <SID> etc in the expanded string:
4746 * Thus the -len here.
4747 */
4748 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
4749 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004750 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 *alias = temp_string;
4752 *arg = skipwhite(p);
4753 return (int)STRLEN(temp_string);
4754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755
4756 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01004757 // Only give an error when there is something, otherwise it will be
4758 // reported at a higher level.
4759 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004760 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761
4762 return len;
4763}
4764
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004765/*
4766 * Find the end of a variable or function name, taking care of magic braces.
4767 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
4768 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004769 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004770 * Return a pointer to just after the name. Equal to "arg" if there is no
4771 * valid name.
4772 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004773 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004774find_name_end(
4775 char_u *arg,
4776 char_u **expr_start,
4777 char_u **expr_end,
4778 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004780 int mb_nest = 0;
4781 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004783 int len;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004784 int vim9script = current_sctx.sc_version == SCRIPT_VERSION_VIM9;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004786 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004788 *expr_start = NULL;
4789 *expr_end = NULL;
4790 }
4791
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004792 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004793 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
4794 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004795 return arg;
4796
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004797 for (p = arg; *p != NUL
4798 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004799 || (*p == '{' && !vim9script)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004800 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004801 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004802 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004803 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00004804 if (*p == '\'')
4805 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004806 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004807 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00004808 ;
4809 if (*p == NUL)
4810 break;
4811 }
4812 else if (*p == '"')
4813 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004814 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004815 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00004816 if (*p == '\\' && p[1] != NUL)
4817 ++p;
4818 if (*p == NUL)
4819 break;
4820 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004821 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
4822 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004823 // "s:" is start of "s:var", but "n:" is not and can be used in
4824 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004825 len = (int)(p - arg);
4826 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01004827 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004828 break;
4829 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00004830
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004831 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004833 if (*p == '[')
4834 ++br_nest;
4835 else if (*p == ']')
4836 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00004838
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004839 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004841 if (*p == '{')
4842 {
4843 mb_nest++;
4844 if (expr_start != NULL && *expr_start == NULL)
4845 *expr_start = p;
4846 }
4847 else if (*p == '}')
4848 {
4849 mb_nest--;
4850 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
4851 *expr_end = p;
4852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 }
4855
4856 return p;
4857}
4858
4859/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004860 * Expands out the 'magic' {}'s in a variable/function name.
4861 * Note that this can call itself recursively, to deal with
4862 * constructs like foo{bar}{baz}{bam}
4863 * The four pointer arguments point to "foo{expre}ss{ion}bar"
4864 * "in_start" ^
4865 * "expr_start" ^
4866 * "expr_end" ^
4867 * "in_end" ^
4868 *
4869 * Returns a new allocated string, which the caller must free.
4870 * Returns NULL for failure.
4871 */
4872 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004873make_expanded_name(
4874 char_u *in_start,
4875 char_u *expr_start,
4876 char_u *expr_end,
4877 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004878{
4879 char_u c1;
4880 char_u *retval = NULL;
4881 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004882
4883 if (expr_end == NULL || in_end == NULL)
4884 return NULL;
4885 *expr_start = NUL;
4886 *expr_end = NUL;
4887 c1 = *in_end;
4888 *in_end = NUL;
4889
Bram Moolenaarb171fb12020-06-24 20:34:03 +02004890 temp_result = eval_to_string(expr_start + 1, FALSE);
4891 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004892 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02004893 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
4894 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004895 if (retval != NULL)
4896 {
4897 STRCPY(retval, in_start);
4898 STRCAT(retval, temp_result);
4899 STRCAT(retval, expr_end + 1);
4900 }
4901 }
4902 vim_free(temp_result);
4903
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004904 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004905 *expr_start = '{';
4906 *expr_end = '}';
4907
4908 if (retval != NULL)
4909 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004910 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004911 if (expr_start != NULL)
4912 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004913 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004914 temp_result = make_expanded_name(retval, expr_start,
4915 expr_end, temp_result);
4916 vim_free(retval);
4917 retval = temp_result;
4918 }
4919 }
4920
4921 return retval;
4922}
4923
4924/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00004926 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004928 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004929eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004931 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
4932}
4933
4934/*
4935 * Return TRUE if character "c" can be used as the first character in a
4936 * variable or function name (excluding '{' and '}').
4937 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004938 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004939eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00004940{
4941 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942}
4943
4944/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004945 * Handle:
4946 * - expr[expr], expr[expr:expr] subscript
4947 * - ".name" lookup
4948 * - function call with Funcref variable: func(expr)
4949 * - method call: var->method()
4950 *
4951 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004952 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004953 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004954handle_subscript(
4955 char_u **arg,
4956 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004957 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004958 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004959{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004960 int evaluate = evalarg != NULL
4961 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004962 int ret = OK;
4963 dict_T *selfdict = NULL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004964
Bram Moolenaar61343f02019-07-20 21:11:13 +02004965 // "." is ".name" lookup when we found a dict or when evaluating and
4966 // scriptversion is at least 2, where string concatenation is "..".
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004967 while (ret == OK
Bram Moolenaarac92e252019-08-03 21:58:38 +02004968 && (((**arg == '['
4969 || (**arg == '.' && (rettv->v_type == VAR_DICT
Bram Moolenaar61343f02019-07-20 21:11:13 +02004970 || (!evaluate
4971 && (*arg)[1] != '.'
4972 && current_sctx.sc_version >= 2)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02004973 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004974 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02004975 && !VIM_ISWHITE(*(*arg - 1)))
4976 || (**arg == '-' && (*arg)[1] == '>')))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004977 {
4978 if (**arg == '(')
4979 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004980 ret = call_func_rettv(arg, rettv, evaluate, selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01004981
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004982 // Stop the expression evaluation when immediately aborting on
4983 // error, or when an interrupt occurred or an exception was thrown
4984 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004985 if (aborting())
4986 {
4987 if (ret == OK)
4988 clear_tv(rettv);
4989 ret = FAIL;
4990 }
4991 dict_unref(selfdict);
4992 selfdict = NULL;
4993 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004994 else if (**arg == '-')
4995 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004996 if (ret == OK)
4997 {
4998 if ((*arg)[2] == '{')
4999 // expr->{lambda}()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005000 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005001 else
5002 // expr->name()
5003 ret = eval_method(arg, rettv, evaluate, verbose);
5004 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005005 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005006 else // **arg == '[' || **arg == '.'
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005007 {
5008 dict_unref(selfdict);
5009 if (rettv->v_type == VAR_DICT)
5010 {
5011 selfdict = rettv->vval.v_dict;
5012 if (selfdict != NULL)
5013 ++selfdict->dv_refcount;
5014 }
5015 else
5016 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005017 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005018 {
5019 clear_tv(rettv);
5020 ret = FAIL;
5021 }
5022 }
5023 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005024
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005025 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5026 // Don't do this when "Func" is already a partial that was bound
5027 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005028 if (selfdict != NULL
5029 && (rettv->v_type == VAR_FUNC
5030 || (rettv->v_type == VAR_PARTIAL
5031 && (rettv->vval.v_partial->pt_auto
5032 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005033 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005034
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005035 dict_unref(selfdict);
5036 return ret;
5037}
5038
5039/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005040 * Make a copy of an item.
5041 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005042 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5043 * reference to an already copied list/dict can be used.
5044 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005045 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005046 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005047item_copy(
5048 typval_T *from,
5049 typval_T *to,
5050 int deep,
5051 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005052{
5053 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005054 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005055
Bram Moolenaar33570922005-01-25 22:26:29 +00005056 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005057 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005058 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005059 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005060 }
5061 ++recurse;
5062
5063 switch (from->v_type)
5064 {
5065 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005066 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005067 case VAR_STRING:
5068 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005069 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005070 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005071 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005072 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005073 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005074 copy_tv(from, to);
5075 break;
5076 case VAR_LIST:
5077 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005078 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005079 if (from->vval.v_list == NULL)
5080 to->vval.v_list = NULL;
5081 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5082 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005083 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005084 to->vval.v_list = from->vval.v_list->lv_copylist;
5085 ++to->vval.v_list->lv_refcount;
5086 }
5087 else
5088 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5089 if (to->vval.v_list == NULL)
5090 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005091 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005092 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005093 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005094 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005095 case VAR_DICT:
5096 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005097 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005098 if (from->vval.v_dict == NULL)
5099 to->vval.v_dict = NULL;
5100 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5101 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005102 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005103 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5104 ++to->vval.v_dict->dv_refcount;
5105 }
5106 else
5107 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5108 if (to->vval.v_dict == NULL)
5109 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005110 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005111 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005112 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005113 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005114 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005115 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005116 }
5117 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005118 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005119}
5120
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005121 void
5122echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
5123{
5124 char_u *tofree;
5125 char_u numbuf[NUMBUFLEN];
5126 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
5127
5128 if (*atstart)
5129 {
5130 *atstart = FALSE;
5131 // Call msg_start() after eval1(), evaluating the expression
5132 // may cause a message to appear.
5133 if (with_space)
5134 {
5135 // Mark the saved text as finishing the line, so that what
5136 // follows is displayed on a new line when scrolling back
5137 // at the more prompt.
5138 msg_sb_eol();
5139 msg_start();
5140 }
5141 }
5142 else if (with_space)
5143 msg_puts_attr(" ", echo_attr);
5144
5145 if (p != NULL)
5146 for ( ; *p != NUL && !got_int; ++p)
5147 {
5148 if (*p == '\n' || *p == '\r' || *p == TAB)
5149 {
5150 if (*p != TAB && *needclr)
5151 {
5152 // remove any text still there from the command
5153 msg_clr_eos();
5154 *needclr = FALSE;
5155 }
5156 msg_putchar_attr(*p, echo_attr);
5157 }
5158 else
5159 {
5160 if (has_mbyte)
5161 {
5162 int i = (*mb_ptr2len)(p);
5163
5164 (void)msg_outtrans_len_attr(p, i, echo_attr);
5165 p += i - 1;
5166 }
5167 else
5168 (void)msg_outtrans_len_attr(p, 1, echo_attr);
5169 }
5170 }
5171 vim_free(tofree);
5172}
5173
Bram Moolenaare9a41262005-01-15 22:18:47 +00005174/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 * ":echo expr1 ..." print each argument separated with a space, add a
5176 * newline at the end.
5177 * ":echon expr1 ..." print each argument plain.
5178 */
5179 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005180ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181{
5182 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005183 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 char_u *p;
5185 int needclr = TRUE;
5186 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01005187 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005188 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005189 evalarg_T evalarg;
5190
5191 CLEAR_FIELD(evalarg);
5192 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193
5194 if (eap->skip)
5195 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005196 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005198 // If eval1() causes an error message the text from the command may
5199 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005200 need_clr_eos = needclr;
5201
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 p = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005203 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 {
5205 /*
5206 * Report the invalid expression unless the expression evaluation
5207 * has been cancelled due to an aborting error, an interrupt, or an
5208 * exception.
5209 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01005210 if (!aborting() && did_emsg == did_emsg_before
5211 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005212 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005213 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 break;
5215 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005216 need_clr_eos = FALSE;
5217
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005219 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005220
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005221 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 arg = skipwhite(arg);
5223 }
5224 eap->nextcmd = check_nextcmd(arg);
5225
5226 if (eap->skip)
5227 --emsg_skip;
5228 else
5229 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005230 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 if (needclr)
5232 msg_clr_eos();
5233 if (eap->cmdidx == CMD_echo)
5234 msg_end();
5235 }
5236}
5237
5238/*
5239 * ":echohl {name}".
5240 */
5241 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005242ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005244 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245}
5246
5247/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005248 * Returns the :echo attribute
5249 */
5250 int
5251get_echo_attr(void)
5252{
5253 return echo_attr;
5254}
5255
5256/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 * ":execute expr1 ..." execute the result of an expression.
5258 * ":echomsg expr1 ..." Print a message
5259 * ":echoerr expr1 ..." Print an error
5260 * Each gets spaces around each argument and a newline at the end for
5261 * echo commands
5262 */
5263 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005264ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265{
5266 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005267 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 int ret = OK;
5269 char_u *p;
5270 garray_T ga;
5271 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272
5273 ga_init2(&ga, 1, 80);
5274
5275 if (eap->skip)
5276 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02005277 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01005279 ret = eval1_emsg(&arg, &rettv, !eap->skip);
5280 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282
5283 if (!eap->skip)
5284 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005285 char_u buf[NUMBUFLEN];
5286
5287 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01005288 {
5289 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
5290 {
5291 emsg(_(e_inval_string));
5292 p = NULL;
5293 }
5294 else
5295 p = tv_get_string_buf(&rettv, buf);
5296 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005297 else
5298 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005299 if (p == NULL)
5300 {
5301 clear_tv(&rettv);
5302 ret = FAIL;
5303 break;
5304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 len = (int)STRLEN(p);
5306 if (ga_grow(&ga, len + 2) == FAIL)
5307 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005308 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 ret = FAIL;
5310 break;
5311 }
5312 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 ga.ga_len += len;
5316 }
5317
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005318 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 arg = skipwhite(arg);
5320 }
5321
5322 if (ret != FAIL && ga.ga_data != NULL)
5323 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005324 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
5325 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005326 // Mark the already saved text as finishing the line, so that what
5327 // follows is displayed on a new line when scrolling back at the
5328 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005329 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01005330 }
5331
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00005333 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005334 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005335 out_flush();
5336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 else if (eap->cmdidx == CMD_echoerr)
5338 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02005339 int save_did_emsg = did_emsg;
5340
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005341 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005342 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 if (!force_abort)
5344 did_emsg = save_did_emsg;
5345 }
5346 else if (eap->cmdidx == CMD_execute)
5347 do_cmdline((char_u *)ga.ga_data,
5348 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
5349 }
5350
5351 ga_clear(&ga);
5352
5353 if (eap->skip)
5354 --emsg_skip;
5355
5356 eap->nextcmd = check_nextcmd(arg);
5357}
5358
5359/*
5360 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
5361 * "arg" points to the "&" or '+' when called, to "option" when returning.
5362 * Returns NULL when no option name found. Otherwise pointer to the char
5363 * after the option name.
5364 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005365 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005366find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367{
5368 char_u *p = *arg;
5369
5370 ++p;
5371 if (*p == 'g' && p[1] == ':')
5372 {
5373 *opt_flags = OPT_GLOBAL;
5374 p += 2;
5375 }
5376 else if (*p == 'l' && p[1] == ':')
5377 {
5378 *opt_flags = OPT_LOCAL;
5379 p += 2;
5380 }
5381 else
5382 *opt_flags = 0;
5383
5384 if (!ASCII_ISALPHA(*p))
5385 return NULL;
5386 *arg = p;
5387
5388 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005389 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 else
5391 while (ASCII_ISALPHA(*p))
5392 ++p;
5393 return p;
5394}
5395
5396/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00005397 * Display script name where an item was last set.
5398 * Should only be invoked when 'verbose' is non-zero.
5399 */
5400 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005401last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005402{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005403 char_u *p;
5404
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005405 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00005406 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005407 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005408 if (p != NULL)
5409 {
5410 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01005411 msg_puts(_("\n\tLast set from "));
5412 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005413 if (script_ctx.sc_lnum > 0)
5414 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01005415 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005416 msg_outnum((long)script_ctx.sc_lnum);
5417 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005418 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005419 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00005420 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00005421 }
5422}
5423
Bram Moolenaarb005cd82019-09-04 15:54:55 +02005424#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425
5426/*
5427 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005428 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 * "flags" can be "g" to do a global substitute.
5430 * Returns an allocated string, NULL for error.
5431 */
5432 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005433do_string_sub(
5434 char_u *str,
5435 char_u *pat,
5436 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005437 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01005438 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439{
5440 int sublen;
5441 regmatch_T regmatch;
5442 int i;
5443 int do_all;
5444 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005445 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 garray_T ga;
5447 char_u *ret;
5448 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005449 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005451 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005453 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454
5455 ga_init2(&ga, 1, 200);
5456
5457 do_all = (flags[0] == 'g');
5458
5459 regmatch.rm_ic = p_ic;
5460 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
5461 if (regmatch.regprog != NULL)
5462 {
5463 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01005464 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
5466 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005467 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01005468 if (regmatch.startp[0] == regmatch.endp[0])
5469 {
5470 if (zero_width == regmatch.startp[0])
5471 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005472 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02005473 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02005474 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
5475 (size_t)i);
5476 ga.ga_len += i;
5477 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01005478 continue;
5479 }
5480 zero_width = regmatch.startp[0];
5481 }
5482
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 /*
5484 * Get some space for a temporary buffer to do the substitution
5485 * into. It will contain:
5486 * - The text up to where the match is.
5487 * - The substituted text.
5488 * - The text after the match.
5489 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005490 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01005491 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
5493 {
5494 ga_clear(&ga);
5495 break;
5496 }
5497
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005498 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 i = (int)(regmatch.startp[0] - tail);
5500 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005501 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02005502 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 + ga.ga_len + i, TRUE, TRUE, FALSE);
5504 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02005505 tail = regmatch.endp[0];
5506 if (*tail == NUL)
5507 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 if (!do_all)
5509 break;
5510 }
5511
5512 if (ga.ga_data != NULL)
5513 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
5514
Bram Moolenaar473de612013-06-08 18:19:48 +02005515 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 }
5517
5518 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
5519 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005520 if (p_cpo == empty_option)
5521 p_cpo = save_cpo;
5522 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005523 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00005524 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525
5526 return ret;
5527}